package rowedto.jockeyedto.sheetedto.slideedto

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.FifteenedtoBinding
import rowedto.jockeyedto.sheetedto.atticedto.Prescriptionedto
import rowedto.jockeyedto.sheetedto.atticedto.calfedto.despiseedto.matrixedto.Upsetedto
import rowedto.jockeyedto.sheetedto.atticedto.directionedto.noxiousedto.batedto.Celebrationedto
import rowedto.jockeyedto.sheetedto.atticedto.directionedto.noxiousedto.batedto.Quicklyedto
import rowedto.jockeyedto.sheetedto.atticedto.equipmentedto.exactedto.costumeedto.Geneticedto
import rowedto.jockeyedto.sheetedto.atticedto.equipmentedto.exactedto.costumeedto.Mythedto
import rowedto.jockeyedto.sheetedto.atticedto.equipmentedto.exactedto.costumeedto.Swimmingedto
import rowedto.jockeyedto.sheetedto.atticedto.openedto.postedto.Killeredto
import rowedto.jockeyedto.sheetedto.atticedto.peakedto.affinityedto.jacketedto.Scrapedto
import rowedto.jockeyedto.sheetedto.atticedto.rededto.knifeedto.economyedto.Viableedto
import rowedto.jockeyedto.sheetedto.atticedto.salonedto.safetyedto.makeupedto.Massedto
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Aloofedto
import rowedto.jockeyedto.sheetedto.knewedto.economicedto.copyrightedto.Suitcaseedto.toFormatSize
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Stewardedto
import rowedto.jockeyedto.sheetedto.strangeedto.pyramidedto.particlesedto.Scandaledto
import java.io.File
import kotlin.random.Random

class Awareedto : Aloofedto<FifteenedtoBinding>() {

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


    override fun setView() {
        Scandaledto.admobMaxShowNativeAd(requireActivity(), binding.tooledto, 1)
        animatorSet = createHeartbeatAnimation(binding.passwordedto)
        showStorage()
    }

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

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

//            sendNotification(requireContext(), Dynamicedto.ID_JUNK_CLEAN_PUSH)
        }
        binding.dueedto.setOnClickListener {
            val jumpJunkFirstAd =
                Stewardedto.getInstance().getString("isjunkPlayAd", "0").toInt()

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

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

            if (jumpJunkFirstAd == 1) {
                startActivity(Intent(requireContext(), Quicklyedto::class.java))
            } else {
                startActivity(Intent(requireContext(), Celebrationedto::class.java))
            }
        }
        binding.theoryedto.setOnClickListener {
            startActivity(Intent(requireContext(), Killeredto::class.java))
        }
        binding.harassedto.setOnClickListener {
            startActivity(Intent(requireContext(), Viableedto::class.java))
        }

        binding.meansedto.setOnClickListener {
            startActivity(Intent(requireContext(), Upsetedto::class.java))
        }
        binding.wisecrackedto.setOnClickListener {
            startActivity(Intent(requireContext(), Massedto::class.java))
        }
        binding.bananaedto.setOnClickListener {
            startActivity(Intent(requireContext(), Scrapedto::class.java))
        }

        binding.welfareedto.setOnClickListener {
            startActivity(Intent(requireContext(), Prescriptionedto::class.java))
        }
        binding.indirectedto.setOnClickListener {
            if (System.currentTimeMillis() - lastClickTime > 5 * 1000 && i != 0) {
                lastClickTime = 0
                i = 0
            }
            if (i == 20) {
                copyUuidGid()
                i = 0
            }
            i++
            lastClickTime = System.currentTimeMillis()
        }
        binding.emotionedto.setOnClickListener {
            startActivity(Intent(requireActivity(), Swimmingedto::class.java))
        }
        binding.earedto.setOnClickListener {
            startActivity(Intent(requireActivity(), Geneticedto::class.java))
        }
    }

    private fun copyUuidGid() {
        val uuid = Stewardedto.getInstance().getString("uuid", "")
        val gid = Stewardedto.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()
        val bean = Mythedto.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 (Mythedto.getWeatherType(bean.iconDay.toInt())) {
                "Sunny day" -> R.mipmap.processedto
                "Cloudy day" -> R.mipmap.designeredto
                "Rainy day" -> R.mipmap.sideedto
                "Snowy day" -> R.mipmap.suspectedto
                "Greasy day" -> R.mipmap.distributoredto
                "Unknown" -> R.mipmap.processedto
                else -> R.mipmap.processedto
            }
            binding.sweetedto.text = value.toString()
            binding.variantedto.setImageResource(icon)
        }
    }

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

}