package com.hi.music.player.ui.fragmnt;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.hi.music.player.adapter.AdapterSuggestion;
import com.hi.music.player.api.HomeItemClickListener;
import com.hi.music.player.databinding.FragmentSearchBinding;
import com.hi.music.player.helper.CommonUtils;
import com.hi.music.player.ui.fragmnt.viewmodel.VMSearch;

import java.util.List;


public class SearchFragment extends BaseFragment<FragmentSearchBinding> implements HomeItemClickListener, View.OnClickListener {


    private VMSearch vmSearch;

    @Override
    protected FragmentSearchBinding getFragmentVb() {
        return FragmentSearchBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
//        Vb.etSearch.requestFocus();
        vmSearch = getFragmentScopeViewModel(VMSearch.class);

        Vb.etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String input = s.toString();
                if (!input.isEmpty()) {
                    vmSearch.getSuggestion(input);
                }

            }
        });
        Vb.etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String s = v.getText().toString();
                    CommonUtils.LogMsg("------------v=" + s);
                    startQuery(s);
                    return true;
                } else
                    return false;
            }
        });
        initSuggestionRecycler();
    }

    private void initSuggestionRecycler() {
        AdapterSuggestion adapterSuggestion = new AdapterSuggestion();
        Vb.recyclerSuggestion.setLayoutManager(new LinearLayoutManager(requireContext()));
        Vb.recyclerSuggestion.setAdapter(adapterSuggestion);
        adapterSuggestion.setHomeItemClickListener(this);
        Vb.imCancel.setOnClickListener(this);
        vmSearch.suggestion.observe(getViewLifecycleOwner(), new Observer<List<String>>() {
            @Override
            public void onChanged(List<String> strings) {
                if (strings != null) {
                    adapterSuggestion.setData(strings);
                    CommonUtils.LogMsg("------------更新=");
                }

            }
        });
    }

    private void startQuery(String query){
        Vb.recyclerSuggestion.setVisibility(View.GONE);
        Vb.recyclerResult.setVisibility(View.VISIBLE);
        vmSearch.getSearchResult(query);
    }

    @Override
    public void onClickItemSuggestion(boolean isSearch, String data) {
        if (isSearch) {
            startQuery(data);
        } else {
            Vb.etSearch.setText(data);
        }
    }


    @Override
    public void onClick(View v) {
        if (v.equals(Vb.imCancel)) {
            Vb.etSearch.setText("");
        }
    }
}