package com.base.datarecovery.fragment

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.app.usage.StorageStatsManager
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 com.base.datarecovery.activity.SettingActivity
import com.base.datarecovery.activity.appmanager.AppManagerAnimationActivity
import com.base.datarecovery.activity.appprocess.AppProcessAnimationActivity
import com.base.datarecovery.activity.battery.BatteryInfoAnimationActivity
import com.base.datarecovery.activity.junkclean.ScanJunkActivity
import com.base.datarecovery.activity.largefile.LargeFileAnimationActivity
import com.base.datarecovery.activity.whatsapp.WhatsAppCleanerAnimationActivity
import com.base.datarecovery.databinding.FragmentHomeBinding
import com.base.datarecovery.help.BaseFragment
import com.base.datarecovery.help.KotlinExt.toFormatSize
import java.io.File

class HomeFragment : BaseFragment<FragmentHomeBinding>() {

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


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

    override fun setListener() {
        binding.flScan.setOnClickListener {
            startActivity(Intent(requireContext(), ScanJunkActivity::class.java))
//            sendNotification(requireContext(), ConstObject.ID_JUNK_CLEAN_PUSH)
        }
        binding.tvClean.setOnClickListener {
            startActivity(Intent(requireContext(), ScanJunkActivity::class.java))
        }
        binding.cardCleanJunk.setOnClickListener {
            startActivity(Intent(requireContext(), ScanJunkActivity::class.java))
        }
        binding.cardWhatsApp.setOnClickListener {
            startActivity(Intent(requireContext(), WhatsAppCleanerAnimationActivity::class.java))
        }
        binding.cardLargeFile.setOnClickListener {
            startActivity(Intent(requireContext(), LargeFileAnimationActivity::class.java))
        }

        binding.cardAppManager.setOnClickListener {
            startActivity(Intent(requireContext(), AppManagerAnimationActivity::class.java))
        }
        binding.cardAppProcess.setOnClickListener {
            startActivity(Intent(requireContext(), AppProcessAnimationActivity::class.java))
        }
        binding.cardBatteryInfo.setOnClickListener {
            startActivity(Intent(requireContext(), BatteryInfoAnimationActivity::class.java))
        }

        binding.flSetting.setOnClickListener {
            startActivity(Intent(requireContext(), SettingActivity::class.java))
        }

    }

    @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.llStorage.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.tvStorage.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.tvStorage.text = "$usedSize / $totalSize"
        }
    }

}