package com.webtoonviewer.ui

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.ImageButton
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.webtoonviewer.R
import com.webtoonviewer.storage.StorageManager
import com.webtoonviewer.ui.adapter.FileAdapter
import com.webtoonviewer.ui.data.FileItem
import java.io.File

class DirectoryActivity : AppCompatActivity() {

    private lateinit var recyclerView: RecyclerView
    private lateinit var fileAdapter: FileAdapter
    private lateinit var titleTextView: TextView
    private lateinit var sumTextView: TextView
    private var currentDir: FileItem = FileItem("", true, "")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_directory)

        titleTextView = findViewById(R.id.textViewDirPageTitle)
        sumTextView = findViewById(R.id.textViewDirPageSum)
        updateTopBar()

        recyclerView = findViewById(R.id.recyclerViewDirectoryList)
        // recyclerView.layoutManager = GridLayoutManager(this, 3)
        recyclerView.layoutManager = LinearLayoutManager(this)

        val fileItems = StorageManager.getFilesAndDirectory(this, currentDir.path)
        fileAdapter = FileAdapter(fileItems) { fileItem ->
            if (fileItem.isDirectory) {
                val newFileItems = StorageManager.getFilesAndDirectory(this, fileItem.path)
                if (newFileItems.size > 0) {
                    fileAdapter.updateFiles(newFileItems)
                    currentDir = fileItem
                    updateTopBar()
                }
            } else {
                // Handle file click
                Log.d("ithi", "文件点击: ${fileItem.name}")
            }
        }
        recyclerView.adapter = fileAdapter

        val btnBack: ImageButton = findViewById(R.id.btnDirPageBack)
        btnBack.setOnClickListener {
            if (currentDir.name == "" || currentDir.name == "files") {
                finish()
            } else {
                val newPath: String = File(currentDir.path).parent ?: ""
                val newName: String = File(currentDir.path).parentFile?.name ?: ""
                val newFileItems = StorageManager.getFilesAndDirectory(this, newPath)
                fileAdapter.updateFiles(newFileItems)
                currentDir = FileItem(newName, true, newPath)
                updateTopBar()
            }
        }

        val btnSubmit: Button = findViewById(R.id.btnDirPageSubmit)
        btnSubmit.setOnClickListener {
            fileAdapter.toggleCheckboxesVisibility()
            btnSubmit.visibility = View.GONE
            val selectedFiles = fileAdapter.getSelectedFiles()
            // 在这里处理选中的文件
            selectedFiles.forEach { file ->
                StorageManager.deleteDirectory(this, file.path)
            }
            val newFileItems = StorageManager.getFilesAndDirectory(this, currentDir.path)
            fileAdapter.updateFiles(newFileItems)
        }

        val btnDelete: ImageButton = findViewById(R.id.btnDirPageDelete)
        btnDelete.setOnClickListener {
            if (currentDir.name != "" && currentDir.name != "files") {
                fileAdapter.toggleCheckboxesVisibility()
                btnSubmit.visibility = View.VISIBLE
            }
        }
    }

    private fun updateTopBar() {
        titleTextView.text = if (currentDir.name == "" || currentDir.name == "files") {
            "内部存储目录"
        } else {
            currentDir.name
        }

        sumTextView.text = StorageManager.getFilesAndDirectorySum(this, currentDir.path).toString()
    }

}