package com.bassbooster.equalizer.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.Button
import android.widget.FrameLayout
import android.widget.ProgressBar
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.AppCompatButton
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import com.bassbooster.equalizer.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


object DialogUtils {

    fun showDialogConfirmPer(context: Context?, title:String?,description: String?,
                   listenerOK:(() -> Unit)? = null, listenerCancel:(() -> Unit)? = null): AlertDialog? {
        if (context == null) return null
        val dialog : AlertDialog =
        AlertDialog.Builder(context)
            .setView(R.layout.layout_dialog_permission)
            .create().apply {
                setCancelable(false)
                setCanceledOnTouchOutside(false)
            }

        dialog.apply {
            window?.setBackgroundDrawableResource(R.drawable.custom_dialog)
            show()
            findViewById<AppCompatTextView>(R.id.tvTitle)?.text = title
            findViewById<AppCompatTextView>(R.id.tvDescription)?.text = description
            findViewById<AppCompatButton>(R.id.btnOK)?.setOnClickListener {
                listenerOK?.invoke()
                this.dismiss()
            }
            findViewById<AppCompatButton>(R.id.btnCancel)?.setOnClickListener {
                listenerCancel?.invoke()
                this.dismiss()
            }
        }
        return dialog
    }

    fun showDialog(context: Context?,description: String?,
                   listenerOK:(() -> Unit)? = null, listenerCancel:(() -> Unit)? = null): AlertDialog? {
        if (context == null) return null
        val dialog : AlertDialog =
            AlertDialog.Builder(context)
                .setView(R.layout.layout_dialog)
                .create().apply {
                    setCancelable(false)
                    setCanceledOnTouchOutside(false)
                }

        dialog.apply {
            window?.setBackgroundDrawableResource(R.drawable.custom_dialog)
            show()
            findViewById<AppCompatTextView>(R.id.tvTitle)?.text = context.resources.getString(R.string.title_opp)
            findViewById<AppCompatTextView>(R.id.tvDescription)?.text = description
            findViewById<AppCompatButton>(R.id.btnOK)?.setOnClickListener {
                listenerOK?.invoke()
                this.dismiss()
            }
        }
        return dialog
    }

    @SuppressLint("SetTextI18n")
    fun showDialogSaveSuccess(
        context: Context?,
        title: String?,
        listenerOK: (() -> Unit)? = null,
        activity: Activity?
    ): AlertDialog? {
        var progressStatus = 0

        val handler = Handler(Looper.getMainLooper())

        val totalTime = 4000 // 4 giây (4000 milliseconds)
        val interval = 40 // Cập nhật giá trị mỗi 40 milliseconds

        val steps = totalTime / interval // Số bước cần để đạt được tổng thời gian
        var homeAdsLoaded = false
        if (context == null) return null
        val dialog: AlertDialog =
            AlertDialog.Builder(context)
                .setView(R.layout.layout_dialog_save)
                .create().apply {
                    setCancelable(false)
                    setCanceledOnTouchOutside(false)
                }

        dialog.apply {
            window?.setBackgroundDrawableResource(R.drawable.custom_dialog)
            show()
            val progressBar = findViewById<ProgressBar>(R.id.Prog)
            val countProg = findViewById<TextView>(R.id.countProg)
            val btnOK = findViewById<AppCompatButton>(R.id.btnOK)

            Thread {
                for (i in 0 until steps) {
                    progressStatus =
                        (i * 100) / steps // Tính giá trị của progressStatus từ 0 đến 100
                    handler.post {
                        progressBar?.progress = progressStatus + 1
                        countProg?.text = "${progressStatus + 1}%"
                    }

                    try {
                        Thread.sleep(interval.toLong()) // Đợi `interval` milliseconds trước khi cập nhật giá trị tiếp theo
                    } catch (e: InterruptedException) {
                        e.printStackTrace()
                    }
                }

                handler.post {
                    btnOK?.isEnabled = true
                    btnOK?.background = context.resources.getDrawable(R.drawable.button_border)
                }
            }.start()

            findViewById<AppCompatTextView>(R.id.tvTitle)?.text = title
            btnOK?.setOnClickListener {
                listenerOK?.invoke()
                this.dismiss()
            }

        }
        return dialog
    }

}