package com.rgt.superr.ui.set

import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import androidx.core.graphics.Insets
import com.rgt.superr.BuildConfig
import com.rgt.superr.GlobalConfig.URL_PRIVACY
import com.rgt.superr.SpConstObject.cleanedSize
import com.rgt.superr.base.BaseFragment
import com.rgt.superr.databinding.FragmentSettingsBinding
import com.rgt.superr.utils.KotlinExt.toFormatSize
import androidx.core.net.toUri
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.rgt.superr.R
import com.rgt.superr.SpConstObject.fcmToken
import com.rgt.superr.bean.config.ConfigBean
import com.rgt.superr.business.ads.AdsMgr
import com.rgt.superr.ui.dialog.RateDialog
import com.rgt.superr.utils.BarUtils
import com.rgt.superr.utils.ClipboardUtils.copyText
import com.rgt.superr.utils.ToastUtils.toast
import com.google.gson.GsonBuilder
import com.rgt.superr.utils.WriteLogEx.writeLogFile
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


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


    @SuppressLint("SetTextI18n")
    override fun initView() {
        super.initView()
        binding.clTop.setPadding(0, BarUtils.getStatusBarHeight(), 0, 0)
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
            val systemBars: Insets = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            binding.clTop.setPadding(0, systemBars.top, 0, 0)
            insets
        }
        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)

            if (BuildConfig.DEBUG) {
                requireContext().copyText("json", json)
                requireContext().toast("copy json")
            }

            lifecycleScope.launch(Dispatchers.IO) {
                writeLogFile("json", 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()
    }
}