package com.dlc.exam.ui.settings

import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.provider.Settings
import android.view.LayoutInflater
import com.bumptech.glide.Glide
import com.dlc.exam.common.CommonUtils
import com.dlc.exam.common.Constants
import com.dlc.exam.databinding.ActivitySettingsBinding
import com.dlc.exam.sp.EnvConfig
import com.dlc.exam.sp.EnvSp
import com.dlc.exam.ui.SplashActivity
import com.rc.core.ui.ActivityCollector
import com.rc.core.ui.activity.RcBaseActivity
import com.rc.core.util.DeviceUtils
import com.rc.httpcore.HttpConfig
import java.lang.ref.WeakReference
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale

/**
 * info
 *
 * @author ReiChin_
 */
class SettingsActivity : RcBaseActivity<ActivitySettingsBinding>() {
    private val weakHandler by lazy { WeakReferenceHandler(this) }
    override fun createViewBinding() = ActivitySettingsBinding.inflate(LayoutInflater.from(this))

    override fun initListener() {
        viewBinding.back.setOnClickListener {
            ActivityCollector.finishAll()
            val intent = Intent(this, SplashActivity::class.java)
            startActivity(intent)
        }

        viewBinding.versionInfoLayout.setOnLongClickListener {
            startActivity(Intent(Settings.ACTION_SETTINGS))
            finish()
            true
        }

        viewBinding.confirm.setOnClickListener { changeEen() }
    }

    private fun changeEen() {
        val inputEnv = viewBinding.curEnvInput.text.toString().trim()
        if (inputEnv.isEmpty()) {
            showToast("服务器新环境不能为空！")
            return
        }

        val fileBrowserEnv = viewBinding.fileBrowserInput.text.toString().trim()
        if (fileBrowserEnv.isEmpty()) {
            showToast("文件浏览环境不能为空！")
            return
        }

        val mqttInputEnv = viewBinding.curMqttEnvInput.text.toString().trim()
        if (mqttInputEnv.isEmpty()) {
            showToast("MQTT新环境不能为空！")
            return
        }

        val mqttInputName = viewBinding.curMqttNameInput.text.toString().trim()
        if (mqttInputName.isEmpty()) {
            showToast("MQTT账号不能为空！")
            return
        }

        val mqttInputPwd = viewBinding.curMqttPwdInput.text.toString().trim()
        if (mqttInputPwd.isEmpty()) {
            showToast("MQTT密码不能为空！")
            return
        }

        val settingPwd = viewBinding.curSettingPwdInput.text.toString().trim()
        if (settingPwd.isEmpty()) {
            showToast("工程模式密码不能为空！")
            return
        }

        val newEnv = if (!inputEnv.endsWith("/")) "${inputEnv}/" else inputEnv
        val newFileBrowserEnv =
            if (!fileBrowserEnv.endsWith("/")) "${fileBrowserEnv}/" else fileBrowserEnv

        val envConfig = EnvConfig(
            newEnv,
            newFileBrowserEnv,
            mqttInputEnv,
            mqttInputName,
            mqttInputPwd,
            settingPwd
        )
        EnvSp.saveEnv(this, envConfig)
       weakHandler.sendEmptyMessageDelayed(1, 1000)
//        object : Handler(Looper.getMainLooper()) {
//            override fun handleMessage(msg: Message) {
//                showToast("设置成功")
//                CommonUtils.restartApp(applicationContext)
//            }
//        }.sendEmptyMessageDelayed(1, 1000)
    }

    override fun initData() {
        viewBinding.androidId.text = CommonUtils.getAndroidId(this)
        viewBinding.versionName.text = DeviceUtils.getVersionName(this)
        viewBinding.curEnv.text = HttpConfig.API_BASE_URL
        viewBinding.fileBrowserEnv.text = HttpConfig.FILE_BROWSER_BASE_URL

        viewBinding.curMqttEnv.text = Constants.MqttConfig.SERVER_URI
        viewBinding.curMqttName.text = Constants.MqttConfig.U_NAME
        viewBinding.curMqttPwd.text = Constants.MqttConfig.U_PWD

        viewBinding.curSettingPwd.text =
            EnvSp.loadEnv(this)?.settingPwd ?: Constants.Mock.SETTING_PWD
    }


    // static + 弱引用  防止内存泄露
    class WeakReferenceHandler(obj: SettingsActivity) : Handler(Looper.getMainLooper()) {
        private val mRef: WeakReference<SettingsActivity> = WeakReference(obj)
        override fun handleMessage(msg: Message) {
            mRef.get()?.run {
                showToast("设置成功")
                CommonUtils.restartApp(this)
            }
        }
    }


    override fun onDestroy() {
        weakHandler.removeCallbacksAndMessages(null)
        super.onDestroy()
    }

}