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.knife.largePhoto
import com.zxhy.hfilemanagermaster.knife.largeVideo
import com.zxhy.hfilemanagermaster.permission.alert
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

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 num = when (currentMode) {
                0 -> mediaSelectAdapter.getSelectSize()
                1 -> imageSelectorAdapter.getSelectSize()
                2 -> videoSelectorAdapter.getSelectSize()
                else -> 0
            }
            if (num == 0) return@setOnClickListener
            this.alert("当前选中${num}项,是否确认删除", "删除确认") {
                positiveButton("确定") { dialog ->
                    dialog.dismiss()
                }
                negativeButton("取消") { 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)
            }
        }
    }
}