package com.base.easyfilemanager.fragment

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Intent
import android.view.animation.LinearInterpolator
import android.widget.ScrollView
import androidx.core.view.updatePadding
import com.base.easyfilemanager.activity.AppManagerActivity
import com.base.easyfilemanager.activity.BatteryInfoActivity
import com.base.easyfilemanager.activity.LargeFileCleanActivity
import com.base.easyfilemanager.activity.NetWorkActivity
import com.base.easyfilemanager.activity.RecentAppActivity
import com.base.easyfilemanager.activity.RepeatPhotoActivity
import com.base.easyfilemanager.activity.ScanJunkActivity
import com.base.easyfilemanager.activity.ScreenShotActivity
import com.base.easyfilemanager.activity.SettingActivity
import com.base.easyfilemanager.activity.photocompress.photo.StartCompressionPhotoActivity
import com.base.easyfilemanager.databinding.FragmentLayoutHomeBinding
import com.base.easyfilemanager.helps.BaseFragment
import com.base.easyfilemanager.helps.KotlinExt.setOnClickListener
import com.base.easyfilemanager.helps.KotlinExt.toFormatSize
import com.base.easyfilemanager.helps.StatFsHelper
import com.base.easyfilemanager.notificationclean.NotificationGuestActivity
import com.base.easyfilemanager.utils.BarUtils

class HomeFragment : BaseFragment<FragmentLayoutHomeBinding>() {

    override val binding: FragmentLayoutHomeBinding by lazy {
        FragmentLayoutHomeBinding.inflate(layoutInflater)
    }


    override fun setView() {
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
        startAnimation()
        initStorage()
    }

    private var animator: ValueAnimator? = null
    private var animPaused = false
    private var rotationValue = 0f

    private fun startAnimation() {
        animator = ValueAnimator.ofFloat(rotationValue, rotationValue + 360f).apply {
            duration = 1000
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener {
                runCatching {
                    rotationValue = it.animatedValue as Float
                    binding.idImgXuanzhuan.rotation = rotationValue
                }
            }
            start()
        }
    }

    override fun onResume() {
        super.onResume()
        if (animPaused) {
            startAnimation()
            animPaused = false
        }
    }

    override fun onPause() {
        super.onPause()
        animator?.pause()
        animPaused = true
    }

    @SuppressLint("SetTextI18n")
    override fun setListener() {
        binding.idImgSl.setOnClickListener {
            binding.idScrollView.fullScroll(ScrollView.FOCUS_DOWN)
        }
        listOf(binding.idCleanJunk, binding.idFlClean, binding.idCleanUp).setOnClickListener {
            startActivity(Intent(requireContext(), ScanJunkActivity::class.java))
        }

        binding.idPhotoCompress.setOnClickListener {
            startActivity(Intent(requireActivity(), StartCompressionPhotoActivity::class.java))
        }
        binding.idLargeFile.setOnClickListener {
            startActivity(Intent(requireContext(), LargeFileCleanActivity::class.java))
        }
        binding.idAppManager.setOnClickListener {
            startActivity(Intent(requireContext(), AppManagerActivity::class.java))
        }
        binding.idBatteryInfo.setOnClickListener {
            startActivity(Intent(requireContext(), BatteryInfoActivity::class.java))
        }

        binding.idSimilarPhotos.setOnClickListener {
            startActivity(Intent(requireContext(), RepeatPhotoActivity::class.java))
        }
        binding.idHomeNetwork.setOnClickListener {
            startActivity(Intent(requireContext(), NetWorkActivity::class.java))
        }
        binding.idNotificationCleaner.setOnClickListener {
            startActivity(Intent(requireActivity(), NotificationGuestActivity::class.java))
        }
        binding.idHomeRecent.setOnClickListener {
            startActivity(Intent(requireContext(), RecentAppActivity::class.java))
        }
        binding.idScreenshotCleaner.setOnClickListener {
            startActivity(Intent(requireContext(), ScreenShotActivity::class.java))
        }
        binding.ivSetting.setOnClickListener {
            startActivity(Intent(requireContext(), SettingActivity::class.java))
        }

    }


    @SuppressLint("SetTextI18n")
    private fun initStorage() {
        val totalSize = StatFsHelper.totalSize()
        val availableSize = StatFsHelper.availableSize()
        val usedSize = totalSize - availableSize
        binding.idTvOccupied.text = "${usedSize.toFormatSize()} / ${totalSize.toFormatSize()}"
    }

}