package com.base.filerecoveryrecyclebin.fragment

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.View
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.base.filerecoveryrecyclebin.activity.MainActivity
import com.base.filerecoveryrecyclebin.adapter.RecycleBinAdapter
import com.base.filerecoveryrecyclebin.bean.RecycleBinBean
import com.base.filerecoveryrecyclebin.databinding.FragmentRecycleBinBinding
import com.base.filerecoveryrecyclebin.help.BaseFragment
import com.base.filerecoveryrecyclebin.help.PermissionHelp.checkStorePermission
import com.base.filerecoveryrecyclebin.help.PermissionHelp.requestStorePermission
import com.base.filerecoveryrecyclebin.service.StayNotificationService
import com.base.filerecoveryrecyclebin.utils.LogEx
import com.base.filerecoveryrecyclebin.view.DialogViews.showGerPermission
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File

class RecycleBinFragment : BaseFragment<FragmentRecycleBinBinding>() {

    private val TAG = "RecycleBinFragment"
    private lateinit var handler: Handler
    private lateinit var recycleBinAdapter: RecycleBinAdapter

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

    override fun setView() {

        handler = Handler(requireActivity().mainLooper) { message ->
            if (message.what == 99) {
//                LogEx.logDebug(TAG, "媒体库更新了")
            }
            if (message.what == 100) {
//                LogEx.logDebug(TAG, "回收站数据更新了")
            }
            true
        }
        StayNotificationService.mainHandler = handler

        requireActivity().startService(Intent(requireActivity(), StayNotificationService::class.java).apply {
            this.action = StayNotificationService.MEDIA_OBSERVER_ACTION
        })
        requireActivity().startService(Intent(requireContext(), StayNotificationService::class.java).apply {
            this.action = StayNotificationService.FILE_OBSERVER_ACTION
            putExtra("isStart", true)
        })
        recycleBinAdapter = RecycleBinAdapter()
        binding.rv.adapter = recycleBinAdapter

        val launcher = (requireActivity() as MainActivity).launcher
        if (requireActivity().checkStorePermission()) {
            initData()
        } else {
            requireContext().showGerPermission(allow = {
                requireContext().requestStorePermission(launcher, result = { flag ->
                    if (flag) {
                        initData()
                    }
                })
            })
        }
    }

    private fun initData() {
        lifecycleScope.launch(Dispatchers.IO) {
            val file = requireActivity().filesDir
            val recycleBinDir = File(file, "RecycleBin")
            if (!recycleBinDir.exists()) {
                recycleBinDir.mkdirs()
            }
            val binList = recycleBinDir.listFiles()?.filter { it.name.contains(".bin") }
            val beanList = arrayListOf<RecycleBinBean>()
            binList?.forEach {
                val bean = Gson().fromJson(it.readText(), RecycleBinBean::class.java)
                beanList.add(bean)
            }
            launch(Dispatchers.Main) {
                binding.rv.isVisible = !binList.isNullOrEmpty()
                binding.llEmpty.isVisible = binList.isNullOrEmpty()
                recycleBinAdapter.setData(beanList)
            }

        }
    }


}