package com.base.appzxhy.ui.main

import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.SpannedString
import android.text.style.ForegroundColorSpan
import androidx.lifecycle.lifecycleScope
import com.base.appzxhy.R
import com.base.appzxhy.base.BaseFragment
import com.base.appzxhy.base.jumpAction
import com.base.appzxhy.bean.FeatureBean.Companion.BATTERY_INFO
import com.base.appzxhy.bean.FeatureBean.Companion.JUNK_CLEAN
import com.base.appzxhy.bean.FeatureBean.Companion.LARGE_FILE_CLEAN
import com.base.appzxhy.bean.FeatureBean.Companion.PHOTO_COMPRESSION
import com.base.appzxhy.bean.FeatureBean.Companion.SCREENSHOT_CLEAN
import com.base.appzxhy.bean.FeatureBean.Companion.SIMILAR_PHOTOS
import com.base.appzxhy.business.ads.AdsMgr
import com.base.appzxhy.databinding.FragmentHome3Binding
import com.base.appzxhy.ui.dialog.permissionStorageJump
import com.base.appzxhy.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)


        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 {
            //TODO (视频清理)
            (requireActivity() as MainActivity).permissionStorageJump(JUNK_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)
            }
        }
    }
}