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.WsclssIndexBean;
import com.zztrapp.zztrapp.databinding.ActivityWsclssListEditBinding;
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/7/7
 * Email bby0856@163.com
 */
public class WsclssListEditActivity extends BaseActivity {

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

    private List<ChoosePopItemBean> xzqhList;
    private List<ChoosePopItemBean> yxztList;

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

    @Override
    protected void initBinding() {
        binding = ActivityWsclssListEditBinding.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<>();
        yxztList = new ArrayList<>();

        yxztList.add(new ChoosePopItemBean("0", "正常运行"));
        yxztList.add(new ChoosePopItemBean("1", "停止运行"));

        MyUtil.setEditTextInputNumber(binding.sbscwzjd,3);
        MyUtil.setEditTextInputNumber(binding.sbscwzwd,2);

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

        binding.yxzt.setText(detail.getCountyName());
        if (null != detail.getYxzt()) {
            switch (Integer.parseInt(detail.getYxzt())) {//治理级别
                case 0:
                    binding.yxzt.setText("正常运行");
                    break;
                case 1:
                    binding.yxzt.setText("停止运行");
                    break;
            }
        }

        binding.sbmc.setText(detail.getSbmc());
        binding.sbbh.setText(detail.getSbbh());
        binding.xzqh.setText(detail.getCountyName());
        binding.shejigm.setText(detail.getShejigm());
        binding.sjgm.setText(detail.getSjgm());
        binding.sbscwzjd.setText(detail.getSbscwzjd());
        binding.sbscwzwd.setText(detail.getSbscwzwd());
        binding.clgy.setText(detail.getClgy());
        binding.sjcsbz.setText(detail.getSjcsbz());
        binding.wsqx.setText(detail.getWsqx());

        binding.jssj.setText(detail.getJssj());
        binding.tysj.setText(detail.getTysj());
        if (jumpType == 2) {
            binding.sbmc.setEnabled(false);
            binding.sbbh.setEnabled(false);
            binding.xzqhIcon.setVisibility(View.GONE);
            binding.shejigm.setEnabled(false);
            binding.sjgm.setEnabled(false);
            binding.sbscwzjd.setEnabled(false);
            binding.sbscwzwd.setEnabled(false);
            binding.clgy.setEnabled(false);
            binding.sjcsbz.setEnabled(false);
            binding.wsqx.setEnabled(false);
            binding.yxztIcon.setVisibility(View.GONE);
            binding.bottomView.setVisibility(View.GONE);

        } else {
            loadXzqhList();
        }
    }

    @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.setSbmc(binding.sbmc.getText().toString());
                detail.setSbbh(binding.sbbh.getText().toString());
                detail.setSjgm(binding.shejigm.getText().toString().trim());
                detail.setSjgm(binding.sjgm.getText().toString().trim());
                detail.setSbscwzjd(binding.sbscwzjd.getText().toString());
                detail.setSbscwzwd(binding.sbscwzwd.getText().toString());
                detail.setClgy(binding.clgy.getText().toString());
                detail.setWsqx(binding.wsqx.getText().toString());

                detail.setJssj(binding.jssj.getText().toString());
                detail.setTysj(binding.tysj.getText().toString());
                try {
                    HashMap<String, Object> map = MyUtil.getObjectToMap(detail);
                    editWsclss(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(WsclssListEditActivity.this, "", AbsListView.CHOICE_MODE_SINGLE, xzqhList, new ArrayList<>(), new ChooseResultCallBack() {
                    @Override
                    public void onResult(Object object) {
                        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.jssjView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (jumpType == 2) {
                    return;
                }
                DatePicker picker = new DatePicker(WsclssListEditActivity.this);
                DateWheelLayout wheelLayout = picker.getWheelLayout();
                String time = binding.jssj.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.jssj.setText(MyUtil.numFormat(year) + "-" + MyUtil.numFormat(month) + "-" + MyUtil.numFormat(day));
                    }
                });
                picker.show();
            }
        });
        //投运时间
        binding.tysjView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (jumpType == 2) {
                    return;
                }
                DatePicker picker = new DatePicker(WsclssListEditActivity.this);
                DateWheelLayout wheelLayout = picker.getWheelLayout();
                String time = binding.tysj.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.tysj.setText(MyUtil.numFormat(year) + "-" + MyUtil.numFormat(month) + "-" + MyUtil.numFormat(day));
                    }
                });
                picker.show();
            }
        });

        binding.yxztView.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                if (jumpType == 2) {
                    return;
                }
                ChooseListFragment chooseListFragment = new ChooseListFragment(WsclssListEditActivity.this, "", AbsListView.CHOICE_MODE_SINGLE, yxztList, new ArrayList<>(), new ChooseResultCallBack() {
                    @Override
                    public void onResult(Object object) {
                        List<ChoosePopItemBean> list = (List<ChoosePopItemBean>) object;
                        if (list.size() > 0) {
                            binding.yxzt.setText(list.get(0).getName());
                            detail.setYxzt(list.get(0).getCode());
                        }
                    }
                });
                chooseListFragment.show(getSupportFragmentManager(), "single");
            }
        });

    }

    //编辑
    private void editWsclss(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("editWsclss", httpResultProgressSubscriber);
        HttpMethods.getInstance(this).editWsclss(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);
    }

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