package com.ritoq.quickphone.ui.set

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

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