package com.zxhy.simplefilesmanager.simplegrid

import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.zxhy.simplefilesmanager.R
import com.zxhy.simplefilesmanager.databinding.ActivitySimpleGridBinding
import com.zxhy.simplefilesmanager.simpledata.JoGridFileJo
import com.zxhy.simplefilesmanager.simplejoke.IntentEx.systemActionView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File

class SimpleGridActivity : AppCompatActivity() {

    private lateinit var binding: ActivitySimpleGridBinding
    private val viewModel: SimpleGridViewModel by viewModels()
    private lateinit var adapter: SimpleGridAdapter
    private var tittle: String = ""
    private val TAG = "SimpleGridActivity"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivitySimpleGridBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        tittle = intent.extras?.getString("Tittle") ?: ""
        binding.tvTittle.text = tittle
        binding.flBack.setOnClickListener { finish() }

        adapter = SimpleGridAdapter(tittle == "VIDEO", this,
            selectClick = { count ->
                if (count == 0) {
                    binding.tvItems.visibility = View.INVISIBLE
                } else {
                    binding.tvItems.visibility = View.VISIBLE
                    binding.tvItems.text = this.getString(R.string.select_d_items, count)
                }
            },
            itemClick = { path ->
                systemActionView(this, path, tittle)
            })
        binding.rv.adapter = adapter
        if (tittle == "IMAGE") {
            viewModel.loadImage(::onFistRefresh, ::onCompleteRefresh)
        }
        if (tittle == "VIDEO") {
            viewModel.loadVideo(::onFistRefresh, ::onCompleteRefresh)
        }

        binding.flDelete.setOnClickListener {
            val datas = adapter.getSelectData()
            val files = datas.map { it.toFile() }
            viewModel.deleteFiles(files) {
                lifecycleScope.launch(Dispatchers.Main) {
                    adapter.removeData(datas)
                }
            }
        }
    }

    private fun onFistRefresh(list: List<JoGridFileJo>) = lifecycleScope.launch(Dispatchers.Main) {
        if (list.isNotEmpty()) {
            binding.pb.visibility = View.GONE
            binding.rv.visibility = View.VISIBLE
            binding.flDelete.visibility = View.VISIBLE
            adapter.setData(list)
        }
    }

    private fun onCompleteRefresh(list: List<JoGridFileJo>) =
        lifecycleScope.launch(Dispatchers.Main) {
            if (list.isNotEmpty()) {
                binding.pb.visibility = View.GONE
                binding.rv.visibility = View.VISIBLE
                binding.flDelete.visibility = View.VISIBLE
                adapter.addData(list)
            }
        }

}