package com.base.scanqrclear.luma

import android.animation.ObjectAnimator
import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.animation.LinearInterpolator
import com.base.scanqrclear.R
import com.base.scanqrclear.databinding.DialogDeleteBinding
import com.base.scanqrclear.databinding.DialogExitBinding
import com.base.scanqrclear.databinding.DialogLoadingBinding
import com.base.scanqrclear.databinding.DialogLogoutBinding
import com.base.scanqrclear.databinding.DialogNotificationBinding
import com.base.scanqrclear.databinding.DialogPermissionBinding
import com.base.scanqrclear.databinding.DialogScoreBinding
import com.base.scanqrclear.luma.AdmobHelps.KEY_SCORE
import com.google.android.material.bottomsheet.BottomSheetDialog

object DialogHelps {

    fun showStoragePermissionDialog(context: Context, open: () -> Unit, dismiss: () -> Unit) {
        val binding = DialogPermissionBinding.inflate(LayoutInflater.from(context))
        val dialog = BottomSheetDialog(context, R.style.BottomSheetDialog).apply {
            setContentView(binding.root)
            show()
        }
        dialog.setOnDismissListener { dismiss.invoke() }
        binding.tvOpenSettings.setOnClickListener {
            it.isEnabled = false
            open.invoke()
            it.postDelayed({ dialog.dismiss() }, 500)
        }
    }

    fun showNotificationDialog(context: Context, turnOn: () -> Unit, dismiss: () -> Unit) {
        val binding = DialogNotificationBinding.inflate(LayoutInflater.from(context))
        val dialog = BottomSheetDialog(context, R.style.BottomSheetDialog).apply {
            setCancelable(false)
            setCanceledOnTouchOutside(false)
            setContentView(binding.root)
            show()
        }
        binding.ivClose.setOnClickListener {
            dismiss.invoke()
            dialog.dismiss()
        }
        binding.tvTurnOn.setOnClickListener {
            it.isEnabled = false
            turnOn.invoke()
            it.postDelayed({ dialog.dismiss() }, 500)
        }
    }

    fun showScoreDialog(context: Context, view: View? = null): Dialog {
        view?.isEnabled = false
        val binding = DialogScoreBinding.inflate(LayoutInflater.from(context))
        val dialog = Dialog(context, R.style.CustomDialogStyle).apply {
            setCancelable(false)
            setCanceledOnTouchOutside(false)
            setContentView(binding.root)
            show()
        }
        val starList = listOf(
            binding.ivStar1,
            binding.ivStar2,
            binding.ivStar3,
            binding.ivStar4,
            binding.ivStar5,
        )
        var score = 0
        starList.forEachIndexed { index, view ->
            view.setOnClickListener {
                starList.forEachIndexed { index2, view2 ->
                    view2.setImageResource(if (index2 <= index) R.mipmap.star_s else R.mipmap.star_n)
                    if (index == index2) scoreAnimation(view2)
                }
                score = index + 1
                binding.lottieFace.cancelAnimation()
                binding.lottieFace.setAnimation(if (score != 5) "top_rate_sad.json" else "top_rate_happy.json")
                binding.lottieFace.playAnimation()
            }
        }
        binding.ivClose.setOnClickListener {
            view?.isEnabled = true
            dialog.dismiss()
        }
        binding.tvSubmit.setOnClickListener {
            if (score == 5) {
                SpUtils.getInstance().putInt(KEY_SCORE, score)
            }
            try {
                val url = "https://play.google.com/store/apps/details?id=${context.packageName}"
                context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
            } catch (_: Exception) {
            }
            view?.isEnabled = true
            dialog.dismiss()
        }
        return dialog
    }

    private fun scoreAnimation(view: View) {
        ObjectAnimator.ofFloat(
            view,
            View.SCALE_X,
            0.75f,
            1.2f,
            1f
        ).apply {
            duration = 500
            interpolator = LinearInterpolator()
            start()
        }
        ObjectAnimator.ofFloat(
            view,
            View.SCALE_Y,
            0.75f,
            1.2f,
            1f
        ).apply {
            duration = 500
            interpolator = LinearInterpolator()
            start()
        }
    }

    fun showDeleteDialog(context: Context, view: View, confirm: (() -> Unit?)?): DialogDeleteBinding {
        view.isEnabled = false
        val binding = DialogDeleteBinding.inflate(LayoutInflater.from(context))
        val dialog = Dialog(context, R.style.CustomDialogStyle).apply {
            setCancelable(false)
            setCanceledOnTouchOutside(false)
            setContentView(binding.root)
            show()
        }
        binding.tvCancel.setOnClickListener {
            dialog.dismiss()
            view.isEnabled = true
        }
        binding.tvDelete.setOnClickListener {
            it.isEnabled = false
            dialog.dismiss()
            confirm?.invoke()
            view.isEnabled = true
        }
        return binding
    }


    fun showExitDialog(
        context: Activity,
        title: String,
        content: Any,
        confirm: () -> Unit,
    ) {
        val binding = DialogExitBinding.inflate(LayoutInflater.from(context))
        val dialog = Dialog(context, R.style.CustomDialogStyle).apply {
            setCancelable(false)
            setCanceledOnTouchOutside(false)
            setContentView(binding.root)
            show()
        }
        binding.tvTitle.text = title
        binding.tvContent.text = content.toString()
        binding.ivCancel.setOnClickListener {
            dialog.dismiss()
        }
        binding.tvCancel.setOnClickListener {
            dialog.dismiss()
        }
        binding.tvSure.setOnClickListener {
            it.isEnabled = false
            dialog.dismiss()
            confirm.invoke()
        }
        AdmobHelps.showNativeAd(context, completed = {
            binding.adNative.setExitNativeAd(it)
        }, maxCompleted = { nativeAdLoader, nativeMaxAd ->
            binding.adNative.setExitNativeAd(nativeAdLoader, nativeMaxAd)
        })
    }

    fun showLogoutDialog(context: Activity, confirm: () -> Unit, check: () -> Unit) {
        val binding = DialogLogoutBinding.inflate(LayoutInflater.from(context))
        val dialog = Dialog(context, R.style.CustomDialogStyle).apply {
            setCancelable(false)
            setCanceledOnTouchOutside(false)
            setContentView(binding.root)
            show()
        }
        binding.ivCancel.setOnClickListener { dialog.dismiss() }
        binding.tvCancel.setOnClickListener {
            it.isEnabled = false
            dialog.dismiss()
            check.invoke()
        }
        binding.tvExit.setOnClickListener {
            it.isEnabled = false
            dialog.dismiss()
            confirm.invoke()
        }
        AdmobHelps.showNativeAd(context, completed = {
            binding.adNative.setExitNativeAd(it)
        }, maxCompleted = { nativeAdLoader, nativeMaxAd ->
            binding.adNative.setExitNativeAd(nativeAdLoader, nativeMaxAd)
        })
    }

    fun showLoadingDialog(context: Context): Dialog {
        val binding = DialogLoadingBinding.inflate(LayoutInflater.from(context))
        val dialog = Dialog(context, R.style.CustomDialogStyle).apply {
            setCancelable(false)
            setCanceledOnTouchOutside(false)
            setContentView(binding.root)
            show()
        }
        return dialog
    }
}