package com.base.appzxhy.ui.main

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.base.appzxhy.R
import com.base.appzxhy.base.BaseFragment
import com.base.appzxhy.base.goToAc
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.FragmentHome2Binding
import com.base.appzxhy.ui.dialog.permissionStorageJump
import com.base.appzxhy.ui.set.SettingActivity
import com.base.appzxhy.ui.similar.SimilarPhotosActivity
import com.base.appzxhy.utils.KotlinExt.toFormatSize
import com.base.appzxhy.utils.Utils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

class Home2Fragment : BaseFragment<FragmentHome2Binding>(FragmentHome2Binding::inflate) {

    override fun initView() {
        super.initView()
        AdsMgr.showNative(binding.flAd, R.layout.layout_admob_native_custom)

        initData()
    }

    override fun initListener() {
        super.initListener()
        binding.ivSet.setOnClickListener {
            goToAc(SettingActivity::class.java)
        }
        binding.flClean.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(JUNK_CLEAN)
        }
        binding.tvClean.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(JUNK_CLEAN)
        }
        binding.llClean.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(JUNK_CLEAN)
        }
        binding.llBattery.setOnClickListener {
            (requireActivity() as MainActivity).jumpAction(BATTERY_INFO)
        }
        binding.llLarge.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(LARGE_FILE_CLEAN)
        }
        binding.llDuplicate.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(SIMILAR_PHOTOS)
        }
        binding.llScreenshot.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(SCREENSHOT_CLEAN)
        }
        binding.llPhotoCompression.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(PHOTO_COMPRESSION)
        }
        binding.ivScrollBottom.setOnClickListener {
            binding.scrollView.smoothScrollTo(0, binding.scrollView.getChildAt(0).measuredHeight)
        }
    }

    @SuppressLint("SetTextI18n")
    private fun initData() {
        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) {
                binding.tvUsed.text = usedStorage
                binding.tvTotal.text = " / $totalStorage"
                binding.circleProgress.setProgressWithAnimation(percent.toFloat(), 1000)
            }
        }
    }

    override fun onResume() {
        super.onResume()

        if (SimilarPhotosActivity.similarSize != 0L) {
            binding.tvDupSize.visibility = View.VISIBLE
            binding.tvDupSize.text = SimilarPhotosActivity.similarSize.toFormatSize()
        } else {
            binding.tvDupSize.visibility = View.INVISIBLE
        }

    }
}