package it.integry.integrywmsnative.ui.filter_chips;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import it.integry.integrywmsnative.core.di.BindableBoolean;
import it.integry.integrywmsnative.core.di.BindableString;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;

public abstract class FilterLayoutView extends BottomSheetDialogFragment {

    protected Context mContext;
    private final BindableString filterName = new BindableString();
    private final BindableBoolean enabledSearch = new BindableBoolean();

    private SearchView mSearchView;
    private RunnableArgs<String> mOnFilterChanged;


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if (mSearchView != null) this.initSearchView();
    }

    protected void initSearchView() {

        mSearchView.setOnSearchClickListener(v -> {
            this.setEnabledSearch(true);
            mSearchView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        });

        mSearchView.setOnCloseListener(() -> {
            setEnabledSearch(false);
            var layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);

            mSearchView.setLayoutParams(layoutParams);
            return false;
        });

        this.mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (mOnFilterChanged != null) mOnFilterChanged.run(newText);

                return false;
            }
        });

//        mSearchView.setQuery("", true);
//        mSearchView.setIconified(true);
//        mSearchView.clearFocus();
//
//        mSearchView.getRootView().invalidate();
    }


    public Context getContext() {
        return mContext;
    }

    public FilterLayoutView setContext(Context mContext) {
        this.mContext = mContext;
        return this;
    }

    public BindableString getFilterName() {
        return filterName;
    }

    public FilterLayoutView setFilterName(String filterName) {
        this.filterName.set(filterName);
        return this;
    }

    public BindableBoolean getEnabledSearch() {
        return enabledSearch;
    }

    public FilterLayoutView setEnabledSearch(boolean enabledSearch) {
        this.enabledSearch.set(enabledSearch);
        return this;
    }

    public FilterLayoutView setSearchView(SearchView searchView, RunnableArgs<String> onFilterChanged) {
        this.mSearchView = searchView;
        this.mOnFilterChanged = onFilterChanged;
        return this;
    }
}
