package com.zztrapp.zztrapp.ui.index.activity;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.AbsListView;

import com.github.gzuliyujiang.wheelpicker.DatePicker;
import com.github.gzuliyujiang.wheelpicker.contract.OnDatePickedListener;
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity;
import com.github.gzuliyujiang.wheelpicker.widget.DateWheelLayout;
import com.zztrapp.zztrapp.base.BaseActivity;
import com.zztrapp.zztrapp.bean.ChoosePopItemBean;
import com.zztrapp.zztrapp.bean.HttpResult;
import com.zztrapp.zztrapp.bean.JcjPagelistBean;
import com.zztrapp.zztrapp.databinding.ActivityJcjListEditBinding;
import com.zztrapp.zztrapp.http.HttpMethods;
import com.zztrapp.zztrapp.http.ISubscriberOnNextListener;
import com.zztrapp.zztrapp.http.ProgressSubscriber;
import com.zztrapp.zztrapp.http.RxApiManager;
import com.zztrapp.zztrapp.ui.index.callback.ChooseResultCallBack;
import com.zztrapp.zztrapp.ui.index.fragment.ChooseListFragment;
import com.zztrapp.zztrapp.utilcode.mUtil.MyUtil;
import com.zztrapp.zztrapp.utilcode.util.ToastUtils;
import com.zztrapp.zztrapp.widget.OnSingleClickListener;

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

/**
 * Describe 监测井编辑界面
 * Created by Bby on 2022/6/27
 * Email bby0856@163.com
 */
public class JcjListEditActivity extends BaseActivity {

    private ActivityJcjListEditBinding binding;
    private int jumpType;//1编辑 2查看详情
    private JcjPagelistBean.RecordsBean detail;

    private List<ChoosePopItemBean> xzqhList;
    private List<ChoosePopItemBean> syqdlxList;
    private List<ChoosePopItemBean> syqdmcList;

    @Override
    public View getLayoutId() {
        return binding.getRoot();
    }

