package importantedto.candidateedto

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.app.usage.StorageStatsManager
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Environment
import android.os.StatFs
import android.os.storage.StorageManager
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.base.datarecovery.databinding.EstablishedtoBinding
import importantedto.afteredto.constitutionaledto.Constraintedto.toFormatSize
import importantedto.afteredto.constitutionaledto.Jumpedto
import importantedto.appliededto.surgeonedto.promiseedto.Hatedto
import importantedto.appliededto.surgeonedto.promiseedto.enteredto.eatenedto.ridingedto.Madeedto
import importantedto.appliededto.surgeonedto.promiseedto.obtainedto.museumedto.outletedto.Probablyedto
import importantedto.appliededto.surgeonedto.promiseedto.petedto.Delayedto
import importantedto.appliededto.surgeonedto.promiseedto.prospectedto.consumptionedto.blameedto.Failedto
import importantedto.appliededto.surgeonedto.promiseedto.prospectedto.consumptionedto.blameedto.Reserveedto
import importantedto.appliededto.surgeonedto.promiseedto.steeledto.Liveredto
import importantedto.appliededto.surgeonedto.promiseedto.wonderedto.Tiptoeedto
import importantedto.gallonedto.Waitedto
import java.io.File

class Problemedto : Jumpedto<EstablishedtoBinding>() {

    override val binding: EstablishedtoBinding by lazy {
        EstablishedtoBinding.inflate(layoutInflater)
    }
    private lateinit var animatorSet: AnimatorSet


    override fun setView() {
        animatorSet = createHeartbeatAnimation(binding.groundedto)
        showStorage()
    }

    var i = 0
    private var lastClickTime = 0L
    override fun setListener() {
        binding.groundedto.setOnClickListener {
            val jumpJunkFirstAd = Waitedto.getInstance().getString("isjunkPlayAd", "0").toInt()

            if (jumpJunkFirstAd == 1) {
                startActivity(Intent(requireContext(), Failedto::class.java))
            } else {
                startActivity(Intent(requireContext(), Reserveedto::class.java))
            }

//            sendNotification(requireContext(), Hateedto.ID_JUNK_CLEAN_PUSH)
        }
        binding.worldedto.setOnClickListener {
            val jumpJunkFirstAd = Waitedto.getInstance().getString("isjunkPlayAd", "0").toInt()

            if (jumpJunkFirstAd == 1) {
                startActivity(Intent(requireContext(), Failedto::class.java))
            } else {
                startActivity(Intent(requireContext(), Reserveedto::class.java))
            }
        }
        binding.convulsionedto.setOnClickListener {
            val jumpJunkFirstAd = Waitedto.getInstance().getString("isjunkPlayAd", "0").toInt()

            if (jumpJunkFirstAd == 1) {
                startActivity(Intent(requireContext(), Failedto::class.java))
            } else {
                startActivity(Intent(requireContext(), Reserveedto::class.java))
            }
        }
        binding.familyedto.setOnClickListener {
            startActivity(Intent(requireContext(), Tiptoeedto::class.java))
        }
        binding.progressiveedto.setOnClickListener {
            startActivity(Intent(requireContext(), Madeedto::class.java))
        }

        binding.bagedto.setOnClickListener {
            startActivity(Intent(requireContext(), Liveredto::class.java))
        }
        binding.excludeedto.setOnClickListener {
            startActivity(Intent(requireContext(), Delayedto::class.java))
        }
        binding.extremeedto.setOnClickListener {
            startActivity(Intent(requireContext(), Probablyedto::class.java))
        }

        binding.whichedto.setOnClickListener {
            startActivity(Intent(requireContext(), Hatedto::class.java))
        }
        binding.assaultedto.setOnClickListener {
            if (System.currentTimeMillis() - lastClickTime > 5 * 1000 && i != 0) {
                lastClickTime = 0
                i = 0
            }
            if (i == 20) {
                copyUuidGid()
                i = 0
            }
            i++
            lastClickTime = System.currentTimeMillis()
        }

    }

    private fun copyUuidGid() {
        val uuid = Waitedto.getInstance().getString("uuid", "")
        val gid = Waitedto.getInstance().getString("gid", "")
        // 获取系统服务ClipboardManager
        val clipboard: ClipboardManager =
            requireContext().getSystemService(AppCompatActivity.CLIPBOARD_SERVICE) as ClipboardManager
        // 创建一个ClipData对象，其中包含要复制的文本
        val clip = ClipData.newPlainText("uuid", "uuid=$uuid gid=$gid")
        // 将ClipData对象设置到剪贴板中
        clipboard.setPrimaryClip(clip)
        Toast.makeText(requireContext(), "copy", Toast.LENGTH_SHORT).show()
    }

    @SuppressLint("Recycle")
    fun createHeartbeatAnimation(view: View): AnimatorSet {
        // 创建放大动画
        val scaleUpAnimator = ObjectAnimator.ofFloat(view, "scaleX", 1f, 1.1f)
        scaleUpAnimator.setDuration(500)
        scaleUpAnimator.interpolator = AccelerateDecelerateInterpolator()
        scaleUpAnimator.repeatCount = ObjectAnimator.INFINITE
        scaleUpAnimator.repeatMode = ObjectAnimator.REVERSE

        // 创建缩小动画
        val scaleDownAnimator = ObjectAnimator.ofFloat(view, "scaleY", 1.1f, 1f)
        scaleDownAnimator.setDuration(500)
        scaleDownAnimator.interpolator = AccelerateDecelerateInterpolator()
        scaleDownAnimator.setStartDelay(500) // 延迟开始，以便与放大动画同步结束
        scaleDownAnimator.repeatCount = ObjectAnimator.INFINITE
        scaleDownAnimator.repeatMode = ObjectAnimator.REVERSE

        // 组合放大和缩小动画
        val animatorSet = AnimatorSet()
        animatorSet.play(scaleUpAnimator).with(scaleDownAnimator)

        // 启动动画
        animatorSet.start()
        return animatorSet
    }

    override fun onPause() {
        super.onPause()
        animatorSet.pause()
    }


    override fun onResume() {
        super.onResume()
        animatorSet.resume()
    }

    @SuppressLint("SetTextI18n")
    fun showStorage() {
        binding.thighedto.visibility = View.VISIBLE

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            val storageStatsManager = requireContext().getSystemService(Context.STORAGE_STATS_SERVICE) as StorageStatsManager

            Thread {
                val totalBytes = storageStatsManager.getTotalBytes(StorageManager.UUID_DEFAULT)
                val usedBytes = totalBytes - storageStatsManager.getFreeBytes(StorageManager.UUID_DEFAULT)
                binding.root.post {
                    binding.intermediateedto.text = "${usedBytes.toFormatSize()} / ${totalBytes.toFormatSize()}"
                }
            }.start()

        } else {
            val path1: File = Environment.getDataDirectory()
            val stat1 = StatFs(path1.path)
            val path2: File = Environment.getExternalStorageDirectory()
            val stat2 = StatFs(path2.path)
            val totalSize = (stat1.totalBytes + stat2.totalBytes).toFormatSize()
            val usedSize = ((stat1.totalBytes + stat2.totalBytes) - (stat1.availableBytes + stat2.availableBytes)).toFormatSize()
            binding.intermediateedto.text = "$usedSize / $totalSize"
        }
    }

}