package com.develop.setting.init.ui


import android.content.Context
import android.os.Bundle
import android.os.PowerManager
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.LanguageUtils
import com.develop.food.base.common.*
import com.develop.food.base.data.DataFactory
import com.develop.food.base.event.FinishAtyEvent
import com.develop.food.base.router.Screens
import com.develop.food.base.router.navigateTo
import com.develop.food.base.ui.BaseActivity
import com.develop.food.base.utils.MmkvUtils
import com.develop.food.base.utils.ResDimension
import com.develop.food.base.widgets.CancelConfirmDialog
import com.develop.setting.R
import com.develop.setting.databinding.ActivityLanguageBinding
import com.develop.setting.init.adapter.LanguageAdapter
import com.kuyuntech.cofarcooking.device.sdk.constant.core.DevStatus
import com.kuyuntech.cofarcooking.device.sdk.util.core.CofarSDK
import org.greenrobot.eventbus.Subscribe
import java.util.*

@Route(path = Screens.Setting.LANGUAGE)
class LanguageActivity : BaseActivity<ActivityLanguageBinding>() {
    private var languageAdapter = LanguageAdapter()
    private var selectPosition = -1
    override fun createViewBinding(inflater: LayoutInflater): ActivityLanguageBinding {
        return ActivityLanguageBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val isFromSetting = intent.extras?.getBoolean(LANGUAGE_FROM_SETTING) ?: false
        if (isFromSetting) {
            binding.ivBanner.visibility = View.GONE
            binding.topBar.visibility = View.VISIBLE
            binding.tvLanguage.visibility = View.GONE
            binding.topBar.setTopBarTitle(getString(R.string.Language))
            val params = binding.languageRecycle.layoutParams as RelativeLayout.LayoutParams
            params.topMargin = ResDimension(R.dimen.convert_251px).toInt()
            binding.languageRecycle.layoutParams = params
            binding.topBar.listenBackClick {
                finish()
            }
        }
        languageAdapter.setList(DataFactory.createLanguageData(resources))
        binding.languageRecycle.apply {
            adapter = languageAdapter
            selectPosition = getSelectedPos()
        }

        languageAdapter.setOnItemClickListener { adapter, view, position ->
            languageAdapter.updateSelectedPosition(position)
            this.selectPosition = position
//            updateLanguage()
        }


        if (isFromSetting) {
            binding.tvNext.setText(getString(R.string.confirm))
        }

        binding.tvNext.setOnClickListener {


            if (CofarSDK.devInfo().status == DevStatus.RUNNING.toInt()) {
                var dailog = CancelConfirmDialog()
                dailog.title = getString(R.string.change_lang_tips)
                dailog.showCancel = false;

                dailog.onDialogClickListener = object : CancelConfirmDialog.OnDialogClickListener {
                    override fun onConfirm() {

                    }

                    override fun onCancel() {
                    }

                }
                dailog.showDialog(supportFragmentManager, "confirm")
                return@setOnClickListener
            }




            if (isFromSetting) {

                if(languageAdapter.itemCount <= 1){
                    finish()
                    return@setOnClickListener
                }

                var dailog = CancelConfirmDialog()
                dailog.title = getString(R.string.change_lang_tips2)

                dailog.onDialogClickListener = object : CancelConfirmDialog.OnDialogClickListener {
                    override fun onConfirm() {

                        updateLanguage()
//                        finish()
//                        navigateTo(Screens.Main.ENTRANCE_CHOSEN)
                        val pm = getSystemService(Context.POWER_SERVICE) as PowerManager
                        pm.reboot("")
                    }

                    override fun onCancel() {
                    }

                }

                dailog.showDialog(supportFragmentManager, "tips2")


            } else {
                updateLanguage()
                navigateTo(Screens.Setting.POLICY)
            }


        }
    }

    private fun updateLanguage() {
        when (selectPosition) {
            0, -1 -> {
                MmkvUtils.save(CURRENT_LANGUAGE, EN)
                LanguageUtils.applyLanguage(Locale.ENGLISH)
            }
            1 -> {
                MmkvUtils.save(CURRENT_LANGUAGE, ZH)
                LanguageUtils.applyLanguage(Locale.CHINESE)
            }
            2 -> {
                MmkvUtils.save(CURRENT_LANGUAGE, FR)
                LanguageUtils.applyLanguage(Locale.FRANCE)
            }
            3 -> {
                MmkvUtils.save(CURRENT_LANGUAGE, JA)
                LanguageUtils.applyLanguage(Locale.JAPAN)
            }
        }
    }

    private fun getSelectedPos(): Int {
        return when (MmkvUtils.getString(CURRENT_LANGUAGE)) {
            EN -> {
                languageAdapter.updateSelectedPosition(0)
                0
            }
            ZH -> {
                languageAdapter.updateSelectedPosition(1)
                1
            }

            FR -> {
                languageAdapter.updateSelectedPosition(2)
                2
            }

            JA -> {
                languageAdapter.updateSelectedPosition(3)
                3
            }
            else -> {
                languageAdapter.updateSelectedPosition(0)
                0
            }
        }
    }

    @Subscribe
    fun onFinishAtyEvent(event: FinishAtyEvent) {
        finish()
    }

}