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

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

import com.zztrapp.zztrapp.R;
import com.zztrapp.zztrapp.base.BaseActivity;
import com.zztrapp.zztrapp.bean.ChoosePopItemBean;
import com.zztrapp.zztrapp.bean.HeiChouShuiTiIndexBean;
import com.zztrapp.zztrapp.bean.HttpResult;
import com.zztrapp.zztrapp.databinding.ActivityHeiChouShuiTiEditBinding;
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.DecimalDigitsInputFilter;
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 HeiChouShuiTiEditActivity extends BaseActivity {

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

    private List<ChoosePopItemBean> xzqxList = new ArrayList<>();
    private List<ChoosePopItemBean> apztList = new ArrayList<>();
    private List<ChoosePopItemBean> zljbList = new ArrayList<>();

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

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

    @Override
    protected void init() {
        binding.navView.navTitle.setText(R.string.edit);
        binding.navView.navBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        zljbList.add(new ChoosePopItemBean("0", "正常治理"));
        zljbList.add(new ChoosePopItemBean("1", "优先治理"));

        apztList.add(new ChoosePopItemBean("0", "黑臭水体"));
        apztList.add(new ChoosePopItemBean("1", "正常水体"));

        binding.hcdqdjd.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(3, 6)});
        binding.hcdqdwd.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(2, 6)});
        binding.hcdzdjd.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(3, 6)});
        binding.hcdzdwd.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(2, 6)});

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

        if (null != detail.getZljb()) {
            switch (detail.getZljb()) {//治理级别
                case 0:
                    binding.zljb.setText("正常治理");
                    break;
                case 1:
                    binding.zljb.setText("优先治理");
                    break;
            }
        }
        if (null != detail.getAqzt()) {
            switch (detail.getAqzt()) {//安全状态
                case 0:
                    binding.aqzt.setText("黑臭水体");
                    break;
                case 1:
                    binding.aqzt.setText("正常水体");
                    break;
            }
        }
        binding.stmc.setText(detail.getStmc());
        binding.stbh.setText(detail.getStbh());
        binding.xzqh.setText(detail.getCountyName());
        binding.symj.setText(detail.getSymj());
        binding.sycd.setText(detail.getSycd());
        binding.sykd.setText(detail.getSykd());
        binding.hcdqd.setText(detail.getHcdqd());
        binding.hcdqdjd.setText(detail.getHcdqdjd());
        binding.hcdqdwd.setText(detail.getHcdqdwd());
        binding.hcdzd.setText(detail.getHcdzd());
        binding.hcdzdjd.setText(detail.getHcdzdjd());
        binding.hcdzdwd.setText(detail.getHcdzdwd());
        if (jumpType == 2) {
            binding.stmc.setEnabled(false);
            binding.stbh.setEnabled(false);
            binding.xzqhIcon.setVisibility(View.GONE);
            binding.zljbIcon.setVisibility(View.GONE);
            binding.symj.setEnabled(false);
            binding.sykd.setEnabled(false);
            binding.hcdqd.setEnabled(false);
            binding.hcdqdjd.setEnabled(false);
            binding.hcdqdwd.setEnabled(false);
            binding.hcdzd.setEnabled(false);
            binding.hcdzdjd.setEnabled(false);
            binding.hcdzdwd.setEnabled(false);
            binding.aqztIcon.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.setStmc(binding.stmc.getText().toString());
                detail.setStbh(binding.stbh.getText().toString());
                detail.setSymj(binding.symj.getText().toString().trim());
                detail.setSycd(binding.sycd.getText().toString().trim());
                detail.setSykd(binding.sykd.getText().toString().trim());
                detail.setHcdqd(binding.hcdqd.getText().toString());
                detail.setHcdqdjd(binding.hcdqdjd.getText().toString());
                detail.setHcdqdwd(binding.hcdqdwd.getText().toString());
                detail.setHcdzd(binding.hcdzd.getText().toString());
                detail.setHcdzdjd(binding.hcdzdjd.getText().toString());
                detail.setHcdzdwd(binding.hcdzdwd.getText().toString());
                try {
                    HashMap<String, Object> map = MyUtil.getObjectToMap(detail);
                    editHcst(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(HeiChouShuiTiEditActivity.this, "", AbsListView.CHOICE_MODE_SINGLE, xzqxList, 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.zljbView.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                if (jumpType == 2) {
                    return;
                }
                ChooseListFragment chooseListFragment = new ChooseListFragment(HeiChouShuiTiEditActivity.this, "", AbsListView.CHOICE_MODE_SINGLE, zljbList, 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.zljb.setText(list.get(0).getName());
                            detail.setZljb(Integer.parseInt(list.get(0).getCode()));
                        }
                    }
                });
                chooseListFragment.show(getSupportFragmentManager(), "single");
            }
        });
        binding.aqztView.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                if (jumpType == 2) {
                    return;
                }
                ChooseListFragment chooseListFragment = new ChooseListFragment(HeiChouShuiTiEditActivity.this, "", AbsListView.CHOICE_MODE_SINGLE, apztList, 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.aqzt.setText(list.get(0).getName());
                            detail.setAqzt(Integer.parseInt(list.get(0).getCode()));
                        }
                    }
                });
                chooseListFragment.show(getSupportFragmentManager(), "single");
            }
        });
    }

    //编辑
    private void editHcst(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("editHcst", httpResultProgressSubscriber);
        HttpMethods.getInstance(this).editHcst(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) {
                    xzqxList = 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("editHcst");
        RxApiManager.get().cancel("xzbmAreaList");
    }
}