package com.develop.food.base.widgets

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import com.blankj.utilcode.util.KeyboardUtils
import com.develop.base.R
import com.develop.base.databinding.HomeSortFilterSearchViewBinding

class HomeFilterSortSearchView : RelativeLayout {
    private var binding: HomeSortFilterSearchViewBinding

    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 = View.inflate(context, R.layout.home_sort_filter_search_view, this)
        binding = HomeSortFilterSearchViewBinding.bind(root)
        binding.ivBack.setOnClickListener {
            binding.cardLayout.visibility = View.VISIBLE
            binding.searchLayout.visibility = View.GONE
            KeyboardUtils.hideSoftInput(context as Activity)
            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) {
        binding.tvFilterName.text = filterName
    }

    fun updateSortName(sortName: String) {
        binding.tvSortName.text = sortName
    }

    fun updateHotWord(hotWord: String) {
        binding.etSearch.setText(hotWord)
    }
}

