package com.simplecleaner.app.ui.set

import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import androidx.core.graphics.Insets
import com.simplecleaner.app.BuildConfig
import com.simplecleaner.app.GlobalConfig.URL_PRIVACY
import com.simplecleaner.app.SpConstObject.cleanedSize
import com.simplecleaner.app.base.BaseFragment
import com.simplecleaner.app.databinding.FragmentSettingsBinding
import com.simplecleaner.app.utils.KotlinExt.toFormatSize
import androidx.core.net.toUri
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.simplecleaner.app.R
import com.simplecleaner.app.SpConstObject.fcmToken
import com.simplecleaner.app.bean.config.ConfigBean
import com.simplecleaner.app.business.ads.AdsMgr
import com.simplecleaner.app.ui.dialog.RateDialog
import com.simplecleaner.app.utils.BarUtils
import com.simplecleaner.app.utils.ClipboardUtils.copyText
import com.simplecleaner.app.utils.ToastUtils.toast
import com.google.gson.GsonBuilder
import com.simplecleaner.app.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()
    }
}