package com.base.filerecoveryrecyclebin.fragment

import android.content.Intent
import android.os.Build
import android.os.Handler
import androidx.annotation.RequiresApi
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.base.filerecoveryrecyclebin.activity.MainActivity
import com.base.filerecoveryrecyclebin.activity.recyclebin.RecycleBinDetailActivity
import com.base.filerecoveryrecyclebin.activity.recyclebin.RecycleBinFileEx.getRecycleBinDir
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.utils.LogEx
import com.base.filerecoveryrecyclebin.view.DialogViews.showGerPermission
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

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

    @RequiresApi(Build.VERSION_CODES.Q)
    override fun setView() {

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


        recycleBinAdapter = RecycleBinAdapter { bean ->
            startActivity(Intent(requireContext(), RecycleBinDetailActivity::class.java).apply {
                putExtra("Json", Gson().toJson(bean))
            })
        }
        binding.rv.adapter = recycleBinAdapter

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

    @RequiresApi(Build.VERSION_CODES.Q)
    override fun setListener() {
        super.setListener()
        binding.flGengduo.setOnClickListener {

        }
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.swipeRefreshLayout.isRefreshing = true
            initData()
            lifecycleScope.launch(Dispatchers.Main) {
                delay(1000)
                binding.swipeRefreshLayout.isRefreshing = false
            }
        }
    }

    @RequiresApi(Build.VERSION_CODES.Q)
    private fun initData() {
        lifecycleScope.launch(Dispatchers.IO) {

            val binList = getRecycleBinDir().listFiles()?.filter { it.name.contains(".bin") }
            val beanList = arrayListOf<RecycleBinBean>()
            binList?.forEach {
                LogEx.logDebug(TAG, it.absolutePath)
                val bean = Gson().fromJson(it.readText(), RecycleBinBean::class.java)
                beanList.add(bean)
            }
            val deleteFile = beanList.filter { it.deleted }
//            val deleteFile = beanList
            launch(Dispatchers.Main) {
                binding.rv.isVisible = deleteFile.isNotEmpty()
                binding.llEmpty.isVisible = deleteFile.isEmpty()
                recycleBinAdapter.setData(deleteFile)
            }

        }
    }

    @RequiresApi(Build.VERSION_CODES.Q)
    override fun onResume() {
        super.onResume()
        LogEx.logDebug(TAG, "onResume")
        if (requireContext().checkStorePermission()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                initData()
            }
        }
    }

    override fun onPause() {
        super.onPause()
        LogEx.logDebug(TAG, "onPause")
    }


}