package com.base.appzxhy.ui.main

import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ConcatAdapter
import com.base.appzxhy.R
import com.base.appzxhy.base.BaseFragment
import com.base.appzxhy.base.jumpAction
import com.base.appzxhy.bean.FeatureBean
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.databinding.FragmentHomeBinding
import com.base.appzxhy.ui.dialog.permissionStorageJump
import com.base.appzxhy.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"))
                )
            }
        }
    }

}