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

import android.content.Intent;
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.AdapterSearch;
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.helper.MyValue;
import com.hi.music.player.javabean.response.ResponseSearch;
import com.hi.music.player.javabean.response.child.ResponseSearchChild;
import com.hi.music.player.javabean.response.child.ResponseSingle;
import com.hi.music.player.ui.activity.CategoryListActivity;
import com.hi.music.player.ui.activity.PlayActivity;
import com.hi.music.player.ui.activity.ResultListActivity;
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();
        initResultRecycler();

    }

    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 initResultRecycler(){
        AdapterSearch adapterSearch = new AdapterSearch();
        adapterSearch.setHomeItemClickListener(this);
        Vb.recyclerResult.setLayoutManager(new LinearLayoutManager(requireContext()));
        Vb.recyclerResult.setAdapter(adapterSearch);
        vmSearch.result.observe(getViewLifecycleOwner(), new Observer<List<ResponseSearch>>() {
            @Override
            public void onChanged(List<ResponseSearch> responseSearches) {
                Vb.pbloading.setVisibility(View.GONE);
                if(responseSearches == null){
                    Vb.recyclerResult.setVisibility(View.GONE);
                    Vb.layoutError.setVisibility(View.VISIBLE);
                    CommonUtils.LogMsg("------------更新结果  null");
                    return;
                }
                Vb.recyclerResult.setVisibility(View.VISIBLE);
                Vb.layoutError.setVisibility(View.GONE);
                adapterSearch.setData(responseSearches);
            }
        });
    }

    private void startQuery(String query){
        Vb.pbloading.setVisibility(View.VISIBLE);
        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("");
        }
    }


    //点击搜索结果的play按钮
    @Override
    public void onClickSearchResultBest(ResponseSearch responseSearch) {
        String beastBrowserId = responseSearch.getBeastBrowserId();
        String beastVideoId = responseSearch.getBeastVideoId();
        String beastSongTitle = responseSearch.getBeastSongTitle();
        String beastSongTCovert = responseSearch.getBeastSongTCovert();
        String pageType = responseSearch.getPageType();

        if(beastVideoId!= null&&!beastVideoId.isEmpty()){
            CommonUtils.LogMsg("---------击搜索结果的play按钮--beastVideoId="+beastVideoId);

            ResponseSingle responseSingle = new ResponseSingle();
            responseSingle.setSongTitle(beastSongTitle);
            responseSingle.setSingerHead(beastSongTCovert);
            responseSingle.setVideoId(beastVideoId);
            responseSingle.setPlaylistId("");
            Intent intent = new Intent(requireActivity(), PlayActivity.class);
            intent.putExtra(MyValue.KEY_PLAY_ACTIVITY_SINGER, responseSingle);
            startActivity(intent);
        }else if(beastBrowserId!= null&&!beastBrowserId.isEmpty()){
            CommonUtils.LogMsg("---------击搜索结果的play按钮--pageType="+pageType);
            switch (pageType){
                case "MUSIC_PAGE_TYPE_PLAYLIST":
                case "MUSIC_PAGE_TYPE_ALBUM":
                    Intent intent1 = new Intent(requireActivity(), CategoryListActivity.class);
                    intent1.putExtra(MyValue.KEY_CATEGORY_LIST_TYPE, pageType);
                    intent1.putExtra(MyValue.KEY_CATEGORY_LIST_SINGER_NAME, responseSearch.getBeastSongDescribe());
                    intent1.putExtra(MyValue.KEY_CATEGORY_LIST_BROWSER_ID, beastBrowserId);
                    startActivity(intent1);
                    break;
                default:
                    Intent intent = new Intent(requireActivity(), ResultListActivity.class);
                    intent.putExtra(MyValue.KEY_SEARCH_RESULT_BROWSER_ID, beastBrowserId);
                    startActivity(intent);
                    break;
            }
      }
    }


    //点击搜索结果普通项
    @Override
    public void onClickSearchResult(ResponseSearchChild responseSearchChild) {
        String browserId = responseSearchChild.getBrowserId();
        String songVideoId = responseSearchChild.getSongVideoId();
        String songCovert = responseSearchChild.getSongCovert();
        String songTitle = responseSearchChild.getSongTitle();
        String songDescribe = responseSearchChild.getSongDescribe();

        if(browserId!= null&&!browserId.isEmpty()){
            String pageType = responseSearchChild.getPageType();
            CommonUtils.LogMsg("---------点击搜索结果--pageType="+pageType);
            switch (pageType){
                case "MUSIC_PAGE_TYPE_ARTIST":
                    Intent intent = new Intent(requireActivity(), ResultListActivity.class);
                    intent.putExtra(MyValue.KEY_SEARCH_RESULT_BROWSER_ID, browserId);
                    startActivity(intent);
                    break;
                default:
                    Intent intent1 = new Intent(requireActivity(), CategoryListActivity.class);
                    intent1.putExtra(MyValue.KEY_CATEGORY_LIST_TYPE, pageType);
                    intent1.putExtra(MyValue.KEY_CATEGORY_LIST_SINGER_NAME, songDescribe);
                    intent1.putExtra(MyValue.KEY_CATEGORY_LIST_BROWSER_ID, browserId);
                    startActivity(intent1);
                    break;
            }

        }else {
            CommonUtils.LogMsg("---------点击搜索结果--VideoId="+songVideoId);

            ResponseSingle responseSingle = new ResponseSingle();
            responseSingle.setSongTitle(songTitle);
            responseSingle.setSingerHead(songCovert);
            responseSingle.setVideoId(songVideoId);
            responseSingle.setPlaylistId("");
            Intent intent = new Intent(requireActivity(), PlayActivity.class);
            intent.putExtra(MyValue.KEY_PLAY_ACTIVITY_SINGER, responseSingle);
            startActivity(intent);
        }
    }
}