    @Override
    protected void initBinding() {
        binding = ActivityJcjListEditBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void init() {
        binding.navView.navTitle.setText(getIntent().getStringExtra("navName"));
        binding.navView.navBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        xzqhList = new ArrayList<>();
        syqdlxList = new ArrayList<>();
        syqdmcList = new ArrayList<>();

        MyUtil.setEditTextInputNumber(binding.jd, 3);
        MyUtil.setEditTextInputNumber(binding.wd, 2);

        jumpType = getIntent().getIntExtra("jumpType", 1);
        detail = (JcjPagelistBean.RecordsBean) getIntent().getSerializableExtra("detail");

        binding.jcjtybh.setText(detail.getJcjtybh());
        binding.ybh.setText(detail.getYbh());
        binding.xzqh.setText(detail.getCountyName());
        binding.dlwz.setText(detail.getDlwz());
        binding.syqdlx.setText(detail.getSyqdlx());
        binding.syqdmc.setText(detail.getSyqdmc());
        binding.jd.setText(detail.getJd());
        binding.wd.setText(detail.getWd());
        binding.ssdw.setText(detail.getSsdw());
        binding.lxr.setText(detail.getLxr());
        binding.dh.setText(detail.getDh());
        binding.ly.setText(detail.getLy());
        binding.dxslx.setText(detail.getDxslx());
        binding.jcsd.setText(detail.getJcsd());
        binding.jcnr.setText(detail.getJcnr());
        binding.jcpl.setText(detail.getJcpl());
        binding.ztsgdw.setText(detail.getZtsgdw());
        binding.zdjgrq.setText(detail.getZdjgrq());
        binding.jcyqazrq.setText(detail.getJcyqazrq());
        if (!TextUtils.isEmpty(detail.getSyqdlx())) {
            getSyqdmc(detail.getSyqdlx());
        }

        if (jumpType == 2) {
            binding.jcjtybh.setEnabled(false);
            binding.ybh.setEnabled(false);
            binding.xzqhIcon.setVisibility(View.GONE);
            binding.dlwz.setEnabled(false);
            binding.syqdlxIcon.setVisibility(View.GONE);
            binding.syqdmcIcon.setVisibility(View.GONE);
            binding.jd.setEnabled(false);
            binding.wd.setEnabled(false);
            binding.ssdw.setEnabled(false);
            binding.lxr.setEnabled(false);
            binding.dh.setEnabled(false);
            binding.ly.setEnabled(false);
            binding.dxslx.setEnabled(false);
            binding.jcsd.setEnabled(false);
            binding.jcnr.setEnabled(false);
            binding.jcpl.setEnabled(false);
            binding.ztsgdw.setEnabled(false);
            binding.bottomView.setVisibility(View.GONE);

        } else {
            loadXzqhList();
            getSyqdlx();
        }
    }

    @Override
    protected void initOnClick() {
        binding.cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        binding.save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                detail.setJcjtybh(binding.jcjtybh.getText().toString());
                detail.setYbh(binding.ybh.getText().toString());
                detail.setDlwz(binding.dlwz.getText().toString());
                detail.setSyqdlx(binding.syqdlx.getText().toString());
                detail.setSyqdmc(binding.syqdmc.getText().toString());
                detail.setJd(binding.jd.getText().toString());
                detail.setWd(binding.wd.getText().toString());
                detail.setSsdw(binding.ssdw.getText().toString());
                detail.setLxr(binding.lxr.getText().toString());
                detail.setDh(binding.dh.getText().toString());
                detail.setLy(binding.ly.getText().toString());
                detail.setJcsd(binding.jcsd.getText().toString());
                detail.setJcnr(binding.jcnr.getText().toString());
                detail.setJcpl(binding.jcpl.getText().toString());
                detail.setZtsgdw(binding.ztsgdw.getText().toString());
                detail.setZdjgrq(binding.zdjgrq.getText().toString());
                detail.setJcyqazrq(binding.jcyqazrq.getText().toString());
                try {
                    HashMap<String, Object> map = MyUtil.getObjectToMap(detail);
                    editJcj(map);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
        //行政区划
        binding.xzqhView.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                if (jumpType == 2) {
                    return;
                }
                ChooseListFragment chooseListFragment = new ChooseListFragment(JcjListEditActivity.this, "", AbsListView.CHOICE_MODE_SINGLE, xzqhList, new ArrayList<>(), new ChooseResultCallBack() {
                    @Override
                    public void onResult(Object object) {
                        // TODO: 2021/10/6 选中的结果，可做强转
                        List<ChoosePopItemBean> list = (List<ChoosePopItemBean>) object;
                        if (list.size() > 0) {
                            binding.xzqh.setText(list.get(0).getName());
                            detail.setCountyCode(list.get(0).getCode());
                            detail.setCountyName(list.get(0).getName());
                        }
                    }
                });
                chooseListFragment.show(getSupportFragmentManager(), "single");
            }
        });
        //双源清单类型
        binding.syqdlxView.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                if (jumpType == 2) {
                    return;
                }
                ChooseListFragment chooseListFragment = new ChooseListFragment(JcjListEditActivity.this, "", AbsListView.CHOICE_MODE_SINGLE, syqdlxList, new ArrayList<>(), new ChooseResultCallBack() {
                    @Override
                    public void onResult(Object object) {
                        // TODO: 2021/10/6 选中的结果，可做强转
                        List<ChoosePopItemBean> list = (List<ChoosePopItemBean>) object;
                        if (list.size() > 0) {
                            binding.syqdlx.setText(list.get(0).getName());
                            detail.setSyqdlx(list.get(0).getName());
                            getSyqdmc(list.get(0).getName());
                        }
                    }
                });
                chooseListFragment.show(getSupportFragmentManager(), "single");
            }
        });
        //双源清单名称
        binding.syqdmcView.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                if (jumpType == 2) {
                    return;
                }
                ChooseListFragment chooseListFragment = new ChooseListFragment(JcjListEditActivity.this, "", AbsListView.CHOICE_MODE_SINGLE, syqdmcList, new ArrayList<>(), new ChooseResultCallBack() {
                    @Override
                    public void onResult(Object object) {
                        // TODO: 2021/10/6 选中的结果，可做强转
                        List<ChoosePopItemBean> list = (List<ChoosePopItemBean>) object;
                        if (list.size() > 0) {
                            binding.syqdmc.setText(list.get(0).getName());
                            detail.setSyqdmc(list.get(0).getName());
                        }
                    }
                });
                chooseListFragment.show(getSupportFragmentManager(), "single");
            }
        });
        //钻探竣工日期
        binding.zdjgrqView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (jumpType == 2) {
                    return;
                }
                DatePicker picker = new DatePicker(JcjListEditActivity.this);
                DateWheelLayout wheelLayout = picker.getWheelLayout();
                String time = binding.zdjgrq.getText().toString();
                if (!TextUtils.isEmpty(time)) {
                    String[] split = time.split("-");
                    DateEntity dateEntity = new DateEntity();
                    dateEntity.setYear(Integer.parseInt(split[0]));
                    dateEntity.setMonth(Integer.parseInt(split[1]));
                    dateEntity.setDay(Integer.parseInt(split[2]));
                    wheelLayout.setDefaultValue(dateEntity);
                }
                picker.getWheelLayout().setResetWhenLinkage(false);
                picker.setOnDatePickedListener(new OnDatePickedListener() {
                    @Override
                    public void onDatePicked(int year, int month, int day) {
                        binding.zdjgrq.setText(MyUtil.numFormat(year) + "-" + MyUtil.numFormat(month) + "-" + MyUtil.numFormat(day));
                    }
                });
                picker.show();
            }
        });
        //监测仪器安装日期
        binding.jcyqazrqView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (jumpType == 2) {
                    return;
                }
                DatePicker picker = new DatePicker(JcjListEditActivity.this);
                DateWheelLayout wheelLayout = picker.getWheelLayout();
                String time = binding.jcyqazrq.getText().toString();
                if (!TextUtils.isEmpty(time)) {
                    String[] split = time.split("-");
                    DateEntity dateEntity = new DateEntity();
                    dateEntity.setYear(Integer.parseInt(split[0]));
                    dateEntity.setMonth(Integer.parseInt(split[1]));
                    dateEntity.setDay(Integer.parseInt(split[2]));
                    wheelLayout.setDefaultValue(dateEntity);
                }
                picker.getWheelLayout().setResetWhenLinkage(false);
                picker.setOnDatePickedListener(new OnDatePickedListener() {
                    @Override
                    public void onDatePicked(int year, int month, int day) {
                        binding.jcyqazrq.setText(MyUtil.numFormat(year) + "-" + MyUtil.numFormat(month) + "-" + MyUtil.numFormat(day));
                    }
                });
                picker.show();
            }
        });
    }

    //编辑
    private void editJcj(HashMap<String, Object> map) {
        ISubscriberOnNextListener<HttpResult<String>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<String>>() {

            @Override
            public void onNext(HttpResult<String> httpResult) {
                if (httpResult.getCode() == 200) {
                    finish();
                } else {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<String>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, this, true);
        RxApiManager.get().add("editJcj", httpResultProgressSubscriber);
        HttpMethods.getInstance(this).editJcj(httpResultProgressSubscriber, map);
    }

    //行政区划列表
    private void loadXzqhList() {
        ISubscriberOnNextListener<HttpResult<List<ChoosePopItemBean>>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<List<ChoosePopItemBean>>>() {
            @Override
            public void onNext(HttpResult<List<ChoosePopItemBean>> httpResult) {
                if (httpResult.getCode() == 200) {
                    xzqhList = httpResult.getResult();
                } else {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<List<ChoosePopItemBean>>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, this, true);
        RxApiManager.get().add("xzbmAreaList", httpResultProgressSubscriber);
        HttpMethods.getInstance(this).xzbmAreaList(httpResultProgressSubscriber);
    }

    //双源清单类型
    private void getSyqdlx() {
        ISubscriberOnNextListener<HttpResult<List<String>>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<List<String>>>() {
            @Override
            public void onNext(HttpResult<List<String>> httpResult) {
                if (httpResult.getCode() == 200) {
                    for (int i = 0; i < httpResult.getResult().size(); i++) {
                        ChoosePopItemBean choosePopItemBean = new ChoosePopItemBean("", httpResult.getResult()
                                .get(i));
                        syqdlxList.add(choosePopItemBean);
                    }
                } else {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<List<String>>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, this, true);
        RxApiManager.get().add("getSyqdlx", httpResultProgressSubscriber);
        HttpMethods.getInstance(this).getSyqdlx(httpResultProgressSubscriber);
    }

    //双源清单名称
    private void getSyqdmc(String s) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "矿山开采");
        ISubscriberOnNextListener<HttpResult<List<String>>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<List<String>>>() {
            @Override
            public void onNext(HttpResult<List<String>> httpResult) {
                if (httpResult.getCode() == 200) {
                    for (int i = 0; i < httpResult.getResult().size(); i++) {
                        ChoosePopItemBean choosePopItemBean = new ChoosePopItemBean("", httpResult.getResult()
                                .get(i));
                        syqdmcList.add(choosePopItemBean);
                    }
                } else {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<List<String>>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, this, true);
        RxApiManager.get().add("getSyqdmc", httpResultProgressSubscriber);
        HttpMethods.getInstance(this).getSyqdmc(httpResultProgressSubscriber, map);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
        RxApiManager.get().cancel("editJcj");
        RxApiManager.get().cancel("xzbmAreaList");
        RxApiManager.get().cancel("getSyqdlx");
        RxApiManager.get().cancel("getSyqdmc");
    }
}