package com.test.basd.fastcleanerjunk.fragment

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Environment
import android.os.StatFs
import android.view.animation.LinearInterpolator
import android.widget.ScrollView
import androidx.core.view.updatePadding
import com.blankj.utilcode.util.BarUtils
import com.test.basd.fastcleanerjunk.activity.AppManagerActivity
import com.test.basd.fastcleanerjunk.activity.BatteryInfoActivity
import com.test.basd.fastcleanerjunk.activity.DeviceScanActivity
import com.test.basd.fastcleanerjunk.activity.LargeFileCleanActivity
import com.test.basd.fastcleanerjunk.activity.NetWorkActivity
import com.test.basd.fastcleanerjunk.activity.RecentAppActivity
import com.test.basd.fastcleanerjunk.activity.ScanJunkActivity
import com.test.basd.fastcleanerjunk.activity.SettingActivity
import com.test.basd.fastcleanerjunk.activity.photocompress.photo.StartCompressionPhotoActivity
import com.test.basd.fastcleanerjunk.databinding.FragmentLayoutHomeBinding
import com.test.basd.fastcleanerjunk.helps.BaseFragment
import com.test.basd.fastcleanerjunk.helps.KotlinExt.setOnClickListener
import com.test.basd.fastcleanerjunk.helps.KotlinExt.toFormatSize
import com.test.basd.fastcleanerjunk.helps.NetWorkSpeed
import com.test.basd.fastcleanerjunk.helps.StatFsHelper.availableSize
import com.test.basd.fastcleanerjunk.helps.StatFsHelper.totalSize
import com.test.basd.fastcleanerjunk.notificationclean.NotificationGuestActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

class HomeFragment : BaseFragment<FragmentLayoutHomeBinding>() {

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

    override fun setView() {
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
        ValueAnimator.ofFloat(0f, 360f).run {
            duration = 1000
            repeatMode = ValueAnimator.RESTART
            repeatCount = ValueAnimator.INFINITE
            interpolator = LinearInterpolator()
            addUpdateListener {
                runCatching {
                    binding.ivXuanzhuan.rotation = it.animatedValue as Float
                }
            }
            start()
        }
        initStorage()
    }

    @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.idImgCompress.setOnClickListener {
            startActivity(Intent(requireActivity(), StartCompressionPhotoActivity::class.java))
        }

        binding.idAppManager.setOnClickListener {
            startActivity(Intent(requireContext(), AppManagerActivity::class.java))
        }

        binding.idLargeFile.setOnClickListener {
            startActivity(Intent(requireContext(), LargeFileCleanActivity::class.java))
        }

        binding.idBatteryInfo.setOnClickListener {
            startActivity(Intent(requireContext(), BatteryInfoActivity::class.java))
        }

        binding.idNotificationCleaner.setOnClickListener {
            startActivity(Intent(requireActivity(), NotificationGuestActivity::class.java))
        }

        binding.idRecentApp.setOnClickListener {
            startActivity(Intent(requireActivity(), RecentAppActivity::class.java))
        }

        binding.idNetwork.setOnClickListener {
            startActivity(Intent(requireActivity(), NetWorkActivity::class.java))
        }

        binding.ivSetting.setOnClickListener {
            startActivity(Intent(requireActivity(), SettingActivity::class.java))
        }
        binding.idDeviceScan.setOnClickListener {
            startActivity(Intent(requireActivity(), DeviceScanActivity::class.java))
        }

        MainScope().launch(Dispatchers.IO) {
            NetWorkSpeed.startMoritor()
        }
    }


    @SuppressLint("SetTextI18n")
    private fun initStorage() {
        val stat = StatFs(Environment.getExternalStorageDirectory().path)
        val totalSize = totalSize()
        val availableSize = availableSize()
        val usedSize = totalSize - availableSize
        val usedPercentage = usedSize.toFloat() / totalSize * 100
        binding.idTvOccupied.text = "${usedSize.toFormatSize()}/ ${totalSize.toFormatSize()}"
    }

}