package com.example.base.utils

import android.app.Activity
import android.content.res.Configuration
import com.example.base.utils.ScreenUtils
import com.example.base.utils.Utils
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method

object TabletUtils {
    /**
     * 判断是否平板设备，此值不会改变
     */
    val isTabletDevice: Boolean by lazy {
        SystemPropertiesProxy["ro.build.characteristics"].contains("tablet")
    }

    /**
     * 动态判断是否平板窗口
     * 在平板设备上，也可能返回false。如分屏模式下
     * 如想判断物理设备是不是平板，请使用 isTabletDevice
     * @return true:平板,false:手机
     * @see isTabletDevice
     */
    fun isTabletWindow(): Boolean {
        return Utils.getApp().resources.configuration.screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK >=
                Configuration.SCREENLAYOUT_SIZE_LARGE
    }

    /**
     * 判断是否平行窗口模式（华为、小米）
     */
    fun inMagicWindow(): Boolean {
        val config: String = Utils.getApp().resources.configuration.toString()
        return config.contains("hwMultiwindow-magic") || config.contains("miui-magic-windows") || config.contains("hw-magic-windows")
    }

    /**
     * 判断是否窗口是横屏
     */
    fun isWindowLandscape(): Boolean {
        val orientation: Int = Utils.getApp().resources.configuration.orientation
        return orientation == Configuration.ORIENTATION_LANDSCAPE
    }

    /**
     * 判断是否设备是横屏
     */
    fun isDeviceLandscape(): Boolean {
        return ScreenUtils.getScreenWidth() > ScreenUtils.getScreenHeight()
    }

    /**
     * 判断是否在分屏模式
     */
    fun isInMultiWindowMode(activity: Activity): Boolean {
        return activity.isInMultiWindowMode
    }
}

object SystemPropertiesProxy {
    operator fun get(key: String?): String {
        var result = ""
        try {
            val c = Class.forName("android.os.SystemProperties")
            val get: Method = c.getMethod("get", String::class.java)
            result = get.invoke(c, key)?.toString() ?: ""
        } catch (e: ClassNotFoundException) {
            e.printStackTrace()
        } catch (e: NoSuchMethodException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        } catch (e: IllegalArgumentException) {
            e.printStackTrace()
        } catch (e: InvocationTargetException) {
            e.printStackTrace()
        }
        return result
    }

    operator fun set(key: String?, value: String?) {
        try {
            val c = Class.forName("android.os.SystemProperties")
            val set: Method = c.getMethod("set", String::class.java, String::class.java)
            set.invoke(c, key, value)
        } catch (e: ClassNotFoundException) {
            e.printStackTrace()
        } catch (e: NoSuchMethodException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        } catch (e: IllegalArgumentException) {
            e.printStackTrace()
        } catch (e: InvocationTargetException) {
            e.printStackTrace()
        }
    }
}