package rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.app.AlertDialog
import android.view.LayoutInflater
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.base.datarecovery.R
import com.base.datarecovery.databinding.FoundedtoBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.Scandaledto

class Vastedto(
    val activity: AppCompatActivity,
) {
    private val TAG = "FileScanDialog"
    private val a1 = ValueAnimator.ofFloat(0f, -360f)
    private val dialog = AlertDialog.Builder(activity).create()
    private val binding = FoundedtoBinding.inflate(LayoutInflater.from(activity))

    @SuppressLint("SetTextI18n")
    fun showFileScanDialog(
        pathFlow: SharedFlow<String>,
        foundFlow: SharedFlow<Pair<Int, String>>,
        foundAction: ((path: String) -> Unit)? = null
    ): AlertDialog {
        dialog?.setView(binding.root)
        dialog?.setCanceledOnTouchOutside(false)
        dialog?.setCancelable(false)
        dialog?.show()

        dialog?.window?.setBackgroundDrawableResource(android.R.color.transparent)
        val params = dialog?.window?.attributes
        params?.width = activity.resources.getDimensionPixelOffset(R.dimen.drewedto)
        dialog?.window?.attributes = params

        a1.run {
            duration = 1000
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener {
                binding.shiftedto.rotation = it.animatedValue as Float
            }
            start()
        }

        dialog?.setOnDismissListener {
            a1.cancel()
        }

        Scandaledto.admobMaxShowNativeAd(activity, binding.breatheedto, 1)

        var pathShowed = false
        activity.lifecycleScope.launch(Dispatchers.Main) {
            pathFlow.collectLatest { path ->
                if (!pathShowed) {
                    pathShowed = true
                    binding.unlawfuledto.visibility = View.VISIBLE
                }
                binding.unlawfuledto.text = path
            }
        }
        activity.lifecycleScope.launch {
            foundFlow.collectLatest {
                binding.attractiveedto.text = "Found ${it.first} files"
                foundAction?.invoke(it.second)
            }
        }

        return dialog
    }

    @SuppressLint("SetTextI18n")
    fun finishScan(clickAction: () -> Unit) {
        a1.cancel()
        binding.recordingedto.visibility = View.VISIBLE
        binding.shiftedto.visibility = View.GONE
        binding.unlawfuledto.visibility = View.GONE
        binding.indirectedto.text = "Scan Completed"
        binding.recordingedto.setOnClickListener {
            clickAction.invoke()
        }
    }
}