package com.mobiuz.app.dev.ui.auth.biometric

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import com.mobiuz.app.R
import com.mobiuz.app.databinding.FragmentBiometricBinding
import com.mobiuz.app.dev.MainActivity
import com.mobiuz.app.dev.model.SharedPref
import com.mobiuz.app.dev.ui.base.BaseFragment
import com.mobiuz.app.dev.ui.global.ButtonClick
import com.mobiuz.app.dev.ui.global.CONSTANTS
import com.mobiuz.app.dev.utils.extensions.getColorCompat
import com.mobiuz.app.dev.utils.extensions.showMessage
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class BiometricFragment : BaseFragment(R.layout.fragment_biometric) {

    @Inject
    lateinit var pref: SharedPref

    private var _bn: FragmentBiometricBinding? = null
    private val bn get() = _bn ?: throw NullPointerException("cannot inflate")
    private val navController: NavController by lazy(LazyThreadSafetyMode.NONE) { NavHostFragment.findNavController(this) }
    var phone = ""


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        _bn = FragmentBiometricBinding.bind(view)

        setUpUI()
        collects()

    }

    override fun setUpUI() {
        bn.apply {

            btnBack.setOnClickListener(object : ButtonClick() {
                override fun onSingleClick(v: View?) {
                    navController.navigateUp()
                }
            })

            txtSkip.setOnClickListener(object : ButtonClick() {
                override fun onSingleClick(v: View?) {
                    pref.isUseFingerPrint = false
                    navigate()
                }
            })

            btnNext.setOnClickListener(object : ButtonClick() {
                override fun onSingleClick(v: View?) {
                    pref.isUseFingerPrint = true
                    navigate()
                }
            })

            btnSwitch.setOnCheckedChangeListener { _, isChecked ->
                val message = if(isChecked) getString(R.string.biometric_enabled) else getString(R.string.biometric_disabled)
                showToastMessage(message)

                btnNext.setTextColor(if(isChecked) getColorCompat(R.color.primary100) else getColorCompat(R.color.primary70))
                btnNext.isEnabled = isChecked
                pref.isUseFingerPrint = isChecked
            }
        }
    }

    private fun navigate() {
        val intent = Intent(requireContext(), MainActivity::class.java)
        intent.putExtra(CONSTANTS.FIRST, CONSTANTS.FIRST)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_TASK_ON_HOME
        startActivity(intent)
        requireActivity().finishAffinity()
    }

    override fun collects() {}

    override fun onDestroy() {
        _bn = null
        super.onDestroy()
    }
}