package com.idle.wall.idleactivity

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.TextView.OnEditorActionListener
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import com.idle.wall.R
import com.idle.wall.databinding.ActivityIdleSearchBinding
import com.idle.wall.idleadapter.OnItemclick
import com.idle.wall.idleitem.ItemIdleList
import com.idle.wall.idlebean.IdleData
import com.idle.wall.idletool.Comutils
import com.idle.wall.idletool.Itemhelper
import com.idle.wall.idletool.ObectManager

class IdleSearchActivity : AppCompatActivity(), OnItemclick {
    private lateinit var adapterWallapper: ItemIdleList
    private lateinit var binding: ActivityIdleSearchBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityIdleSearchBinding.inflate(layoutInflater)
        setContentView(binding.root)
        Comutils.initFull(this, false)
        initList()

        // 返回按钮点击事件
        binding.imageviewBack.setOnClickListener {
            finish()
        }
    }

    private fun initList() {
        val height = Comutils.dp2Px(180)
        adapterWallapper = ItemIdleList(this, height, true)
        adapterWallapper.setInfoWallpaperListener(this)

        val recyclerItemHelper = Itemhelper(10, 10, 15)
        val gridLayoutManager = GridLayoutManager(this, 2)

        binding.resultRecycler.run {
            layoutManager = gridLayoutManager
            addItemDecoration(recyclerItemHelper)
            adapter = adapterWallapper
        }

        // 搜索框回车触发搜索
        binding.et.setOnEditorActionListener(OnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                startSearch()
                true
            } else {
                false
            }
        })

        // 搜索按钮点击触发搜索
        binding.btnSearch.setOnClickListener { startSearch() }
    }

    // 关闭软键盘
    private fun closeKeyboard() {
        val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(binding.et.windowToken, 0)
    }

    // 核心修复：替换错误的search方法为正确的searchByDescription
    private fun startSearch() {
        closeKeyboard()
        binding.searchPb.visibility = View.VISIBLE

        val keyword = binding.et.text.toString().trim() // 增加trim()，避免空格干扰

        if (keyword.isNotEmpty()) {
            val searchResult: List<IdleData> = ObectManager.search(keyword)

            binding.searchPb.visibility = View.GONE
            if (searchResult.isNotEmpty()) {
                showEmpty(false)
                adapterWallapper.setDataInfo(searchResult) // 刷新列表数据
            } else {
                showEmpty(true)
                Toast.makeText(this, getString(R.string.no_search_result), Toast.LENGTH_SHORT).show()
            }
        } else {
            binding.searchPb.visibility = View.GONE
            Toast.makeText(
                this,
                getString(R.string.please_input_keyword),
                Toast.LENGTH_SHORT
            ).show()
        }
    }

    // 显示/隐藏空页面
    private fun showEmpty(showEmpty: Boolean) {
        if (showEmpty) {
            binding.emptyLayout.visibility = View.VISIBLE
            binding.resultRecycler.visibility = View.GONE
        } else {
            binding.emptyLayout.visibility = View.GONE
            binding.resultRecycler.visibility = View.VISIBLE
        }
    }

    // 列表项点击事件：跳转到预览页
    override fun onItemClickAction(info: IdleData?) {
        info ?: return // 空值保护，避免空指针

        val intent = Intent(this, IdleWallActivity::class.java)
        intent.putExtra(Comutils.key_info, info)
        startActivity(intent)
    }
}