package com.zxhy.simplefilesmanager.simpledup

import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.zxhy.simplefilesmanager.R
import com.zxhy.simplefilesmanager.databinding.ActivitySimpleDupBinding
import com.zxhy.simplefilesmanager.simpledata.JoGridFileJo
import com.zxhy.simplefilesmanager.simplejoke.JoActivityJoLauncher
import com.zxhy.simplefilesmanager.simplejoke.permission.JoPermissionJoCheckEx.jostorejoPermissionCheck
import com.zxhy.simplefilesmanager.simplejoke.permission.JoStoreJoPermissionEx.jorequestjoStoreFollow
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class SimpleDupActivity : AppCompatActivity() {

    private val viewModel: SimpleDupViewModel by viewModels()
    private lateinit var binding: ActivitySimpleDupBinding
    private lateinit var launcher: JoActivityJoLauncher
    private lateinit var adapter: SimpleDupAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        launcher = JoActivityJoLauncher(this)
        binding = ActivitySimpleDupBinding.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
        }

        initAdapter()
        if (!jostorejoPermissionCheck()) {
            showPermissionSet()
        } else {
            loadData()
        }
        binding.flBack.setOnClickListener {
            finish()
        }
    }

    private fun initAdapter() {
        adapter = SimpleDupAdapter { flag, count ->
            binding.ivAll.isSelected = flag
            if (count == 0) {
                binding.tvDelete.visibility = View.GONE
            } else {
                binding.tvDelete.visibility = View.VISIBLE
                binding.tvDelete.setOnClickListener {
                    val data = adapter.getSubList()
                    viewModel.deleteFiles(data.map { it.toFile() }) {
                        lifecycleScope.launch(Dispatchers.Main) {
                            adapter.removeData(data)
                            binding.tvDelete.visibility = View.GONE
                        }
                    }
                }
            }
        }
        binding.rvDup.adapter = adapter
        binding.flAll.setOnClickListener {
            binding.ivAll.isSelected = !binding.ivAll.isSelected
            adapter.toggleSelect(binding.ivAll.isSelected)
        }
    }

    private fun showPermissionSet() {
        binding.flPermissionSet.visibility = View.VISIBLE
        binding.tvSet.setOnClickListener {
            jorequestjoStoreFollow(launcher,
                disAgreeAction = {
                },
                agreeAction = {
                    binding.flPermissionSet.visibility = View.GONE
                    loadData()
                })
        }
    }

    private fun loadData() {
        binding.pb.visibility = View.VISIBLE
        viewModel.loadDup { list ->
            lifecycleScope.launch(Dispatchers.Main) {
                binding.pb.visibility = View.GONE
                adapter.setData(list)
            }
        }
    }
}