package com.example.administrator.zhapp.activity.ydckgl;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.BaseActivity;
import com.example.administrator.zhapp.adapter.LlrAdapter;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.LlrInfo;
import com.example.administrator.zhapp.utils.GsonUtils;
import com.example.administrator.zhapp.utils.HttpListener;
import com.example.administrator.zhapp.utils.NohttpUtils;
import com.example.administrator.zhapp.utils.TextType;
import com.example.administrator.zhapp.utils.UiUtlis;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;
import java.util.ArrayList;
import java.util.List;

/**
 * 领料人
 */
public class LlrActivity extends BaseActivity {

    EditText etLlr;
    ListView lv;

    private LlrInfo info;
    private List<LlrInfo.Llr> list;
    private LlrAdapter adapter;

    @Override
    public int getContentViewID() {
        return R.layout.activity_llr;
    }

    @Override
    protected void initView() {

        etLlr = findViewById(R.id.et_llr);
        lv = findViewById(R.id.lv);
        mToolbar.setNavigationIcon(R.mipmap.back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UiUtlis.hideInput(etLlr, context);
                finish();
            }
        });
        setTitle("领料人");

        info = new LlrInfo();
        list = new ArrayList<>();

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                UiUtlis.hideInput(etLlr, context);
                Intent intent = new Intent();
                intent.putExtra("LLR", list.get(i));
                setResult(0, intent);
                finish();
            }
        });

    }

    @Override
    public void initData() {

        etLlr.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                if (charSequence.length() != 0) {
                    String text = charSequence.toString();
                    if (TextType.isNum(text)) {
                        if (text.length() >= 3) {
                            sendRequestNohttp(text);
                        }
                    } else {
                        if (text.length() >= 1) {
                            sendRequestNohttp(text);
                        }
                    }
                } else {
                    list.clear();
                    if (adapter != null)
                        adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
    }

    private void sendRequestNohttp(String text) {
        Request<String> request = NoHttp.createStringRequest(Contans.IP + Contans.LLR);
        request.set("userid", text);
        request.set("types", "0");
        NohttpUtils.getInstance().add(this, 0, request, callback, true, true, "加载中...");
    }

    private HttpListener<String> callback = new HttpListener<String>() {
        @Override
        public void onSucceed(int what, Response<String> response) {

            switch (what) {
                case 0:
                    info = GsonUtils.GsonToBean(response.get(), LlrInfo.class);
                    if (info != null) {
                        if (info.getTotal() != 0) {
                            list.clear();
                            list.addAll(info.getRows());
                            if (adapter == null) {
                                adapter = new LlrAdapter(LlrActivity.this, list);
                                lv.setAdapter(adapter);
                            } else {
                                adapter.notifyDataSetChanged();
                            }
                        } else {
                            list.clear();
                            if (adapter != null) {
                                adapter.notifyDataSetChanged();
                            }
                        }
                    } else {
                        showToast(UiUtlis.getString(context, R.string.data_error));
                    }
                    break;
            }
        }

        @Override
        public void onFailed(int what, Response<String> response) {

        }
    };
}