package com.easy.clean.ui.set

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

        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()
    }
}