package com.webtoonviewer.ui

import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.ImageButton
import android.widget.ScrollView
import android.widget.Spinner
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.webtoonviewer.R
import com.webtoonviewer.db.Book
import com.webtoonviewer.repository.BookInformation
import com.webtoonviewer.repository.BookRepository
import com.webtoonviewer.utils.StatusBar.darkStatusBar
import kotlinx.coroutines.launch

class DescriptionActivity : AppCompatActivity() {
    private var bookRepository: BookRepository = BookRepository(this)
    private lateinit var book: Book
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_description)

        val id = intent.getIntExtra("id", 0)
        val description = intent.getStringExtra("description")
        val scrollView: ScrollView = findViewById(R.id.scrollView)
        val textView: TextView = findViewById(R.id.textViewDescriptionPage)
        textView.text = description
        val editText: EditText = findViewById(R.id.editTextDescriptionPage)
        editText.setText(textView.text)
        val spinner: Spinner = findViewById(R.id.spinnerDescriptionPage)
        val buttonSubmit: ImageButton = findViewById(R.id.btnDescriptionPageSubmit)

        textView.setOnLongClickListener {
            scrollView.visibility = View.GONE
            textView.visibility = View.GONE
            editText.visibility = View.VISIBLE
            spinner.visibility = View.VISIBLE
            buttonSubmit.visibility = View.VISIBLE
            true
        }

        editText.movementMethod = ScrollingMovementMethod()

        ArrayAdapter.createFromResource(
            this,
            R.array.tag_items, // 这里是你的字符串数组资源
            android.R.layout.simple_spinner_item
        ).also { adapter ->
            // 设置下拉菜单样式
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            // 将适配器应用于Spinner
            spinner.adapter = adapter
        }

        lifecycleScope.launch {
            book = bookRepository.getBookById(id)!!
            val tag = book.tag
            val position = getPositionFromArray(R.array.tag_items, tag)
            if (position != -1) {
                spinner.setSelection(position)
            }
        }

        spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
                val selectedItem = parent.getItemAtPosition(position).toString()
                Log.d("ithi", "onItemSelected: $selectedItem")
                // 处理选中的项
            }

            override fun onNothingSelected(parent: AdapterView<*>) {
                // 处理未选择任何项的情况
            }
        }

        buttonSubmit.setOnClickListener {
            lifecycleScope.launch {
                val info = BookInformation(
                    book.title,
                    book.author,
                    spinner.selectedItem.toString(),
                    editText.text.toString(),
                    book.img)
                bookRepository.updateDescriptionById(id, editText.text.toString())

                bookRepository.updateInformationById(id, info)
                scrollView.visibility = View.VISIBLE
                textView.visibility = View.VISIBLE
                editText.visibility = View.GONE
                spinner.visibility = View.GONE
                buttonSubmit.visibility = View.GONE
            }
        }

        val buttonClose: ImageButton = findViewById(R.id.btnDescriptionPageClose)
        buttonClose.setOnClickListener {
            finish() // 关闭当前 Activity
        }
    }

    private fun getPositionFromArray(arrayResId: Int, targetString: String): Int {
        val array = resources.getStringArray(arrayResId)
        return array.indexOf(targetString)
    }
}