package com.ritoq.quickphone.ui.main

import androidx.core.graphics.Insets
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.ritoq.quickphone.R
import com.ritoq.quickphone.base.BaseFragment
import com.ritoq.quickphone.base.goToAc
import com.ritoq.quickphone.base.jumpAction
import com.ritoq.quickphone.bean.FeatureBean
import com.ritoq.quickphone.bean.FeatureBean.Companion.BATTERY_INFO
import com.ritoq.quickphone.bean.FeatureBean.Companion.DEVICE_INFO
import com.ritoq.quickphone.bean.FeatureBean.Companion.JUNK_CLEAN
import com.ritoq.quickphone.bean.FeatureBean.Companion.LARGE_FILE_CLEAN
import com.ritoq.quickphone.bean.FeatureBean.Companion.PHOTO_COMPRESSION
import com.ritoq.quickphone.bean.FeatureBean.Companion.SCREENSHOT_CLEAN
import com.ritoq.quickphone.bean.FeatureBean.Companion.SETTINGS
import com.ritoq.quickphone.bean.FeatureBean.Companion.SIMILAR_PHOTOS
import com.ritoq.quickphone.bean.FeatureBean.Companion.UNINSTALL_APP
import com.ritoq.quickphone.databinding.FragmentHome3Binding
import com.ritoq.quickphone.ui.dialog.permissionStorageJump
import com.ritoq.quickphone.ui.set.SettingActivity
import com.ritoq.quickphone.utils.BarUtils
import com.ritoq.quickphone.utils.MyAnimationUtils


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

    private val adapter = FeatureItemAdapter()

    private val featureList by lazy {

        listOf<FeatureBean>(
            FeatureBean(
                JUNK_CLEAN,
                R.string.quick_clean,
                R.drawable.icon_clean_h,
            ).apply {
                bg = R.drawable.beijing1
            },
            FeatureBean(
                BATTERY_INFO,
                R.string.battery_info,
                R.drawable.icon_battery_h,
            ).apply {
                bg = R.drawable.beijing2
            },
            FeatureBean(
                SIMILAR_PHOTOS,
                R.string.similar_photos,
                R.drawable.icon_similar_h,
            ).apply {
                bg = R.drawable.bejing3
            },
            FeatureBean(
                UNINSTALL_APP,
                R.string.uninstall_app,
                R.drawable.icon_app_uninstall_h,
            ).apply {
                bg = R.drawable.bejing4
            },

            FeatureBean(
                PHOTO_COMPRESSION,
                R.string.photo_compression,
                R.drawable.icon_compression_h,
            ).apply {
                bg = R.color.color_f5fff9
                span = 2
                showTitle = true
            },

            FeatureBean(
                LARGE_FILE_CLEAN,
                R.string.large_file_clean,
                R.drawable.icon_large_file_h,
            ).apply {
                bg = R.color.color_f7feff
                span = 2
            },

            FeatureBean(
                SCREENSHOT_CLEAN,
                R.string.screenshot_clean,
                R.drawable.icon_screenshot_h,
            ).apply {
                bg = R.color.color_f7feff
                span = 2
            },

//            FeatureBean(
//                DEVICE_INFO,
//                R.string.device_info,
//                R.drawable.icon_device_info_h,
//            ).apply {
//                bg = R.color.color_fff7fe
//                span = 2
//            },
        )
    }


    override fun initView() {
        super.initView()
        binding.flTop.setPadding(0, BarUtils.getStatusBarHeight(), 0, 0)
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
            val systemBars: Insets = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            binding.flTop.setPadding(0, systemBars.top, 0, 0)
            insets
        }

        val layoutManager = GridLayoutManager(requireContext(), 2)
        layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                val span = featureList[position].span
                return span
            }
        }
        binding.rv.layoutManager = layoutManager
        binding.rv.adapter = adapter
        adapter.submitList(featureList)

        MyAnimationUtils.rotation(binding.ivYuan)
    }

    override fun initListener() {
        super.initListener()
        binding.flClean.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(JUNK_CLEAN)
        }
        binding.ivSet.setOnClickListener {
            goToAc(SettingActivity::class.java)
        }

        adapter.itemClick = {


            when (it.key) {
                JUNK_CLEAN,
                SCREENSHOT_CLEAN,
                LARGE_FILE_CLEAN,
                PHOTO_COMPRESSION, SIMILAR_PHOTOS -> {
                    (requireActivity() as MainActivity).permissionStorageJump(it.key)
                }

                BATTERY_INFO, UNINSTALL_APP, DEVICE_INFO -> {
                    (requireActivity() as MainActivity).jumpAction(it.key)
                }


                SETTINGS -> {
                    (requireActivity() as MainActivity).goToAc(SettingActivity::class.java)
                }

            }

        }
    }

}