package com.easy.clean.ui.main

import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import androidx.lifecycle.lifecycleScope
import com.easy.clean.base.BaseFragment
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.PHOTO_COMPRESSION
import com.easy.clean.bean.FeatureBean.Companion.SCREENSHOT_CLEAN
import com.easy.clean.bean.FeatureBean.Companion.SIMILAR_PHOTOS
import com.easy.clean.bean.FeatureBean.Companion.VIDEO_CLEAN
import com.easy.clean.databinding.FragmentHome3Binding
import com.easy.clean.ui.dialog.permissionStorageJump
import com.easy.clean.utils.Utils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

class Home3Fragment : BaseFragment<FragmentHome3Binding>(FragmentHome3Binding::inflate) {

    override fun initView() {
        super.initView()
//        AdsMgr.showNative(binding.flAd, R.layout.layout_admob_native_custom)
        binding.circleProgress.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(JUNK_CLEAN)
        }
        getData()
    }

    override fun initListener() {
        binding.ivCleanJunk.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(JUNK_CLEAN)
        }
        binding.ivBatteryInfo.setOnClickListener {
            (requireActivity() as MainActivity).jumpAction(BATTERY_INFO)
        }
        binding.ivVideoClean.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(VIDEO_CLEAN)
        }
        binding.tvLargeFileClean.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(LARGE_FILE_CLEAN)
        }
        binding.tvPhotoCompress.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(PHOTO_COMPRESSION)
        }
        binding.tvScreenshotClean.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(SCREENSHOT_CLEAN)
        }
        binding.tvSimilarPhoto.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(SIMILAR_PHOTOS)
        }
    }

    fun getData() {
        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) {
                val spanned = SpannableStringBuilder(usedStorage + " / $totalStorage")
                spanned.setSpan(
                    ForegroundColorSpan(Color.parseColor("#F22525")),
                    0,
                    usedStorage.length,
                    Spanned.SPAN_EXCLUSIVE_INCLUSIVE
                )
                binding.tvUseSpace.text = spanned
                binding.circleProgress.setProgressWithAnimation(percent.toFloat(), 1000)
            }
        }
    }
}