package com.base.scanqr.ui.main

import android.content.Intent
import android.net.Uri
import androidx.core.view.updatePadding
import com.base.scanqr.BuildConfig
import com.base.scanqr.GlobalConfig
import com.base.scanqr.R
import com.base.scanqr.base.BaseFragment
import com.base.scanqr.bean.EmailUIBean
import com.base.scanqr.databinding.FragmentSettingsBinding
import com.base.scanqr.utils.BarUtils
import com.base.scanqr.utils.IntentUtils.intentSendEmail
import com.base.scanqr.utils.ToastUtils.toast

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

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