package com.abbidot.tracker.ui.activity.set

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.base.BaseDialog
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.ActivityAdvancedSetBinding
import com.abbidot.tracker.ui.activity.subscribe.PaymentMethodActivity
import com.abbidot.tracker.util.FileUtil
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.vm.UserProfileViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class AdvancedSetActivity :
    BaseActivity<ActivityAdvancedSetBinding>(ActivityAdvancedSetBinding::inflate) {

    private val mUserViewModel: UserProfileViewModel by viewModels()

    private val mClearCacheTipsType = 1
    private val mCloseAccountTipsType = 2

    override fun getTopBar() = mViewBinding.ilAdvancedSetTopBar.titleTopBar

    override fun initData() {
        super.initData()
        setTopBarTitle(R.string.txt_advanced_set)
        setLeftBackImage(R.drawable.icon_white_back_svg)

        mViewBinding.apply {
            ilAdvancedSetClearCache.root.let {
                it.text = getString(R.string.txt_setting_clear)
                it.setImageDrawable(
                    ContextCompat.getDrawable(
                        mContext, R.drawable.icon_database_restore_svg
                    )
                )
            }
            ilAdvancedSetPayManager.root.let {
                it.text = getString(R.string.txt_payment_manager)
                it.setImageDrawable(
                    ContextCompat.getDrawable(
                        mContext, R.drawable.card_pay_manager
                    )
                )
            }
            ilAdvancedSetDeleteAccount.root.let {
                it.text = getString(R.string.txt_delete_account)
                it.setImageDrawable(
                    ContextCompat.getDrawable(
                        mContext, R.drawable.ico_account
                    )
                )
            }

            setOnClickListenerViews(
                ilAdvancedSetClearCache.root,
                ilAdvancedSetPayManager.root,
                ilAdvancedSetDeleteAccount.root
            )
        }

        getCacheSize()
    }

    override fun liveDataObserve() {
        mUserViewModel.mUserLogoffLiveData.observe(this) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    Util.logOut(this@AdvancedSetActivity, lifecycleScope)
                }
            })
        }
    }

    private fun dialogShowType(type: Int, string: String) {
        ViewUtil.instance.showDialog(mContext, string, object : BaseDialog.OnDialogOkListener {
            override fun onOkClick(dialog: BaseDialog<*>) {
                dialog.dismiss()
                when (type) {
                    mClearCacheTipsType -> clearCache()
                    mCloseAccountTipsType -> mUserViewModel.logoff(this@AdvancedSetActivity)
                }
            }
        }, okTextResId = R.string.txt_sure)
    }

    /**
     * 清除缓存
     */
    private fun clearCache() {
        lifecycleScope.launch {
            withContext(Dispatchers.IO) {
                FileUtil.clearCache(mActivity)
            }
            getCacheSize()
            showToast(R.string.txt_clear_cache_success)
        }
    }

    /**
     * 获取缓存大小
     */
    private fun getCacheSize() {
        lifecycleScope.launch {
            mViewBinding.ilAdvancedSetClearCache.root.detailText = withContext(Dispatchers.IO) {
                FileUtil.getCacheSize(mActivity)
            }
        }
    }


    override fun onClick(v: View?) {
        mViewBinding.apply {
            when (v!!) {
                ilAdvancedSetClearCache.root -> dialogShowType(
                    mClearCacheTipsType, getString(R.string.txt_clear_cache_tips)
                )

                ilAdvancedSetPayManager.root -> startActivity(
                    Intent(
                        mContext, PaymentMethodActivity::class.java
                    )
                )

                ilAdvancedSetDeleteAccount.root -> dialogShowType(
                    mCloseAccountTipsType, getString(R.string.txt_close_account_tips)
                )
            }
        }
    }
}