package com.base.appzxhy.ui.set

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.base.appzxhy.BuildConfig
import com.base.appzxhy.GlobalConfig.URL_PRIVACY
import com.base.appzxhy.SpConstObject.cleanedSize
import com.base.appzxhy.base.BaseFragment
import com.base.appzxhy.databinding.FragmentSettingsBinding
import com.base.appzxhy.utils.KotlinExt.toFormatSize
import androidx.core.net.toUri
import androidx.core.view.updatePadding
import com.base.appzxhy.R
import com.base.appzxhy.SpConstObject.fcmToken
import com.base.appzxhy.bean.config.ConfigBean
import com.base.appzxhy.business.ads.AdsMgr
import com.base.appzxhy.ui.dialog.RateDialog
import com.base.appzxhy.utils.ClipboardUtils.copyText
import com.base.appzxhy.utils.MyAnimationUtils
import com.base.appzxhy.utils.ToastUtils.toast
import com.google.gson.Gson
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.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)
        }

    }

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