package com.base.locationsharewhite.ui.set

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import com.base.locationsharewhite.R
import com.base.locationsharewhite.databinding.ActivitySettingBinding
import com.base.locationsharewhite.helper.BaseActivity
import com.base.locationsharewhite.helper.MyApplication
import com.base.locationsharewhite.helper.config.AppConfig
import com.base.locationsharewhite.location.LocationLoginUtils
import com.base.locationsharewhite.ui.views.RateDialog.showRateDialog
import com.base.locationsharewhite.utils.BarUtils

class SettingActivity : BaseActivity<ActivitySettingBinding>() {
    private val TAG = "SettingActivity"

    override val binding: ActivitySettingBinding by lazy {
        ActivitySettingBinding.inflate(layoutInflater)
    }

    @SuppressLint("SetTextI18n")
    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.WHITE)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())

        binding.tvHelloName.text = getString(R.string.hello) + ",\n" + LocationLoginUtils.myNickNameSp
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            finish()
        }
        binding.flFanhui.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.llFeedback.setOnClickListener {
            startActivity(Intent(this, FeedbackActivity::class.java))
        }
        binding.llLanguage.setOnClickListener {
            startActivity(Intent(this, LanguageActivity::class.java))
        }
        binding.llRateUse.setOnClickListener {
            showRateDialog(true)
        }
        binding.llRename.setOnClickListener {
            startActivity(Intent(this, RenameActivity::class.java))
        }
        binding.llPrivacyPolicy.setOnClickListener {
            val intent = Intent(
                Intent.ACTION_VIEW,
                Uri.parse(AppConfig.privacyPolicy)
            )
            startActivity(intent)
        }
    }

    override fun onResume() {
        super.onResume()
        val isRecreate = updateAppLanguage(MyApplication.settingsLanguage, TAG) {
            MyApplication.settingsLanguage = it
        }
        if (isRecreate) {
            return
        }
        LocationLoginUtils.login()
        LocationLoginUtils.loginCallBack = {
            runOnUiThread {
                binding.tvHelloName.text = getString(R.string.hello) + ",\n" + LocationLoginUtils.myNickNameSp
            }
        }
    }

}