package com.dumpster.cleaner.ui.main

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.dumpster.cleaner.R
import com.dumpster.cleaner.base.BaseFragment
import com.dumpster.cleaner.base.goToAc
import com.dumpster.cleaner.base.jumpAction
import com.dumpster.cleaner.bean.FeatureBean.Companion.BATTERY_INFO
import com.dumpster.cleaner.bean.FeatureBean.Companion.JUNK_CLEAN
import com.dumpster.cleaner.bean.FeatureBean.Companion.LARGE_FILE_CLEAN
import com.dumpster.cleaner.bean.FeatureBean.Companion.PHOTO_COMPRESSION
import com.dumpster.cleaner.bean.FeatureBean.Companion.SCREENSHOT_CLEAN
import com.dumpster.cleaner.bean.FeatureBean.Companion.SIMILAR_PHOTOS
import com.dumpster.cleaner.business.ads.AdsMgr
import com.dumpster.cleaner.databinding.FragmentHome2Binding
import com.dumpster.cleaner.ui.dialog.permissionStorageJump
import com.dumpster.cleaner.ui.set.SettingActivity
import com.dumpster.cleaner.ui.similar.SimilarPhotosActivity
import com.dumpster.cleaner.utils.KotlinExt.toFormatSize
import com.dumpster.cleaner.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
        }

    }
}