package com.develop.setting.ui

import android.content.ContentResolver
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.provider.Settings.System.SCREEN_OFF_TIMEOUT
import android.util.Log
import android.view.LayoutInflater
import android.widget.SeekBar
import androidx.annotation.RequiresApi
import com.alibaba.android.arouter.facade.annotation.Route
import com.develop.base.ext.updateText
import com.develop.base.util.MMkvUtils
import com.develop.common.router.Screens
import com.develop.common.tag.SCREENSAVER
import com.develop.common.ui.CommonBindingActivity
import com.develop.setting.R
import com.develop.setting.databinding.ActivityBirghtnessBinding
import com.kuyuntech.cofarcooking.device.sdk.util.core.CofarSDK


@Route(path = Screens.Setting.BRIGHTNESS)
class BrightnessActivity : CommonBindingActivity<ActivityBirghtnessBinding>() {
    override fun createViewBinding(inflater: LayoutInflater): ActivityBirghtnessBinding {
        return ActivityBirghtnessBinding.inflate(layoutInflater)
    }

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreate(savedInstanceState: Bundle?) {

        Log.i("screen", Settings.System.getInt(contentResolver, SCREEN_OFF_TIMEOUT).toString())

        super.onCreate(savedInstanceState)
        binding.tvTitle.updateText(getString(com.develop.common.R.string.Brightness))
        binding.ivClose.setOnClickListener {
            finish()
        }

        setScreenManualMode()
        binding.sbBrightness.max = 127
        binding.sbBrightness.progress = getScreenBrightness()
        binding.sbBrightness.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
                saveScreenBrightness(p1)
            }

            override fun onStartTrackingTouch(p0: SeekBar?) {

            }

            override fun onStopTrackingTouch(p0: SeekBar?) {
                if (p0 != null) {
                    CofarSDK.configBrightness((128 - p0.progress))
                }

            }

        })

        //回显设置值
        val min = (Settings.System.getInt(contentResolver, SCREEN_OFF_TIMEOUT) / 1000 / 60)
        binding.rgLockTime.check(R.id.rb_3_min)
        if (min == 10) {
            binding.rgLockTime.check(R.id.rb_10_min)
        }
        if (min == 30) {
            binding.rgLockTime.check(R.id.rb_30_min)
        }

        binding.rgLockTime.setOnCheckedChangeListener { p0, p1 ->
            var minute = 3
            when (p1) {
                R.id.rb_3_min -> {
                    minute = 3
                }
                R.id.rb_10_min -> {
                    minute = 5
                }

                R.id.rb_30_min -> {
                    minute = 10
                }
            }
            setScreenSaverEnable(minute)
        }
    }

    private fun setScreenManualMode() {
        val contentResolver: ContentResolver = contentResolver
        try {
            val mode: Int = Settings.System.getInt(
                contentResolver,
                Settings.System.SCREEN_BRIGHTNESS_MODE
            )
            if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
                Settings.System.putInt(
                    contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE,
                    Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
                )
            }
        } catch (e: Settings.SettingNotFoundException) {
            e.printStackTrace()
        }
    }

    private fun saveScreenBrightness(value: Int) {

        Settings.System.putInt(
            contentResolver,
            Settings.System.SCREEN_BRIGHTNESS, value
        )
    }

    private fun getScreenBrightness(): Int {
        val defVal = 128
        return Settings.System.getInt(
            contentResolver,
            Settings.System.SCREEN_BRIGHTNESS, defVal
        )
    }

    private fun setScreenSaverEnable(minute: Int) {
        Settings.System.putInt(contentResolver, SCREEN_OFF_TIMEOUT, 1000 * 60 * minute)
        val uri: Uri = Settings.System
            .getUriFor(SCREEN_OFF_TIMEOUT)
        contentResolver.notifyChange(uri, null)
        MMkvUtils.save(SCREENSAVER, minute)
    }


}