package com.zxhyis.tools.ui.home


import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Environment
import android.os.StatFs
import android.text.format.Formatter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.gif.GifDrawable
import com.bumptech.glide.request.target.ImageViewTarget
import com.bumptech.glide.request.transition.Transition
import com.zxhyis.tools.R
import com.zxhyis.tools.base.TopFragment
import com.zxhyis.tools.databinding.FragmentToolBinding
import com.zxhyis.tools.logic.setOnClick
import com.zxhyis.tools.logic.setStatusBarTextDark
import com.zxhyis.tools.logic.setStatusBarTextLight
import com.zxhyis.tools.logic.startActivity
import com.zxhyis.tools.ui.`fun`.BatteryInfoActivity
import com.zxhyis.tools.ui.`fun`.JunkCleanActivity
import com.zxhyis.tools.ui.`fun`.LargeFilesActivity
import com.zxhyis.tools.ui.`fun`.RecycleBinActivity
import com.zxhyis.tools.ui.`fun`.StorageInfoActivity

@SuppressLint("SetTextI18n")
class ToolFragment : TopFragment<FragmentToolBinding>() {

    override fun createBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentToolBinding {
        return FragmentToolBinding.inflate(inflater,container,false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        init()
    }

    private fun init(){

        Glide.with(this)
            .asGif()
            .load(R.drawable.start)
            .into(mK.ivStart)

        Glide.with(this)
            .asGif()
            .load(R.drawable.end)
            .into(mK.ivEnd)


        mK.ivBg.setOnClick {
            requireActivity().startActivity<StorageInfoActivity>()
        }

        mK.tvCleanSpace.setOnClick {
            mK.tvCleanSpace.setBackgroundResource(R.drawable.bg_shape_set_click)
            requireActivity().startActivity<JunkCleanActivity>()
        }
        mK.clBattery.setOnClick {
            mK.clBattery.setBackgroundResource(R.drawable.bg_shape_home_item1_click)
            requireActivity().startActivity<BatteryInfoActivity>()
        }

        mK.clBin.setOnClick {
//            mK.clBin.setBackgroundResource(R.drawable.bg_shape_home_item1_click)
//            requireActivity().startActivity<RecycleBinActivity>()
        }

        mK.clLargeFile.setOnClick {
            mK.clLargeFile.setBackgroundResource(R.drawable.bg_shape_home_item1_click)
            requireActivity().startActivity<LargeFilesActivity>()
        }
    }

    override fun onResume() {
        super.onResume()
        requireActivity().window.statusBarColor=ContextCompat.getColor(requireActivity(),R.color.tool_top)
        setStatusBarTextLight(requireActivity())
        initStorage()
        updateUi()
    }

    private fun updateUi(){
        mK.tvCleanSpace.setBackgroundResource(R.drawable.bg_shape_set)
        mK.clBattery.setBackgroundResource(R.drawable.bg_shape_home_item1)
        mK.clBin.setBackgroundResource(R.drawable.bg_shape_home_item1)
        mK.clLargeFile.setBackgroundResource(R.drawable.bg_shape_home_item1)
    }

    private fun initStorage(){
        val stat = StatFs(Environment.getExternalStorageDirectory().path)
        val totalSize = stat.totalBytes
        val availableSize = stat.availableBytes
        val usedSize = totalSize - availableSize
        val usedPercentage = usedSize.toFloat() / totalSize * 100
        mK.pbTool.progress = usedPercentage.toInt()
        mK.tvPercent.text= "${usedPercentage.toInt()}"
        mK.tvStorage.text=
            "${Formatter.formatFileSize(requireActivity(), usedSize)}/${Formatter.formatFileSize(requireActivity(), totalSize)}"
    }

}