package com.base.datarecovery.fragment

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import com.base.datarecovery.activity.MainActivity
import com.base.datarecovery.activity.SettingActivity
import com.base.datarecovery.activity.junkclean.ScanJunkActivity
import com.base.datarecovery.activity.privacyspace.PrivacyPinOneActivity
import com.base.datarecovery.activity.privacyspace.PrivacySpaceActivity
import com.base.datarecovery.activity.recovery.FileScanResultActivity
import com.base.datarecovery.activity.repeat.RepeatAnimationActivity
import com.base.datarecovery.activity.screenshot.ScreenShotAnimationActivity
import com.base.datarecovery.ads.AdmobMaxHelper
import com.base.datarecovery.ads.admob.AdmobBannerUtils
import com.base.datarecovery.bean.ConstObject
import com.base.datarecovery.bean.ConstObject.SCAN_DOCUMENTS
import com.base.datarecovery.bean.ConstObject.SCAN_PHOTOS
import com.base.datarecovery.bean.ConstObject.SCAN_VIDEOS
import com.base.datarecovery.bean.ConstObject.privacyPinPassword
import com.base.datarecovery.databinding.FragmentHomeBinding
import com.base.datarecovery.fcm.NotificationUtil.sendNotification
import com.base.datarecovery.help.BaseFragment
import com.base.datarecovery.help.KotlinExt.toFormatSize
import com.google.android.gms.ads.AdView

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)
    }

    override fun setListener() {
        binding.flScan.setOnClickListener {
            startActivity(Intent(requireContext(), ScanJunkActivity::class.java))
//            sendNotification(requireContext(), ConstObject.ID_JUNK_CLEAN_PUSH)
        }
        binding.flRyPhoto.setOnClickListener {
            startActivity(Intent(requireContext(), FileScanResultActivity::class.java).apply {
                putExtra("ScanType", SCAN_PHOTOS)
            })
        }
        binding.flRyVideo.setOnClickListener {
            startActivity(Intent(requireContext(), FileScanResultActivity::class.java).apply {
                putExtra("ScanType", SCAN_VIDEOS)
            })
        }
        binding.cardRyDocument.setOnClickListener {
            startActivity(Intent(requireContext(), FileScanResultActivity::class.java).apply {
                putExtra("ScanType", SCAN_DOCUMENTS)
            })
        }
        binding.cardPrivacySpace.setOnClickListener {
            if (privacyPinPassword.isNotEmpty()) {
                startActivity(Intent(requireContext(), PrivacyPinOneActivity::class.java))
            } else {
                startActivity(Intent(requireContext(), PrivacySpaceActivity::class.java).apply {

                })
            }
        }
        binding.cardScreenshot.setOnClickListener {
            startActivity(Intent(requireContext(), ScreenShotAnimationActivity::class.java))
        }
        binding.cardRepeatPhotos.setOnClickListener {
            startActivity(Intent(requireContext(), RepeatAnimationActivity::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()

        (requireActivity() as MainActivity).setScreenSize {
            if (it > 0) {
                binding.tvScreenSize.visibility = View.VISIBLE
                binding.tvScreenSize.text = it.toFormatSize(0)
            } else {
                binding.tvScreenSize.visibility = View.GONE
            }
        }
    }

}