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.base.BaseFragment
import com.base.appzxhy.databinding.FragmentHome3Binding
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()


        getData()
    }

    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)
            }
        }
    }
}