package com.base.filerecoveryrecyclebin.fragment

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.base.filerecoveryrecyclebin.activity.MainActivity
import com.base.filerecoveryrecyclebin.activity.SettingActivity
import com.base.filerecoveryrecyclebin.activity.junkclean.ScanJunkActivity
import com.base.filerecoveryrecyclebin.activity.photomanager.PhotoManagerAnimationActivity
import com.base.filerecoveryrecyclebin.activity.recovery.FileScanResultActivity
import com.base.filerecoveryrecyclebin.activity.whatsapp.WhatsAppCleanerAnimationActivity
import com.base.filerecoveryrecyclebin.bean.ConstObject
import com.base.filerecoveryrecyclebin.bean.ConstObject.SCAN_AUDIOS
import com.base.filerecoveryrecyclebin.bean.ConstObject.SCAN_DOCUMENTS
import com.base.filerecoveryrecyclebin.bean.ConstObject.SCAN_PHOTOS
import com.base.filerecoveryrecyclebin.bean.ConstObject.SCAN_VIDEOS
import com.base.filerecoveryrecyclebin.databinding.FragmentHome3Binding
import com.base.filerecoveryrecyclebin.fcm.NotificationUtil.sendNotification
import com.base.filerecoveryrecyclebin.help.BaseFragment
import com.base.filerecoveryrecyclebin.help.KotlinExt.toFormatSize
import com.base.filerecoveryrecyclebin.utils.LogEx
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

class HomeFragment : BaseFragment<FragmentHome3Binding>() {
    private val TAG = "HomeFragment"

    override val binding: FragmentHome3Binding by lazy {
        FragmentHome3Binding.inflate(layoutInflater)
    }

    @SuppressLint("SetTextI18n")
    override fun setView() {
        binding.tvTitle.text = "Dumpster"
        binding.lottie.repeatCount = ValueAnimator.INFINITE
    }

    override fun setListener() {
        binding.flScan.setOnClickListener {
            startActivity(Intent(requireContext(), ScanJunkActivity::class.java))
//            sendNotification(requireContext(), ConstObject.ID_JUNK_CLEAN_PUSH)
        }
        binding.cardRyPhotos.setOnClickListener {
            startActivity(Intent(requireContext(), FileScanResultActivity::class.java).apply {
                putExtra("ScanType", SCAN_PHOTOS)
            })
        }
        binding.cardRyVideos.setOnClickListener {
            startActivity(Intent(requireContext(), FileScanResultActivity::class.java).apply {
                putExtra("ScanType", SCAN_VIDEOS)
            })
        }
        binding.cardRyDocuments.setOnClickListener {
            startActivity(Intent(requireContext(), FileScanResultActivity::class.java).apply {
                putExtra("ScanType", SCAN_DOCUMENTS)
            })
        }
        binding.cardRyAudios.setOnClickListener {
            startActivity(Intent(requireContext(), FileScanResultActivity::class.java).apply {
                putExtra("ScanType", SCAN_AUDIOS)
            })
        }
        binding.cardPhotoManager.setOnClickListener {
            startActivity(Intent(requireContext(), PhotoManagerAnimationActivity::class.java))
        }

        binding.cardWhatsapp.setOnClickListener {
            startActivity(Intent(requireContext(), WhatsAppCleanerAnimationActivity::class.java))
        }
        binding.flSetting.setOnClickListener {
            startActivity(Intent(requireContext(), SettingActivity::class.java))
        }
//        binding.ivVip.setOnClickListener {
//            startActivity(Intent(requireContext(), VipActivity::class.java))
//        }

        lifecycleScope.launch(Dispatchers.Main) {
            (requireActivity() as MainActivity).photoSizeFlow.collectLatest {
                LogEx.logDebug(TAG, "photoSizeFlow=$it")
                if (it == 0L) {
                    binding.tvPhotoSize.visibility = View.INVISIBLE
                } else {
                    binding.tvPhotoSize.visibility = View.VISIBLE
                    binding.tvPhotoSize.text = it.toFormatSize(0)
                }
            }
        }
    }

    override fun onPause() {
        super.onPause()
        binding.lottie.pauseAnimation()

    }

    override fun onResume() {
        super.onResume()
        binding.lottie.resumeAnimation()
    }

}