package com.wall.snap.wallpaper.acctivitysnap

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.wall.snap.wallpaper.R
//import com.wall.snap.wallpaper.databinding.ActivitySearchBinding
import com.wall.snap.wallpaper.toolsnap.ComUtilssnap
import com.wall.snap.wallpaper.toolsnap.ObectBoxManagersnap
import com.wall.snap.wallpaper.toolsnap.ItemHelpersnap
import com.wall.snap.wallpaper.connectorsnap.OnItemsnapClick
import com.wall.snap.wallpaper.databinding.ActivitySearchsnapBinding
import com.wall.snap.wallpaper.mysnapbean.MysnapData
import com.wall.snap.wallpaper.itemsnap.ItemsnapList

class SearchsnapActivity : AppCompatActivity() ,
    OnItemsnapClick {
    private lateinit var adapterWallapper: ItemsnapList
    private lateinit var binding: ActivitySearchsnapBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySearchsnapBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ComUtilssnap.initFull(this, false)
        initList()
        binding.imageviewBack.setOnClickListener {
            finish()
        }
    }


    private fun initList() {
        val height = ComUtilssnap.dp2Px(180)
        adapterWallapper =
            ItemsnapList(this, height, true)
        adapterWallapper.setInfoWallpaperListener(this)
        val recyclerItemHelpertwo = ItemHelpersnap(10, 10, 15)
        val gridLayoutManager = GridLayoutManager(this, 2)
        binding.resultRecycler.run {
            layoutManager = gridLayoutManager
            addItemDecoration(recyclerItemHelpertwo)
            adapter = adapterWallapper
        }

        binding.et.setOnEditorActionListener(OnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                startSearch()
                return@OnEditorActionListener true
            }
            false
        })
        binding.btnSearch.setOnClickListener { startSearch() }
    }

    private fun closeKeyboard() {
        val imm =  getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(binding.et.windowToken, 0)
    }

    private fun startSearch() {
        closeKeyboard()
        binding.searchPb.visibility = View.VISIBLE
        val string: String = binding.et.text.toString()
        if (string.isNotEmpty()) {
            val search = ObectBoxManagersnap.search(string)
            if (search.size > 0) {
                showEmpty(false)
                adapterWallapper.setDataInfo(search)
                binding.searchPb.visibility = View.GONE
            } else {
                binding.searchPb.visibility = View.GONE
                showEmpty(true)
            }

        } 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: MysnapData?) {
        val intent = Intent(this, WallpapersnapActivity::class.java)
        intent.putExtra(ComUtilssnap.key_info, info)
        startActivity(intent)
    }
}