package com.amz.genie.activities

import android.Manifest
import android.app.ActivityOptions
import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toDrawable
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import com.airbnb.lottie.LottieAnimationView
import com.amz.genie.R

open class BaseActivity: AppCompatActivity() {

    private var showLoadingDialog: Dialog? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (showLoadingDialog == null) {
            showLoadingDialog = Dialog(this)
            showLoadingDialog!!.window!!.setBackgroundDrawable(0.toDrawable())
            showLoadingDialog!!.setContentView(R.layout.loading_dialog)
        }
    }

    fun loadFragment(fragment: Fragment) {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.frameLayout_Container, fragment)
        transaction.commit()
    }

    fun showProgressDialog(show: Boolean, type: Int = 1) {
        when {
            show -> {
                if (!isFinishing) {

                    val lottie = showLoadingDialog
                        ?.findViewById<LottieAnimationView>(R.id.lottieLoading)

                    val fileName = when (type) {
                        1 -> "lottie/loader_circle.json"
                        2 -> "lottie/loader_send.json"
                        else -> "lottie/loading.json"
                    }

                    lottie?.apply {
                        setAnimation(fileName)
                        repeatCount = com.airbnb.lottie.LottieDrawable.INFINITE
                        playAnimation()
                    }

                    showLoadingDialog?.setCanceledOnTouchOutside(false)
                    if (showLoadingDialog?.isShowing != true) {
                        showLoadingDialog?.show()
                    }
                }
            }
            else -> {
                try {
                    if (showLoadingDialog?.isShowing == true && !isFinishing) {
                        showLoadingDialog?.dismiss()
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }
    }

    fun navigateTo(target: Class<*>, enterAnim: Int, exitAnim: Int) {
        startActivity(Intent(this@BaseActivity, target),
            ActivityOptions.makeCustomAnimation(this@BaseActivity,
                enterAnim, exitAnim).toBundle())
    }

    fun arePushNotificationsEnabled(context: Context): Boolean {
        val enabledByApp = NotificationManagerCompat.from(context).areNotificationsEnabled()

        if (!enabledByApp) return false

        // Android 13+ also needs POST_NOTIFICATIONS permission
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            ContextCompat.checkSelfPermission(
                context,
                Manifest.permission.POST_NOTIFICATIONS
            ) == PackageManager.PERMISSION_GRANTED
        } else {
            true
        }
    }

    fun setSearchClick(listener: View.OnClickListener?, ibSearch: ImageView) {
        ibSearch.setOnClickListener(listener)
    }

    fun handleBackPress(stat: Int = 0) {
        if (stat == 5) {
            navigateTo(LoginActivity::class.java, R.anim.right_in, R.anim.left_out)
        }

        finish()
        overridePendingTransition(R.anim.left_in, R.anim.right_out)
    }


}