package uz.mobiuz.mobiservice.dev

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import uz.mobiuz.mobiservice.dev.databinding.ActivityOfflineBinding
import uz.mobiuz.mobiservice.dev.model.SharedPref
import uz.mobiuz.mobiservice.dev.ui.auth.AuthActivity
import uz.mobiuz.mobiservice.dev.ui.global.CONSTANTS
import uz.mobiuz.mobiservice.dev.ui.service.ServicesAdapter
import uz.mobiuz.mobiservice.dev.utils.LocaleHelper
import uz.mobiuz.mobiservice.dev.utils.extensions.showMessage
import dagger.hilt.android.AndroidEntryPoint
import uz.mobiuz.mobiservice.dev.utils.NetworkUtil
import javax.inject.Inject


@AndroidEntryPoint
class OfflineActivity : AppCompatActivity() {

    private var _bn: ActivityOfflineBinding? = null
    private val bn get() = _bn ?: throw NullPointerException("cannot inflate")
    private lateinit var navController: NavController
    private lateinit var receiver: BroadcastReceiver
    private val intentFilter = IntentFilter()

    // if offline type == 0
    private var type = 0

    @Inject
    lateinit var pref: SharedPref

    override fun onCreate(savedInstanceState: Bundle?) {
        LocaleHelper.setLocale(this)
        super.onCreate(savedInstanceState)
        _bn = ActivityOfflineBinding.inflate(layoutInflater)
        setContentView(bn.root)

        val fragment = supportFragmentManager.findFragmentById(R.id.offline_container) as NavHostFragment
        navController = NavHostFragment.findNavController(fragment)
        navController.popBackStack()
        navController.navigate(R.id.serviceFragment, bundleOf(CONSTANTS.TYPE_SERVICE to CONSTANTS.OFFLINE))

        receiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context?, intent: Intent?) {
                if (intent?.action == CONSTANTS.CONNECTIVITY_CHANGE) {
                    type = NetworkUtil.getConnectivityStatus(this@OfflineActivity)
                    bn.btnTryAgain.isVisible = (type != 0)
                    if (type != 0) {
                        showMessage(getString(R.string.yes_internet_connect))
                    }
                }
            }
        }

        intentFilter.addAction(CONSTANTS.CONNECTIVITY_CHANGE)
        registerReceiver(receiver, intentFilter)

        bn.btnTryAgain.setOnClickListener {
            if (type != 0) {
                navigate()
            }
        }

    }

    fun navigate() {
        when {
            !pref.isRegistered -> {
                startActivity(Intent(this, AuthActivity::class.java))
            }

            pref.pinCode.isEmpty() -> {
                startActivity(Intent(this, AuthActivity::class.java).putExtra(CONSTANTS.TYPE_AUTH, CONSTANTS.LOGIN))
            }

            else -> {
                val intent = Intent(this, MainActivity::class.java)
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_TASK_ON_HOME
                startActivity(intent)
            }
        }
        finish()
    }


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