package com.base.scanqrclear.ui.main

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.view.View
import androidx.core.view.updatePadding
import com.base.scanqrclear.BuildConfig
import com.base.scanqrclear.GlobalConfig
import com.base.scanqrclear.R
import com.base.scanqrclear.ads.AdsMgr
import com.base.scanqrclear.base.BaseFragment
import com.base.scanqrclear.bean.EmailUIBean
import com.base.scanqrclear.bean.config.AdConfigBean.Companion.adsConfigBean
import com.base.scanqrclear.bean.config.ConfigBean
import com.base.scanqrclear.databinding.FragmentSettingsBinding
import com.base.scanqrclear.ui.language.LanguageActivity
import com.base.scanqrclear.ui.widget.LogDialog.showLogDialog
import com.base.scanqrclear.ui.widget.RateDialog.showRateDialog
import com.base.scanqrclear.utils.AppPreferences
import com.base.scanqrclear.utils.BarUtils
import com.base.scanqrclear.utils.ClickTimesUtils.isContinuousTimesClick
import com.base.scanqrclear.utils.IntentUtils
import com.base.scanqrclear.utils.IntentUtils.intentSendEmail
import com.base.scanqrclear.utils.ToastUtils.toast
import com.google.gson.Gson

class SettingsFragment : BaseFragment<FragmentSettingsBinding>(FragmentSettingsBinding::inflate) {

    @SuppressLint("SetTextI18n")
    override fun initView() {
        super.initView()
        binding.flTop.updatePadding(top = BarUtils.getStatusBarHeight())

        binding.tvVersion.text = getString(R.string.version) + BuildConfig.VERSION_NAME

        if (adsConfigBean.setShowNative) {
            AdsMgr.showNative(binding.flAd, R.layout.layout_admob_item)
        } else {
            binding.flAd.visibility = View.GONE
        }
    }

    override fun initListener() {
        super.initListener()

        binding.llFeedback.setOnClickListener {
            val address = "gamexzone.rk@gmail.com"
            val email = EmailUIBean()
            email.address = address
            val intent = intentSendEmail(email)
            try {
                startActivity(intent)
            } catch (e: Exception) {
                requireContext().toast("No application was found to handle the email request", true)
            }
        }
        binding.llPrivacy.setOnClickListener {
            runCatching {
                val intent = Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse(GlobalConfig.URL_PRIVACY)
                )
                requireContext().startActivity(intent)
            }
        }
        binding.llLanguage.setOnClickListener {
            val activity = requireActivity() as MainActivity?
            activity?.startActivity(Intent(activity, LanguageActivity::class.java))
        }
        binding.llRate.setOnClickListener {
            requireContext().showRateDialog(true)
        }
        binding.llShare.setOnClickListener {
            try {
                val uri = "https://play.google.com/store/apps/details?id=" + requireContext().packageName
                val intent = IntentUtils.intentShareText(uri)
                (requireActivity() as MainActivity?)?.startActivity(intent)
            } catch (e: Exception) {
                requireContext().toast("No application was found to handle the share", true)
            }

        }
        binding.llApp.setOnClickListener {
            if (isContinuousTimesClick()) {
                val sb = StringBuilder()
                val gson = Gson()
                sb.append("isInBlackList:").append(ConfigBean.configBean.isInBlackList).append("\n")
                sb.append("ut:${ConfigBean.configBean.ut}").append("\n")
                val adLog = gson.toJson(ConfigBean.configBean.adConfigBean).replace(",", ",\n")
                sb.append(adLog).append("\n")
                val popupLog =
                    gson.toJson(ConfigBean.configBean.popupConfigBean).replace(",", ",\n")
                sb.append(popupLog).append("\n")
                sb.append("uuid:").append(AppPreferences.getInstance().getString("uuid", "null"))
                requireContext().showLogDialog(sb.toString())
            }
        }
    }
}