package com.easy.clean.ui.main

import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ConcatAdapter
import com.easy.clean.R
import com.easy.clean.base.BaseFragment
import com.easy.clean.base.jumpAction
import com.easy.clean.bean.FeatureBean
import com.easy.clean.bean.FeatureBean.Companion.BATTERY_INFO
import com.easy.clean.bean.FeatureBean.Companion.JUNK_CLEAN
import com.easy.clean.bean.FeatureBean.Companion.LARGE_FILE_CLEAN
import com.easy.clean.bean.FeatureBean.Companion.PHOTO_COMPRESSION
import com.easy.clean.bean.FeatureBean.Companion.SCREENSHOT_CLEAN
import com.easy.clean.bean.FeatureBean.Companion.SIMILAR_PHOTOS
import com.easy.clean.databinding.FragmentHomeBinding
import com.easy.clean.ui.dialog.permissionStorageJump
import com.easy.clean.utils.Utils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch


class HomeFragment : BaseFragment<FragmentHomeBinding>(FragmentHomeBinding::inflate) {

    private val concatAdapter = ConcatAdapter()
    private val featureList = listOf(
        FeatureBean(JUNK_CLEAN, R.string.junk_clean, R.drawable.h_junkclean).apply { span = 1 },
        FeatureBean(BATTERY_INFO, R.string.battery_info, R.drawable.h_battery).apply { span = 1 },
        FeatureBean(LARGE_FILE_CLEAN, R.string.large_file_clean, R.drawable.h_lagre).apply { span = 1 },
        FeatureBean(SCREENSHOT_CLEAN, R.string.screenshot_clean, R.drawable.h_screenshot).apply { span = 3 },
        FeatureBean(PHOTO_COMPRESSION, R.string.photo_compression, R.drawable.h_compression).apply { span = 3 },
        FeatureBean(SIMILAR_PHOTOS, R.string.similar_photos, R.drawable.h_similar).apply { span = 3 },
    )

    override fun initView() {
        super.initView()
        binding.rv.adapter = concatAdapter
        concatAdapter.addAdapter(YuanAdapter2().apply {
            submitList(listOf(arrayOf("", "")))
            cleanClick = {
                (requireActivity() as MainActivity).permissionStorageJump(JUNK_CLEAN)
            }
            initData(this)
        })

        concatAdapter.addAdapter(FeatureAdapter().apply {
            submitList(listOf(featureList))
            itemClick = { bean ->
                if (bean.key == BATTERY_INFO) {
                    requireActivity().jumpAction(bean.key)
                } else {
                    requireActivity().permissionStorageJump(bean.key)
                }
            }
        })

    }

    override fun initListener() {
        super.initListener()
    }

    private fun initData(adapter: YuanAdapter2) {
        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) {
                adapter.submitList(
                    listOf(arrayOf(usedStorage, totalStorage, "$percent"))
                )
            }
        }
    }

}