package com.base.scanqrclear.ui.main

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import androidx.core.view.updatePadding
import com.base.scanqrclear.BuildConfig
import com.base.scanqrclear.GlobalConfig
import com.base.scanqrclear.R
import com.base.scanqrclear.base.BaseFragment
import com.base.scanqrclear.bean.EmailUIBean
import com.base.scanqrclear.databinding.FragmentSettingsBinding
import com.base.scanqrclear.ui.language.LanguageActivity
import com.base.scanqrclear.ui.widget.RateDialog.showRateDialog
import com.base.scanqrclear.utils.BarUtils
import com.base.scanqrclear.utils.IntentUtils
import com.base.scanqrclear.utils.IntentUtils.intentSendEmail
import com.base.scanqrclear.utils.ToastUtils.toast

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

    @SuppressLint("SetTextI18n")
    override fun initView() {
        super.initView()
        binding.flTop.updatePadding(top = BarUtils.getStatusBarHeight())

        binding.tvVersion.text = getString(R.string.version) + BuildConfig.VERSION_NAME
    }

    override fun initListener() {
        super.initListener()

        binding.llFeedback.setOnClickListener {
            val address = "gamexzone.rk@gmail.com"
            val email = EmailUIBean()
            email.address = address
            val intent = intentSendEmail(email)
            try {
                startActivity(intent)
            } catch (e: Exception) {
                requireContext().toast("No application was found to handle the email request", true)
            }
        }
        binding.llPrivacy.setOnClickListener {
            runCatching {
                val intent = Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse(GlobalConfig.URL_PRIVACY)
                )
                requireContext().startActivity(intent)
            }
        }
        binding.llLanguage.setOnClickListener {
            val activity = requireActivity() as MainActivity?
            activity?.startActivity(Intent(activity, LanguageActivity::class.java))
        }
        binding.llRate.setOnClickListener {
            requireContext().showRateDialog(true)
        }
        binding.llShare.setOnClickListener {
            try {
                val uri = "https://play.google.com/store/apps/details?id=" + requireContext().packageName
                val intent = IntentUtils.intentShareText(uri)
                (requireActivity() as MainActivity?)?.startActivity(intent)
            } catch (e: Exception) {
                requireContext().toast("No application was found to handle the share", true)
            }

        }
    }
}