package com.develop.food.base.widgets

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.os.Build
import android.os.Handler
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.widget.RelativeLayout
import android.widget.Toast
import com.blankj.utilcode.util.KeyboardUtils
import android.view.inputmethod.InputMethodManager
import androidx.annotation.RequiresApi
import com.develop.base.R
import com.develop.base.databinding.HomeSortFilterSearchView1Binding
import com.develop.base.databinding.HomeSortFilterSearchViewBinding
import com.develop.food.base.utils.isNightTheme


class HomeFilterSortSearchView : RelativeLayout {
    private lateinit var binding: HomeSortFilterSearchViewBinding
    private lateinit var binding1: HomeSortFilterSearchView1Binding

    var onFunctionCallback: OnFunctionCallback? = null

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    init {
        val root = if (isNightTheme()) {
            View.inflate(context, R.layout.home_sort_filter_search_view_1, this)
        } else {
            View.inflate(context, R.layout.home_sort_filter_search_view, this)

        }
        if (isNightTheme()) {
            binding1 = HomeSortFilterSearchView1Binding.bind(root)
            binding1.ivBack.setOnClickListener {
                binding1.cardLayout.visibility = View.VISIBLE
                binding1.searchLayout.visibility = View.GONE
                KeyboardUtils.hideSoftInput(context as Activity)
                binding1.etSearch.setText("")
                onFunctionCallback?.onBackClick()
            }
            binding1.searchCardView.setOnClickListener {
                binding1.cardLayout.visibility = View.GONE
                binding1.searchLayout.visibility = View.VISIBLE
                onFunctionCallback?.onSearchClick()
            }
            binding1.filterCardView.setOnClickListener {
                onFunctionCallback?.onFilterClick(binding1.filterCardView)
            }
            binding1.sortCardView.setOnClickListener {
                onFunctionCallback?.onSortClick(binding1.sortCardView)
            }
            binding1.tvSearch.setOnClickListener {
                KeyboardUtils.hideSoftInput(context as Activity)
                val content = binding1.etSearch.text.toString()
                if (content.isNotEmpty()) {
                    onFunctionCallback?.onInputSearch(content)
                }
            }
        } else {
            binding = HomeSortFilterSearchViewBinding.bind(root)
            binding.ivBack.setOnClickListener {
                binding.cardLayout.visibility = View.VISIBLE
                binding.searchLayout.visibility = View.GONE
                KeyboardUtils.hideSoftInput(context as Activity)
                binding.etSearch.setText("")
                onFunctionCallback?.onBackClick()
            }
            binding.searchCardView.setOnClickListener {
                binding.cardLayout.visibility = View.GONE
                binding.searchLayout.visibility = View.VISIBLE
                onFunctionCallback?.onSearchClick()
            }
            binding.filterCardView.setOnClickListener {
                onFunctionCallback?.onFilterClick(binding.filterCardView)
            }
            binding.sortCardView.setOnClickListener {
                onFunctionCallback?.onSortClick(binding.sortCardView)
            }
            binding.tvSearch.setOnClickListener {
                KeyboardUtils.hideSoftInput(context as Activity)
                val content = binding.etSearch.text.toString()
                if (content.isNotEmpty()) {
                    onFunctionCallback?.onInputSearch(content)
                }
            }
        }
    }

    interface OnFunctionCallback {
        fun onFilterClick(filterView: View)
        fun onSortClick(sortView: View)
        fun onSearchClick()
        fun onInputSearch(searchContent: String)
        fun onBackClick()
    }

    fun updateFilterName(filterName: String) {
        if (isNightTheme()) {
            binding1.tvFilterName.text = filterName
        } else {
            binding.tvFilterName.text = filterName
        }

    }

    fun updateSortName(sortName: String) {
        if (isNightTheme()) {
            binding1.tvSortName.text = sortName
        } else {
            binding.tvSortName.text = sortName
        }

    }

    fun updateHotWord(hotWord: String) {
        if (isNightTheme()) {
            binding1.etSearch.setText(hotWord)
        } else {
            binding.etSearch.setText(hotWord)
        }
    }
}


