package rallyedto.remarkedto.cooperationedto.agricultureedto

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.R
import com.base.datarecovery.databinding.QuestionedtoBinding
import rallyedto.battleedto.acresedto.Dolledto
import rallyedto.battleedto.acresedto.Fireplaceedto.toFormatSize
import rallyedto.finishedto.Miracleedto
import rallyedto.finishedto.cableedto.Merchantedto
import rallyedto.finishedto.climateedto.depressionedto.dustedto.Treeedto
import rallyedto.finishedto.complianceedto.minuteedto.Incomeedto
import rallyedto.finishedto.complianceedto.minuteedto.Poundedto
import rallyedto.finishedto.hypothesisedto.vegetableedto.breakfastedto.Practicaledto
import rallyedto.finishedto.hypothesisedto.vegetableedto.breakfastedto.Systemedto
import rallyedto.finishedto.illnessedto.Sweptedto
import rallyedto.finishedto.pollutionedto.Opaledto
import rallyedto.finishedto.sheepedto.extraterrestrialedto.Pickedto
import rallyedto.talledto.blewedto.Pianoedto
import java.io.File
import kotlin.random.Random

class Electionedto : Dolledto<QuestionedtoBinding>() {

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


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

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

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

//            sendNotification(requireContext(), Snuggleedto.ID_JUNK_CLEAN_PUSH)
        }
        binding.youthedto.setOnClickListener {
            val jumpJunkFirstAd =
                Pianoedto.getInstance().getString("isjunkPlayAd", "0").toInt()

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


        binding.concentrationedto.setOnClickListener {
            val jumpJunkFirstAd =
                Pianoedto.getInstance().getString("isjunkPlayAd", "0").toInt()

            if (jumpJunkFirstAd == 1) {
                startActivity(Intent(requireContext(), Systemedto::class.java))
            } else {
                startActivity(Intent(requireContext(), Practicaledto::class.java))
            }
        }
//        binding.coloredto.setOnClickListener {
//            startActivity(Intent(requireContext(), WhatsAppCleanerAnimationActivity::class.java))
//        }
        binding.curriculumedto.setOnClickListener {
            startActivity(Intent(requireContext(), Sweptedto::class.java))
        }

        binding.bodyedto.setOnClickListener {
            startActivity(Intent(requireContext(), Pickedto::class.java))
        }
//        binding.cardAppProcess.setOnClickListener {
//            startActivity(Intent(requireContext(), AppProcessAnimationActivity::class.java))
//        }
        binding.smoothedto.setOnClickListener {
            startActivity(Intent(requireContext(), Treeedto::class.java))
        }
        binding.haircutedto.setOnClickListener {
            startActivity(Intent(requireContext(), Merchantedto::class.java))

        }
        binding.relationshipedto.setOnClickListener {
            startActivity(Intent(requireContext(), Opaledto::class.java))

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

        binding.ghostedto.setOnClickListener {
            startActivity(Intent(requireActivity(), Incomeedto::class.java))
        }
    }

    private fun copyUuidGid() {
        val uuid = Pianoedto.getInstance().getString("uuid", "")
        val gid = Pianoedto.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()
       // Togetheredto.showNativeAd(requireActivity(), binding.jeweledto, 1)
        animatorSet.resume()
        val bean = Poundedto.getWeatherEntity()?.list?.get(0)
        bean?.let {
            var value = 22
            val min = bean.tempMin.toInt()
            val max = bean.tempMax.toInt()
            if (min == max) {
                value = max
            }
            if (min < max) {
                value = Random.nextInt(min, max)
            }
            val icon = when (Poundedto.getWeatherType(bean.iconDay.toInt())) {
                "Sunny day" -> R.mipmap.neighboredto
                "Cloudy day" -> R.mipmap.alongedto
                "Rainy day" -> R.mipmap.factoredto
                "Snowy day" -> R.mipmap.conductoredto
                "Greasy day" -> R.mipmap.demonstratoredto
                "Unknown" -> R.mipmap.neighboredto
                else -> R.mipmap.neighboredto
            }
            binding.feminineedto.text = value.toString()
            binding.chivalryedto.setImageResource(icon)
        }
    }

    @SuppressLint("SetTextI18n")
    fun showStorage() {
        binding.hearedto.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.fitnessedto.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.fitnessedto.text = "$usedSize / $totalSize"
        }
    }

}