package com.easy.clean.ui.main

import android.annotation.SuppressLint
import androidx.lifecycle.lifecycleScope
import com.easy.clean.R
import com.easy.clean.base.BaseFragment
import com.easy.clean.base.goToAc
import com.easy.clean.base.jumpAction
import com.easy.clean.bean.FeatureBean.Companion.BATTERY_INFO
import com.easy.clean.bean.FeatureBean.Companion.JUNK_CLEAN
import com.easy.clean.bean.FeatureBean.Companion.LARGE_FILE_CLEAN
import com.easy.clean.bean.FeatureBean.Companion.SCREENSHOT_CLEAN
import com.easy.clean.bean.FeatureBean.Companion.SIMILAR_PHOTOS
import com.easy.clean.business.ads.AdsMgr
import com.easy.clean.databinding.FragmentHome2Binding
import com.easy.clean.ui.dialog.permissionStorageJump
import com.easy.clean.ui.set.SettingActivity
import com.easy.clean.utils.Utils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

class Home2Fragment : BaseFragment<FragmentHome2Binding>(FragmentHome2Binding::inflate) {

    override fun initView() {
        super.initView()
        AdsMgr.showNative(binding.flAd, R.layout.layout_admob_native_custom)

        initData()
    }

    override fun initListener() {
        super.initListener()
        binding.ivSet.setOnClickListener {
            goToAc(SettingActivity::class.java)
        }
        binding.flClean.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(JUNK_CLEAN)
        }
        binding.tvClean.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(JUNK_CLEAN)
        }
        binding.llClean.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(JUNK_CLEAN)
        }
        binding.llBattery.setOnClickListener {
            (requireActivity() as MainActivity).jumpAction(BATTERY_INFO)
        }
        binding.llLarge.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(LARGE_FILE_CLEAN)
        }
        binding.llDuplicate.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(SIMILAR_PHOTOS)
        }
        binding.llScreenshot.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(SCREENSHOT_CLEAN)
        }
    }

    @SuppressLint("SetTextI18n")
    private fun initData() {
        lifecycleScope.launch(Dispatchers.IO) {
            val totalStorageSize = async { Utils.getTotalStorageSize() }.await()
            val availableStorageSize = async { Utils.getAvailableStorageSize() }.await()
            val usedStorageSize = totalStorageSize - availableStorageSize
            val totalStorage = Utils.getSizeFormat(totalStorageSize)
            val usedStorage = Utils.getSizeFormat(usedStorageSize)
            val percent = (usedStorageSize * 100 / totalStorageSize).toInt()

            launch(Dispatchers.Main) {
                binding.tvUsed.text = usedStorage
                binding.tvTotal.text = " / $totalStorage"
                binding.circleProgress.setProgressWithAnimation(percent.toFloat(), 1000)
            }
        }
    }
}