package com.tgv.malware.ui.set

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.tgv.malware.BuildConfig
import com.tgv.malware.GlobalConfig.URL_PRIVACY
import com.tgv.malware.SpConstObject.cleanedSize
import com.tgv.malware.base.BaseFragment
import com.tgv.malware.databinding.FragmentSettingsBinding
import com.tgv.malware.utils.KotlinExt.toFormatSize
import androidx.core.net.toUri
import androidx.core.view.updatePadding
import com.tgv.malware.R
import com.tgv.malware.SpConstObject.fcmToken
import com.tgv.malware.bean.config.ConfigBean
import com.tgv.malware.business.ads.AdsMgr
import com.tgv.malware.ui.dialog.RateDialog
import com.tgv.malware.utils.ClipboardUtils.copyText
import com.tgv.malware.utils.ToastUtils.toast
import com.google.gson.GsonBuilder


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

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (requireActivity() is SettingActivity) {
            (requireActivity() as SettingActivity).insetsCallBack = {
                binding.clTop.updatePadding(0, it.top, 0, 0)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    override fun initView() {
        super.initView()

        binding.tvCleanedUp.text = cleanedSize.toFormatSize()
        binding.tvVersion.text = "v${BuildConfig.VERSION_NAME}"
//        MyAnimationUtils.rotation(binding.ivBlade)
        AdsMgr.showNative(binding.flAd, R.layout.layout_admob_native_custom)

        if (BuildConfig.DEBUG) {
            binding.tvToken.visibility = View.VISIBLE
            binding.tvToken.text = fcmToken
            binding.tvToken.setOnClickListener {
                requireContext().copyText("token", fcmToken)
                requireContext().toast("copy token")
            }

            binding.tvSettings.setOnClickListener {
                val configBean = ConfigBean.configBean
                val gson = GsonBuilder().setPrettyPrinting().create()
                val json = gson.toJson(configBean)
                requireContext().copyText("json", json)
                requireContext().toast("copy json")
            }
        }
    }

    override fun initListener() {
        super.initListener()
        binding.clPrivacyPolicy.setOnClickListener {
            startActivity(Intent(Intent.ACTION_VIEW, URL_PRIVACY.toUri()))
        }
        binding.clRateUs.setOnClickListener {
            val dialog = RateDialog(requireActivity())
            dialog.showDialog(true)
        }
        binding.ivBack.setOnClickListener {
            (requireActivity() as SettingActivity).onBackPressedDispatcher.onBackPressed()
        }
    }

    override fun onResume() {
        super.onResume()
        binding.tvCleanedUp.text = cleanedSize.toFormatSize()
    }
}