package com.adealink.weparty.setting

import android.annotation.SuppressLint
import androidx.activity.viewModels
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.log.data.ReportLogReason
import com.adealink.frame.log.data.UserLogType
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.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.module.webview.Web
import com.adealink.weparty.setting.databinding.ActivitySettingBinding
import com.adealink.weparty.setting.language.LanguageSettingDialog
import com.adealink.weparty.setting.viewmodel.SettingViewModel
import com.adealink.weparty.setting.viewmodel.SettingViewModelFactory
import com.adealink.weparty.url.UrlConfig
import com.adealink.weparty.util.applyImmersive
import com.adealink.weparty.R as APP_R

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

    private val binding by viewBinding(ActivitySettingBinding::inflate)

    private val settingViewModel by viewModels<SettingViewModel> { SettingViewModelFactory() }

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

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        applyImmersive { top ->
            binding.topBar.setPadding(0, top, 0, 0)
            binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
                height = top + 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.vUploadLog.onClick {
            uploadLog()
        }

        binding.vDeleteAccount.onClick {
            deleteAccount()
        }

        binding.vLogout.onClick {
            clickLogout()
        }

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

    override fun observeViewModel() {
        super.observeViewModel()
        settingViewModel.newVersionLD.observe(this) {
            binding.tvNewVersion.show(it)
        }
    }

    override fun loadData() {
        super.loadData()
        settingViewModel.checkAppUpdate(this)
        settingViewModel.getCacheSize().observe(this) { totalSize ->
            binding.tvCacheSize.text = when {
                totalSize <= 0 -> {
                    "0MB"
                }

                else -> {
                    "${totalSize / 1024 / 1024}MB"
                }
            }
        }
    }

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

    @SuppressLint("SetTextI18n")
    private fun clearCache() {
        CommonDialog.Builder()
            .title(getCompatString(R.string.setting_clear_cache_confirm_title))
            .showTopClosetButton(false)
            .canceledOnTouchOutside(true)
            .cancelable(true)
            .setShowDefaultCancel(true)
            .onPositive {
                showToast(getCompatString(APP_R.string.common_success))
                settingViewModel.clearCache()
                binding.tvCacheSize.text = "0MB"
            }
            .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")
    }

    private fun deleteAccount() {
        Router.build(this, Web.FullScreen.PATH)
            .putExtra(
                Web.Common.EXTRA_URL,
                UrlConfig.deleteAccount
            )
            .putExtra(Web.Common.EXTRA_SHOW_TOPBAR, true)
            .start()
    }

    private fun uploadLog() {
        showLoading()
        settingViewModel.uploadLog(ReportLogReason.User(UserLogType.SETTING)).observe(this) {
            dismissLoading()
            showToast(it)
        }
    }

}