package com.dumpster.cleaner.ui.set

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