package com.gh.base

import android.view.Gravity
import androidx.appcompat.app.AppCompatActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.DarkModeUtils
import com.gh.gamecenter.common.utils.DialogHelper
import com.gh.gamecenter.common.utils.PackageFlavorHelper
import com.gh.gamecenter.common.utils.dip2px
import com.lzf.easyfloat.EasyFloat
import com.lzf.easyfloat.enums.ShowPattern
import com.lzf.easyfloat.enums.SidePattern

object DarkModeSwitchHelper {

    fun showDarkModeSwitchFloatingView(activity: AppCompatActivity) {
        if (PackageFlavorHelper.IS_TEST_FLAVOR) {
            EasyFloat.with(activity)
                .setLayout(R.layout.layout_day_mode_night_mode_switch)
                .setTag("night_mode_switch_floating_view")
                .setAnimator(null)
                .setGravity(Gravity.TOP.xor(Gravity.END), 0, 114F.dip2px())
                .setSidePattern(SidePattern.RESULT_SIDE)
                .setDragEnable(true)
                .setShowPattern(ShowPattern.CURRENT_ACTIVITY)
                .registerCallback {
                    createResult { _, _, view ->
                        view?.setOnClickListener {
                            showDarkModeSwitchDialog(activity)
                        }
                    }
                }
                .show()
        }
    }

    fun dismissDarkModeSwitchFloatingView() {
        if (PackageFlavorHelper.IS_TEST_FLAVOR) {
            EasyFloat.dismiss("night_mode_switch_floating_view")
        }
    }

    private fun showDarkModeSwitchDialog(activity: AppCompatActivity) {
        if (PackageFlavorHelper.IS_TEST_FLAVOR) {
            //切换深色模式
            var mode = "普通模式"
            var positive = "跟随系统模式"
            var negative = "深色模式"
            if (DarkModeUtils.isFollowSystemDarkModeFromSp()) {
                mode = "跟随系统模式"
                positive = "普通模式"
                negative = "深色模式"
            } else if (DarkModeUtils.isAppDarkModeEnabledFromSp()) {
                mode = "深色模式"
                positive = "跟随系统模式"
                negative = "普通模式"
            }
            DialogHelper.showDialog(
                context = activity,
                title = "选择模式",
                content = "当前为 $mode",
                confirmText = positive,
                cancelText = negative,
                confirmClickCallback = {
                    if (DarkModeUtils.isFollowSystemDarkModeFromSp()) {
                        DarkModeUtils.updateAppDarkModeStatusToSp(false)
                        DarkModeUtils.updateFollowSystemDarkModeToSp(false)
                    } else {
                        DarkModeUtils.updateFollowSystemDarkModeToSp(true)
                    }
                    DarkModeUtils.initDarkMode()
                },
                cancelClickCallback = {
                    if (DarkModeUtils.isFollowSystemDarkModeFromSp()) {
                        DarkModeUtils.updateAppDarkModeStatusToSp(true)
                    } else {
                        DarkModeUtils.updateAppDarkModeStatusToSp(!DarkModeUtils.isAppDarkModeEnabledFromSp())
                    }
                    DarkModeUtils.updateFollowSystemDarkModeToSp(false)
                    DarkModeUtils.initDarkMode()
                },
                extraConfig = DialogHelper.Config(centerTitle = true, centerContent = true)
            )
        }
    }
}