package uz.mobiuz.mobiservice.dev.ui.global

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import uz.mobiuz.mobiservice.dev.OfflineActivity
import uz.mobiuz.mobiservice.dev.databinding.LayoutOfflineBottomSheetBinding
import uz.mobiuz.mobiservice.dev.model.SharedPref
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class OfflineBottomSheet: BottomSheetDialogFragment() {

    @Inject
    lateinit var pref: SharedPref
    private var _bn: LayoutOfflineBottomSheetBinding? = null
    private val bn get() = _bn ?: throw NullPointerException("cannot inflate")
    private var lng = ""

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _bn = LayoutOfflineBottomSheetBinding.inflate(inflater)
        return bn.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setUpUI()
    }

    private fun setUpUI() {
        bn.apply {
            lng = pref.language
            btnToOfflineActivity.setOnClickListener(object : ButtonClick() {
                override fun onSingleClick(v: View?) {
                    pref.isShowPin = false
                    navigate()
                }
            })

            btnCancel.setOnClickListener(object : ButtonClick() {
                override fun onSingleClick(v: View?) {
                    dismiss()
                }
            })
        }
    }

    fun navigate() {
        val intent = Intent(requireContext(), OfflineActivity::class.java)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_TASK_ON_HOME
        startActivity(intent)
        requireActivity().finishAffinity()
        dismiss()
    }

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

}