package uz.mobiuz.mobiservice.dev

import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import uz.mobiuz.mobiservice.dev.databinding.ActivitySplashBinding
import uz.mobiuz.mobiservice.dev.model.SharedPref
import uz.mobiuz.mobiservice.dev.network.model.UiStateObject
import uz.mobiuz.mobiservice.dev.ui.auth.AuthActivity
import uz.mobiuz.mobiservice.dev.ui.global.ButtonClick
import uz.mobiuz.mobiservice.dev.ui.global.CONSTANTS
import uz.mobiuz.mobiservice.dev.utils.extensions.showMessage
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import uz.mobiuz.mobiservice.dev.utils.NetworkUtil
import javax.inject.Inject

@SuppressLint("CustomSplashScreen")
@AndroidEntryPoint
class SplashActivity : AppCompatActivity() {

    private var _bn: ActivitySplashBinding? = null
    private val bn get() = _bn ?: throw NullPointerException("cannot inflate")
    private val viewModel: MainViewModel by viewModels()

    @Inject
    lateinit var pref: SharedPref
    private lateinit var receiver: BroadcastReceiver
    private val intentFilter = IntentFilter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _bn = ActivitySplashBinding.inflate(layoutInflater)
        setContentView(bn.root)

        window.decorView.systemUiVisibility = View.VISIBLE

        receiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context?, intent: Intent?) {
                val type = NetworkUtil.getConnectivityStatus(this@SplashActivity)
                if (type == 0) {
                    if (pref.services.isNotEmpty()) {
                        lifecycleScope.launchWhenCreated {
                            delay(1500)
                            startActivity(Intent(this@SplashActivity, OfflineActivity::class.java))
                            finish()
                        }
                    } else {
                        bn.btnTryAgain.isVisible = true
                    }
                } else {
                    viewModel.services()
                }
            }
        }
        intentFilter.addAction(CONSTANTS.CONNECTIVITY_CHANGE)
        registerReceiver(receiver, intentFilter)


        bn.btnTryAgain.setOnClickListener(object : ButtonClick() {
            override fun onSingleClick(v: View?) {
                viewModel.services()
            }
        })
        lifecycleScope.launchWhenStarted {
            viewModel.servicesUiState.collect {
                when (it) {
                    is UiStateObject.SUCCESS -> {
                        bn.btnTryAgain.isVisible = false
                        delay(800)
                        navigate()
                    }
                    is UiStateObject.ERROR -> {
                        bn.btnTryAgain.isVisible = true
                        showMessage(it.message)
                    }
                    is UiStateObject.LOADING -> {
                        bn.btnTryAgain.isVisible = false
                    }
                    else -> Unit
                }
            }
        }

    }


    fun navigate() {
        when {

            pref.language.isEmpty() -> {
                startActivity(Intent(this@SplashActivity, LanguageActivity::class.java))
            }

            !pref.isRegistered -> {
                startActivity(Intent(this@SplashActivity, AuthActivity::class.java))
            }

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

            else -> {
                startActivity(Intent(this@SplashActivity, LanguageActivity::class.java))
            }
        }
        finish()
    }

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