package com.base.appzxhy.ui.main


import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.core.graphics.toColorInt
import androidx.lifecycle.lifecycleScope
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.ANTIVIRUS
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.SETTINGS
import com.base.appzxhy.bean.FeatureBean.Companion.SIMILAR_PHOTOS
import com.base.appzxhy.bean.FeatureBean.Companion.getFunctionTodayUsed
import com.base.appzxhy.databinding.FragmentHomeBinding
import com.base.appzxhy.ui.dialog.permissionStorageJump
import com.base.appzxhy.ui.malware.MalwareDialog
import com.base.appzxhy.ui.set.SettingActivity
import com.base.appzxhy.utils.MyAnimationUtils
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 adapter = FeatureItemAdapter()

    private val featureList by lazy {

        listOf<FeatureBean>(
            FeatureBean(
                JUNK_CLEAN,
                R.string.clean_junk,
                R.drawable.icon_clean,
                R.string.make_your_phone_clean,
                "#FFF7F2".toColorInt()
            ),
            FeatureBean(
                ANTIVIRUS,
                R.string.antivirus,
                R.drawable.icon_antivirus,
                R.string.mobile_antivirus_protection,
                "#F2FFF7".toColorInt()
            ),
            FeatureBean(
                SCREENSHOT_CLEAN,
                R.string.screenshot_clean,
                R.drawable.icon_screenshot,
                R.string.clear_phone_screenshot,
                "#FFFCF2".toColorInt()
            ),
            FeatureBean(
                LARGE_FILE_CLEAN,
                R.string.large_file_clean,
                R.drawable.icon_large_file_clean,
                R.string.clear_large_files_on_the_phone,
                "#F2FFF7".toColorInt()
            ),
            FeatureBean(
                SIMILAR_PHOTOS,
                R.string.similar_photos,
                R.drawable.icon_similar,
                R.string.clear_similar_pictures_on_the_phone,
                "#FBF7FF".toColorInt()
            ),
            FeatureBean(
                PHOTO_COMPRESSION,
                R.string.image_compression,
                R.drawable.icon_compression,
                R.string.compress_mobile_phone_images,
                "#FFF7F2".toColorInt()
            ),
            FeatureBean(
                BATTERY_INFO,
                R.string.battery_info,
                R.drawable.icon_battery,
                R.string.view_battery_information,
                "#F2FFF7".toColorInt()
            ),
            FeatureBean(
                SETTINGS,
                R.string.settings,
                R.drawable.icon_set,
                R.string.show_all_settings,
                "#FBF7FF".toColorInt()
            ),
        )
    }

    override fun initView() {
        super.initView()
        (requireActivity() as MainActivity).insetsCallBack = { insets ->
            binding.flTop.setPadding(0, insets.top, 0, 0)
        }
        binding.rv.adapter = adapter
        startAnimation()

        initData()
    }

    override fun initListener() {
        super.initListener()

        binding.tvClean.setOnClickListener {
            (requireActivity() as MainActivity).permissionStorageJump(JUNK_CLEAN)
        }

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

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


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

                ANTIVIRUS -> {
                    val dialog = MalwareDialog(requireActivity())
                    dialog.action = {
                        (requireActivity() as MainActivity).permissionStorageJump(it.key)
                    }
                    dialog.showDialog()

                }

            }
        }
    }

    private fun startAnimation() {

        MyAnimationUtils.rotation(binding.ivYuan, 2000)

        binding.tvJunkFile.alpha = 0f
        ValueAnimator.ofFloat(0f, 1f).apply {
            this.duration = 1000
            this.startDelay = 1200
            addUpdateListener { animation ->
                val value = animation.animatedValue as Float
                binding.tvJunkFile.alpha = value
            }
            start()
        }

        val top = requireActivity().resources.getDimension(R.dimen.dp_6)
        ObjectAnimator.ofFloat(
            binding.ivUp,
            View.TRANSLATION_Y,
            0f,
            top
        ).apply {
            duration = 1000
            interpolator = LinearInterpolator()
            repeatCount = ObjectAnimator.INFINITE
            repeatMode = ValueAnimator.REVERSE
            start()
        }

    }

    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
            }
        }
    }

    override fun onResumeOneShoot() {
        super.onResumeOneShoot()
        changeListTodayUsed()
    }

    @SuppressLint("NotifyDataSetChanged")
    fun changeListTodayUsed() {
        featureList[0].todayUsed = getFunctionTodayUsed(JUNK_CLEAN)
        featureList[1].todayUsed = getFunctionTodayUsed(ANTIVIRUS)
        adapter.submitList(featureList)
    }
}
