package com.yidian.shenghuoquan.newscontent.utils

import android.content.Context
import android.os.Environment
import java.io.File
import java.math.BigDecimal

class DataCleanManager {
    companion object{
        fun getTotalCacheSizeShow(context: Context): String{
            val cacheSize = getTotalCacheSize(context)
            return getFormatSize(cacheSize.toDouble())
        }

        fun clearAllCache(context: Context){
            cleanCache(context)
            val cacheSize = getTotalCacheSize(context)
            if(cacheSize > 0){
                clearAllCache(context)
            }
        }

        private fun cleanCache(context: Context){
            deleteDir(context.cacheDir)
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                context.externalCacheDir?.let { deleteDir(it) }
            }
        }

        private fun getTotalCacheSize(context: Context): Long{
            var cacheSize = getFolderSize(context.cacheDir)
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                cacheSize += getFolderSize(context.externalCacheDir)
            }
            return cacheSize
        }

        private fun deleteDir(dir: File): Boolean{
            if(dir.isDirectory){
                val children = dir.list()
                repeat(children.size){
                    return deleteDir(File(dir, children[it]))
                }
            }
            return dir.delete()
        }

        private fun getFolderSize(file: File?): Long{
            var size: Long = 0
            try {
                val fileList = file?.listFiles()
                fileList?.size?.let { length ->
                    repeat(length){
                        size += if(fileList[it].isDirectory){
                            getFolderSize(fileList[it])
                        }else{
                            fileList[it].length()
                        }
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return size
        }

        private fun getFormatSize(size: Double): String{
            val kiloByte = size / 1024
            if(kiloByte < 1){
                return "0K"
            }

            val megaByte = kiloByte / 1024
            if(megaByte < 1){
                val result1 = BigDecimal(kiloByte)
                return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "K"
            }

            val gigaByte = megaByte / 1024
            if(gigaByte < 1){
                val result2 = BigDecimal(megaByte)
                return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "M"
            }

            val teraBytes = gigaByte / 1024
            if(teraBytes < 1){
                val result3 = BigDecimal(gigaByte)
                return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "G"
            }

            val result4 = BigDecimal(teraBytes)
            return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "T"
        }
    }
}