package com.base.datarecovery.view

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.Intent
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.activity.recovery.FileScanResultActivity
import com.base.datarecovery.ads.AdmobNativeUtils
import com.base.datarecovery.databinding.DialogFileScanBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

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

    @SuppressLint("SetTextI18n")
    fun showFileScanDialog(
        sharedFlow: SharedFlow<String>,
        foundFlow: SharedFlow<Int>
    ): 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.dp_300)
        dialog?.window?.attributes = params

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

        dialog?.setOnDismissListener {
            a1.cancel()
        }

        AdmobNativeUtils.showNativeAd(activity, binding.flAd, R.layout.layout_native_custom_big)

        activity.lifecycleScope.launch(Dispatchers.Main) {
            sharedFlow.collectLatest { path ->
                binding.tvPath.text = path
            }
        }
        activity.lifecycleScope.launch {
            foundFlow.collectLatest {
                binding.tvFoundNumber.text = "Found $it files"
            }
        }


        return dialog
    }

    @SuppressLint("SetTextI18n")
    fun stopScan(scanType: Int, pathList: ArrayList<String>) {
        a1.cancel()
        binding.tvBtn.visibility = View.VISIBLE
        binding.iv.visibility = View.GONE
        binding.tvTitle.text = "Scan Completed"
        binding.tvBtn.setOnClickListener {
            activity.startActivity(Intent(activity, FileScanResultActivity::class.java).apply {
                putExtra("ScanType", scanType)
                putExtra("PathList", pathList.toTypedArray())
            })
            activity.finish()
        }
    }
}