package com.base.superfilemanager.fragment

import android.content.Intent
import android.os.Build
import android.os.Environment
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.base.superfilemanager.R
import com.base.superfilemanager.activity.FileCategoryActivity
import com.base.superfilemanager.activity.MainActivity
import com.base.superfilemanager.adapter.FileCategoryAdapter
import com.base.superfilemanager.bean.FileCategoryBean
import com.base.superfilemanager.databinding.FragmentFileCategoryBinding
import com.base.superfilemanager.helps.BaseFragment
import com.base.superfilemanager.helps.file.MediaStoreEx
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.PermissionUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

class FileCategoryFragment : BaseFragment<FragmentFileCategoryBinding>() {


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

    private lateinit var docAdapter: FileCategoryAdapter
    private lateinit var mediaAdapter: FileCategoryAdapter
    private lateinit var recentAdapter: FileCategoryAdapter


    private val docList = listOf(
        FileCategoryBean(FileCategoryBean.ALL, R.drawable.all, FileCategoryBean.ALL),
        FileCategoryBean(FileCategoryBean.XLS, R.drawable.xls, FileCategoryBean.XLS),
        FileCategoryBean(FileCategoryBean.DOC, R.drawable.doc, FileCategoryBean.DOC),
        FileCategoryBean(FileCategoryBean.ZIP, R.drawable.zip, FileCategoryBean.ZIP),
        FileCategoryBean(FileCategoryBean.TXT, R.drawable.txt, FileCategoryBean.TXT),
        FileCategoryBean(FileCategoryBean.PPT, R.drawable.ppt, FileCategoryBean.PPT),
        FileCategoryBean(FileCategoryBean.PDF, R.drawable.pdf, FileCategoryBean.PDF),
    )
    private val mediaList = listOf(
        FileCategoryBean(FileCategoryBean.Video, R.drawable.video, FileCategoryBean.Video),
        FileCategoryBean(FileCategoryBean.Audio, R.drawable.audio, FileCategoryBean.Audio),
        FileCategoryBean(FileCategoryBean.APK, R.drawable.apk, FileCategoryBean.APK),
        FileCategoryBean(FileCategoryBean.Image, R.drawable.image, FileCategoryBean.Image),
    )


    override fun setView() {
        docAdapter = FileCategoryAdapter(FileCategoryAdapter.MODE_DOCUMENT, ::itemClick)
        binding.rvDocument.layoutManager = GridLayoutManager(requireContext(), 4).apply {
            scrollToPositionWithOffset(0, 0)
        }
        binding.rvDocument.adapter = docAdapter
        docAdapter.setData(docList)

        mediaAdapter = FileCategoryAdapter(FileCategoryAdapter.MODE_MEDIA, ::itemClick)
        binding.rvMedia.adapter = mediaAdapter
        mediaAdapter.setData(mediaList)

        binding.tvAuthorization.setOnClickListener {
            (requireActivity() as MainActivity).checkPermission(true)
        }
        binding.llAll.setOnClickListener {
            (requireActivity() as MainActivity).checkPermission(true)
        }
        recentAdapter = FileCategoryAdapter(FileCategoryAdapter.MODE_RECENT, ::itemClick)
        binding.rvRecent.adapter = recentAdapter
    }

    override fun onStart() {
        super.onStart()
        if (checkStorage()) {
            binding.llPermission.visibility = View.GONE
            binding.tvAuthorization.visibility = View.GONE
            binding.rvRecent.visibility = View.VISIBLE
            loadRecentImageVideo()
        }
    }

    private fun itemClick(key: String) {
        requireContext().startActivity(Intent(requireContext(), FileCategoryActivity::class.java).apply {
            putExtra("type", key)
        })
    }


    private fun checkStorage(): Boolean {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            Environment.isExternalStorageManager()
        } else {
            PermissionUtils.isGranted(PermissionConstants.STORAGE)
        }
    }

    fun refreshRecent() {
        if (isVisible) {
            binding.llPermission.visibility = View.GONE
            binding.rvRecent.visibility = View.VISIBLE
            loadRecentImageVideo()
        }
    }

    private fun loadRecentImageVideo() = lifecycleScope.launch(Dispatchers.Main) {
        val imageList = arrayListOf<FileCategoryBean>()
        async {
            val list = MediaStoreEx.recentImage(requireContext()).map { FileCategoryBean(url = it.toString()) }
            imageList.addAll(list)
        }.await()
        if (imageList.isEmpty()) {
            binding.llEmpty.visibility = View.VISIBLE
        } else {
            recentAdapter.setData(imageList)
        }
    }


}