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

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;

import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.BaseActivity;
import com.example.administrator.zhapp.adapter.XsTjExpandableListAdapter;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.BasicDataBean;
import com.example.administrator.zhapp.entity.XsDqgzTjListBean1;
import com.example.administrator.zhapp.entity.XsRequestInfo;
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.weight.InputLayout;
import com.example.administrator.zhapp.weight.ListDialog;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;
import java.util.ArrayList;
import java.util.List;


/**
 * 巡视定期工作统计
 */
public class XsDqgzTjActivity extends BaseActivity {

    ExpandableListView elv;
    InputLayout ilJhmc;
    InputLayout ilJhzy;
    Button btn_query;

    private List<XsDqgzTjListBean1.DataBeanX> datas;
    private List<BasicDataBean.DataBean> zys;

    private List<String> dialogDatas;

    private XsTjExpandableListAdapter adapter;

    private void findView() {
        elv = findViewById(R.id.elv);
        ilJhmc = findViewById(R.id.il_jhmc);
        ilJhzy = findViewById(R.id.il_jhzy);
        btn_query = findViewById(R.id.btn_query);
        btn_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestDatas();
            }
        });
    }

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

    @Override
    protected void initView() {
        findView();
        setTitle("定期工作统计");
        setBack();

        datas = new ArrayList<>();
        zys = new ArrayList<>();
        dialogDatas = new ArrayList<>();

        ilJhzy.setTvOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ListDialog dialog = new ListDialog(context, dialogDatas, new ListDialog.ListDialogListener() {
                    @Override
                    public void selectText(String str, int position) {
                        ilJhzy.setContent(str);
                    }
                });
                dialog.show();
            }
        });

        requestZyDatas();
    }

    @Override
    protected void initData() {

    }

    private void requestDatas() {


        final Request<String> requset = NoHttp.createStringRequest(Contans.IP + Contans.XSDJ, RequestMethod.POST);
        requset.setDefineRequestBodyForJson(createJson());

        NohttpUtils.getInstance().add(this, 0, requset, new HttpListener<String>() {
            @Override
            public void onSucceed(int what, Response<String> response) {

                try {
                    XsDqgzTjListBean1 xsDqgzBgListBean = GsonUtils.GsonToBean(response.get(), XsDqgzTjListBean1.class);
                    if (xsDqgzBgListBean != null) {
                        if (xsDqgzBgListBean.getState() == 1) {
                            showToast(xsDqgzBgListBean.getMsg());
                            datas.clear();
                            datas.addAll(xsDqgzBgListBean.getData());
                            setAdapter();
                        } else {
                            showToast(xsDqgzBgListBean.getMsg());
                            datas.clear();
                            setAdapter();
                        }
                    } else {
                        showToast(R.string.data_error);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

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

            }
        });
    }

    private void requestZyDatas() {


        final Request<String> requset = NoHttp.createStringRequest(Contans.IP + Contans.XS_JCSJ, RequestMethod.POST);

        requset.setDefineRequestBodyForJson(createZyJson());

        NohttpUtils.getInstance().add(this, 0, requset, new HttpListener<String>() {
            @Override
            public void onSucceed(int what, Response<String> response) {

                try {
                    BasicDataBean bean = GsonUtils.GsonToBean(response.get(), BasicDataBean.class);
                    if (bean != null) {
                        if (bean.getState() == 1) {
                            if (bean.getData().size() != 0) {
                                zys.clear();
                                zys.addAll(bean.getData());
                                initDialogDatas();
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

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

            }
        });
    }

    private void initDialogDatas() {

        for (BasicDataBean.DataBean bean : zys) {
            dialogDatas.add(bean.getName());
        }
    }

    private void setAdapter() {

        if (adapter == null) {
            adapter = new XsTjExpandableListAdapter(context, datas);
            elv.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    private String createJson() {
        XsRequestInfo info = new XsRequestInfo();
        info.setAction("DQGZ_ZXTJ_GET");
        info.setZxmc(ilJhmc.getContent());

        if (TextUtils.isEmpty(ilJhzy.getContent())) {
            info.setZy("");
        } else {
            for (BasicDataBean.DataBean bean : zys) {
                if (ilJhzy.getContent().equals(bean.getName())) {
                    info.setZy(bean.getId());
                }
            }
        }

        info.setRqts("");
        String json = GsonUtils.GsonString(info);
        return json;
    }

    private String createZyJson() {
        XsRequestInfo info = new XsRequestInfo();
        info.setAction("GGJK_JCSJ_GET");
        info.setModuletype("Set_DQZY");
        String json = GsonUtils.GsonString(info);
        return json;
    }
}
