package com.base.filerecoveryrecyclebin.activity.recyclebin

import android.content.Intent
import android.os.Build
import android.os.Environment
import android.os.Handler
import androidx.annotation.RequiresApi
import androidx.lifecycle.lifecycleScope
import com.base.filerecoveryrecyclebin.adapter.RecycleBinAdapter
import com.base.filerecoveryrecyclebin.bean.RecycleBinBean
import com.base.filerecoveryrecyclebin.databinding.ActivityRecycleBinBinding
import com.base.filerecoveryrecyclebin.help.BaseActivity
import com.base.filerecoveryrecyclebin.help.PermissionHelp.checkStorePermission
import com.base.filerecoveryrecyclebin.help.PermissionHelp.requestStorePermission
import com.base.filerecoveryrecyclebin.service.StayNotificationService
import com.base.filerecoveryrecyclebin.service.StayNotificationService.Companion.FILE_OBSERVER_ACTION
import com.base.filerecoveryrecyclebin.service.StayNotificationService.Companion.MEDIA_OBSERVER_ACTION
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
import java.io.File

@RequiresApi(Build.VERSION_CODES.Q)
class RecycleBinActivity : BaseActivity<ActivityRecycleBinBinding>() {
    private val TAG = "RecycleBinActivity"

    private lateinit var handler: Handler
    private lateinit var recycleBinAdapter: RecycleBinAdapter

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


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

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

        if (checkStorePermission()) {
            initData()
        } else {
            showGerPermission(allow = {
                requestStorePermission(launcher, result = { flag ->
                    if (flag) {
                        initData()
                    }
                })
            })
        }
    }

    override fun initListener() {
        super.initListener()
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.swipeRefreshLayout.isRefreshing = true
            initData()
            lifecycleScope.launch {
                delay(1500)
                binding.swipeRefreshLayout.isRefreshing = false
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        startService(Intent(FILE_OBSERVER_ACTION).apply {
            putExtra("isStart", false)
        })
    }

    private fun initData() {
        lifecycleScope.launch(Dispatchers.IO) {
            val file = this@RecycleBinActivity.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) {
                recycleBinAdapter.setData(beanList)
            }

        }
    }

}