package com.test.basd.onecleanmaster.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 com.blankj.utilcode.util.SPUtils
import com.test.basd.onecleanmaster.activity.AppManagerActivity
import com.test.basd.onecleanmaster.activity.BatteryInfoActivity
import com.test.basd.onecleanmaster.activity.LargeFileCleanActivity
import com.test.basd.onecleanmaster.activity.NetWorkActivity
import com.test.basd.onecleanmaster.activity.RecentAppActivity
import com.test.basd.onecleanmaster.activity.RepeatedPhotoActivity
import com.test.basd.onecleanmaster.activity.ScanJunkActivity
import com.test.basd.onecleanmaster.activity.SpeakerCleanerActivity
//import com.test.basd.quickclean.activity.VirusActivity
import com.test.basd.onecleanmaster.activity.photocompress.photo.StartCompressionPhotoActivity
import com.test.basd.onecleanmaster.databinding.FragmentLayoutHomeBinding
import com.test.basd.onecleanmaster.helps.BaseFragment
import com.test.basd.onecleanmaster.helps.KotlinExt.setOnClickListener
import com.test.basd.onecleanmaster.helps.KotlinExt.toFormatSize
import com.test.basd.onecleanmaster.notificationclean.NotificationCleanActivity
import com.test.basd.onecleanmaster.notificationclean.NotificationGuestActivity

class HomeFragment : BaseFragment<FragmentLayoutHomeBinding>() {

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

    override fun setView() {
        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()
    }

    override fun setListener() {
        super.setListener()
        listOf(binding.idCleanJunk, binding.idFlClean).setOnClickListener {
            startActivity(Intent(requireContext(), ScanJunkActivity::class.java))
        }
        binding.idVirusScan.setOnClickListener {
//            startActivity(Intent(requireActivity(), VirusActivity::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.llSpeaker.setOnClickListener {
            startActivity(Intent(requireContext(), SpeakerCleanerActivity::class.java))
        }
        binding.llSimilar.setOnClickListener {
            startActivity(Intent(requireContext(), RepeatedPhotoActivity::class.java))
        }
        binding.llNetwork.setOnClickListener {
            startActivity(Intent(requireContext(), NetWorkActivity::class.java))
        }
        binding.llNotification.setOnClickListener {
            if (SPUtils.getInstance().getBoolean("notification_guest", false)) {
                startActivity(Intent(requireActivity(), NotificationCleanActivity::class.java))
            } else {
                startActivity(Intent(requireActivity(), NotificationGuestActivity::class.java))
            }
        }
        binding.llRecent.setOnClickListener {
            startActivity(Intent(requireContext(), RecentAppActivity::class.java))
        }
        binding.llCompress.setOnClickListener {
            startActivity(Intent(requireContext(), StartCompressionPhotoActivity::class.java))
        }

    }

    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
    }

}