package com.zxhy.hfilemanagermaster.largefile

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.hfilemanagermaster.R
import com.example.hfilemanagermaster.databinding.ActivityLargeFileBinding
import com.zxhy.hfilemanagermaster.data.MediaDataC
import com.zxhy.hfilemanagermaster.dialog.showOperationDialog
import com.zxhy.hfilemanagermaster.knife.deleteDirectory
import com.zxhy.hfilemanagermaster.knife.largePhoto
import com.zxhy.hfilemanagermaster.knife.largeVideo
import com.zxhy.hfilemanagermaster.knife.toast
import com.zxhy.hfilemanagermaster.permission.alert
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.io.File

class LargeFileActivity : AppCompatActivity() {
    private lateinit var binding: ActivityLargeFileBinding
    private lateinit var mediaSelectAdapter: LagerFileSelectorAdapter
    private lateinit var imageSelectorAdapter: LargeImageSelectorAdapter
    private lateinit var videoSelectorAdapter: LargeVideoSelectorAdapter
    private var currentMode = 0//0 文件 1图片 2视频

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityLargeFileBinding.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
        }
        window.statusBarColor = ContextCompat.getColor(this, R.color.color_C2F300)
        binding.ivArrow.setOnClickListener {
            finish()
        }

        binding.tvDelete.setOnClickListener {
            val list = when (currentMode) {
                0 -> mediaSelectAdapter.getSelectData()
                1 -> imageSelectorAdapter.getSelectData()
                2 -> videoSelectorAdapter.getSelectData()
                else -> listOf()
            }
            val num = list.size
            if (num == 0) return@setOnClickListener
            this.alert("Currently selected $num items, are you sure to delete them") {
                positiveButton("confirm") { dialog ->
                    val files = list.map { File(it.path) }
                    deleteFiles(files)
                    dialog.dismiss()
                }
                negativeButton("cancel") { dialog ->
                    dialog.dismiss()
                }
                //禁止取消
                isCancel(false)
            }
        }
        binding.ivAll.setOnClickListener { view ->
            when (currentMode) {
                0 -> view.isSelected = mediaSelectAdapter.setToggleSelect()
                1 -> view.isSelected = imageSelectorAdapter.setToggleSelect()
                2 -> view.isSelected = videoSelectorAdapter.setToggleSelect()
            }
        }

        fileMode()

        binding.tvFile.setOnClickListener {
            fileMode()
        }
        binding.tvImage.setOnClickListener {
            imageMode()
        }
        binding.tvVideo.setOnClickListener {
            videoMode()
        }
    }


    private fun fileMode() {
        currentMode = 0
        binding.tvFile.setTextColor(ContextCompat.getColor(this, R.color.color_333333))
        binding.tvImage.setTextColor(ContextCompat.getColor(this, R.color.color_999999))
        binding.tvVideo.setTextColor(ContextCompat.getColor(this, R.color.color_999999))

        mediaSelectAdapter = LagerFileSelectorAdapter {
            binding.ivAll.isSelected = it
        }
        binding.rv.layoutManager = LinearLayoutManager(this)
        binding.rv.adapter = mediaSelectAdapter

        loadLargeFileData()
    }

    private fun loadLargeFileData() {
        lifecycleScope.launch(Dispatchers.IO) {
            val list = this@LargeFileActivity.largeVideo()
            lifecycleScope.launch(Dispatchers.Main) {
                mediaSelectAdapter.setData(list)
            }
        }
    }

    private fun imageMode() {
        currentMode = 1
        binding.tvFile.setTextColor(ContextCompat.getColor(this, R.color.color_999999))
        binding.tvImage.setTextColor(ContextCompat.getColor(this, R.color.color_333333))
        binding.tvVideo.setTextColor(ContextCompat.getColor(this, R.color.color_999999))

        imageSelectorAdapter = LargeImageSelectorAdapter {
            binding.ivAll.isSelected = it
        }
        binding.rv.layoutManager = GridLayoutManager(this, 3)
        binding.rv.adapter = imageSelectorAdapter
        loadLargeImageData()
    }

    private fun loadLargeImageData() {
        lifecycleScope.launch(Dispatchers.IO) {
            val list = this@LargeFileActivity.largePhoto()
            lifecycleScope.launch(Dispatchers.Main) {
                imageSelectorAdapter.setData(list)
            }
        }
    }

    private fun videoMode() {
        currentMode = 2
        binding.tvFile.setTextColor(ContextCompat.getColor(this, R.color.color_999999))
        binding.tvImage.setTextColor(ContextCompat.getColor(this, R.color.color_999999))
        binding.tvVideo.setTextColor(ContextCompat.getColor(this, R.color.color_333333))


        videoSelectorAdapter = LargeVideoSelectorAdapter {
            binding.ivAll.isSelected = it
        }
        binding.rv.layoutManager = GridLayoutManager(this, 3)
        binding.rv.adapter = videoSelectorAdapter

        loadLargeVideoData()
    }

    private fun loadLargeVideoData() {
        lifecycleScope.launch(Dispatchers.IO) {
            val list = this@LargeFileActivity.largeVideo()
            lifecycleScope.launch(Dispatchers.Main) {
                videoSelectorAdapter.setData(list)
            }
        }
    }

    private fun deleteFiles(files: List<File>) = Thread {
        try {
            files.forEach {
                if (it.exists()) {
                    if (it.isFile) it.delete() else deleteDirectory(it)
                }
            }
            runOnUiThread {
                lifecycleScope.launch(Dispatchers.Main) {
                    val dialog = showOperationDialog(R.mipmap.dads_889562, "delete success!") {}
                    delay(200)
                    dialog.dismiss()
                }
            }
            when (currentMode) {
                0 -> loadLargeFileData()
                1 ->  loadLargeImageData()
                2 ->  loadLargeVideoData()
            }
        } catch (e: Exception) {
            e.printStackTrace()
            runOnUiThread {
                toast("delete failed")
            }
        }
    }.start()
}