package com.android.chmo.ui.activity.model;

import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.android.chmo.R;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.LabelListRes;
import com.android.chmo.http.service.ModelService;
import com.android.chmo.model.LabelInfo;
import com.android.chmo.ui.adpater.LabelAdapter;
import com.android.chmo.utils.CommonUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class SearchModelActivity extends BaseActivity {
    @BindView(R.id.searchEdit)
    EditText searchEdit;
    @BindView(R.id.gridview)
    GridView gridView;

    private LabelAdapter labelAdapter;

    private List<LabelInfo> mList = new ArrayList<>();

    @OnClick(R.id.cancel)
    public void cancel() {
        close();
    }
    @Override
    protected int getContentResId() {
        return R.layout.activity_search_model;
    }

    @Override
    public void initView() {
        labelAdapter = new LabelAdapter(this);
        gridView.setAdapter(labelAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                labelSearch(mList.get(position));
            }
        });

        searchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {//搜索按键action
                    editSearch();
                    return true;
                }
                return false;
            }
        });

        getLabels();
    }

    private void getLabels() {
        showLoading();
        ModelService.getAllSkills(new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                hideLoading();
                LabelListRes res = new Gson().fromJson(result,LabelListRes.class);
                if (res.data != null) {
                    mList = res.data;
                    labelAdapter.setList(mList);
                }
            }

            @Override
            public void onFailure(String error) {
                hideLoading();
            }
        });
    }

    private void editSearch() {
        CommonUtils.closeKeybord(searchEdit,this);
        String search = searchEdit.getText().toString();
        if(TextUtils.isEmpty(search)) {
            return;
        }
        Intent intent = new Intent(this, SearchResultActivity.class);
        intent.putExtra("search", search);
        openPage(intent);
    }

    private void labelSearch(LabelInfo label) {
        Intent intent = new Intent(this, SearchResultActivity.class);
        intent.putExtra("label", label);
        openPage(intent);
    }
}
