package com.test.basd.smartjunkcleaner.fragment

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Environment
import android.os.StatFs
import android.util.Log
import android.view.animation.LinearInterpolator
import androidx.core.view.updatePadding
import com.blankj.utilcode.util.BarUtils
import com.test.basd.smartjunkcleaner.activity.AppManagerActivity
import com.test.basd.smartjunkcleaner.activity.BatteryInfoActivity
import com.test.basd.smartjunkcleaner.activity.LargeFileCleanActivity
import com.test.basd.smartjunkcleaner.activity.NetWorkActivity
import com.test.basd.smartjunkcleaner.activity.RecentAppActivity
import com.test.basd.smartjunkcleaner.activity.RepeaterdPhotoActivity
import com.test.basd.smartjunkcleaner.activity.ScanJunkActivity
import com.test.basd.smartjunkcleaner.activity.photocompress.photo.StartCompressionPhotoActivity
import com.test.basd.smartjunkcleaner.databinding.FragmentLayoutHomeBinding
import com.test.basd.smartjunkcleaner.helps.BaseFragment
import com.test.basd.smartjunkcleaner.helps.KotlinExt.setOnClickListener
import com.test.basd.smartjunkcleaner.helps.KotlinExt.toFormatSize
import com.test.basd.smartjunkcleaner.helps.NetWorkSpeed
import com.test.basd.smartjunkcleaner.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.idImgXuanzhuan.rotation = it.animatedValue as Float
                }
            }
            start()
        }
        initStorage()
    }

    @SuppressLint("SetTextI18n")
    override fun setListener() {

        listOf(binding.idCleanJunk, binding.idFlClean,binding.idCleanJunk2).setOnClickListener {
            startActivity(Intent(requireContext(), ScanJunkActivity::class.java))
        }

        binding.idPhotoCompress.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.llSimilar.setOnClickListener {
            startActivity(Intent(requireActivity(),RepeaterdPhotoActivity::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))
        }


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

    var totalsize = 0L

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

    fun f(): Long {
        return if (totalsize > 0) {
            totalsize
        } else try {
            val statFs = StatFs(Environment.getExternalStorageDirectory().path)
            val statFs2 = StatFs(Environment.getRootDirectory().path)
            Log.i(
                "storeByteDebug",
                "store1: " + statFs2.blockSizeLong * statFs2.blockCountLong + "====store2: " + statFs.blockSizeLong * statFs.blockCountLong
            )
            var blockSizeLong =
                statFs2.blockSizeLong * statFs2.blockCountLong + statFs.blockSizeLong * statFs.blockCountLong

            blockSizeLong = getClosestPowerOfTwo(blockSizeLong)
            totalsize = blockSizeLong
            blockSizeLong

        } catch (e10: java.lang.Exception) {
            0L
        }
    }

    fun a(j10: Long): Long {
        var j10 = j10
        var i7 = 0
        while (true) {
            val j11: Long = 1000
            if (j10 < j11) {
                break
            }
            j10 /= j11
            i7++
        }
        for (i10 in 0 until i7) {
            j10 *= 1024
        }
        return j10
    }

    fun b(): Long {
        try {
            val statFs = StatFs(Environment.getExternalStorageDirectory().path)
            val statFs2 = StatFs(Environment.getRootDirectory().path)
            val blackSize =
                statFs2.blockSizeLong * statFs2.availableBlocksLong + statFs.blockSizeLong * statFs.availableBlocksLong
            return a(blackSize)
        } catch (e10: Exception) {
            return 0L
        }
    }

    fun getClosestPowerOfTwo(size: Long): Long {
        var powerOfTwo: Long = 1
        while (powerOfTwo < size) {
            powerOfTwo *= 2
        }
        return powerOfTwo
    }
}