package com.adealink.weparty.setting

import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.adealink.frame.aab.util.getCompatDimension
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.AppBase
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.frame.util.onClick
import com.adealink.frame.util.statusBarHeight
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.toast.util.showToast
import com.adealink.weparty.commonui.widget.CommonDialog
import com.adealink.weparty.debug.Debug
import com.adealink.weparty.module.account.AccountModule
import com.adealink.weparty.module.setting.Setting
import com.adealink.weparty.setting.databinding.ActivitySettingBinding
import com.adealink.weparty.setting.language.LanguageSettingDialog
import com.adealink.weparty.R as APP_R

@RouterUri(
    path = [Setting.Setting.PATH],
    desc = "设置"
)
class SettingActivity : BaseActivity() {

    private val binding by viewBinding(ActivitySettingBinding::inflate)

    override fun onBeforeCreate() {
        super.onBeforeCreate()
        Router.bind(this)
    }

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        val statusBarHeight = statusBarHeight()
        binding.topBar.setPadding(0, statusBarHeight, 0, 0)
        binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
            height = statusBarHeight + getCompatDimension(APP_R.dimen.common_top_bar_height).toInt()
        }

        binding.vLanguage.onClick {
            clickLanguage()
        }
        binding.vClearCache.onClick {
            clearCache()
        }
        binding.tvAbout.text = getCompatString(R.string.setting_about, AppBase.appName)
        binding.vAbout.onClick {
            clickAbout()
        }

        binding.vLogout.onClick {
            clickLogout()
        }

        if (AppBase.isRelease) {
            binding.vDebug.gone()
        } else {
            binding.vDebug.show()
            binding.vDebug.onClick {
                Router.build(this, Debug.Debug.PATH).start()
            }
        }
    }

    private fun clickLanguage() {
        LanguageSettingDialog().show(supportFragmentManager, Setting.Language.PATH)
    }

    private fun clearCache() {
        CommonDialog.Builder()
            .title(getCompatString(R.string.setting_clear_cache_confirm_title))
            .message(getCompatString(R.string.setting_clear_cache_confirm_desc))
            .showTopClosetButton(false)
            .canceledOnTouchOutside(true)
            .cancelable(true)
            .setShowDefaultCancel(true)
            .onPositive {
                showToast(getCompatString(APP_R.string.common_success))
            }
            .show(supportFragmentManager, "ClearCache")
    }

    private fun clickAbout() {
        Router.build(this, Setting.About.PATH).start()
    }

    private fun clickLogout() {
        CommonDialog.Builder()
            .title(getCompatString(R.string.setting_logout_confirm_title))
            .showTopClosetButton(false)
            .canceledOnTouchOutside(true)
            .cancelable(true)
            .setShowDefaultCancel(true)
            .onPositive {
                AccountModule.logout(true)
            }
            .show(supportFragmentManager, "Logout")
    }

}