package com.wd.workoffice.ui.activity.mes;

import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseMesBean;
import com.wd.workoffice.app.Config;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.mesBean.MesServiceCheckBillBean;
import com.wd.workoffice.bean.mesBean.ServiceCheckBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.MesServiceCheckDetailAdapter;
import com.wd.workoffice.util.WorkUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import io.reactivex.disposables.Disposable;

/**
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class ServiceCheckDetailActivity extends WorkToolBarActivity {

    @BindView(R.id.tv_refuse)
    TextView tvRefuse;
    @BindView(R.id.tv_agree)
    TextView tvAgree;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    @BindView(R.id.tv_no)
    TextView tvNo;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_person)
    TextView tvPerson;
    @BindView(R.id.tv_new_person)
    TextView tvNewPerson;
    @BindView(R.id.tv_new_time)
    TextView tvNewTime;
    @BindView(R.id.tv_machine)
    TextView tvMachine;
    @BindView(R.id.tv_factory)
    TextView tvFactory;
    @BindView(R.id.tv_remark)
    TextView tvRemark;
    @BindView(R.id.tv_plan_star_time)
    TextView tvPlanStarTime;
    @BindView(R.id.tv_plan_end_time)
    TextView tvPlanEndTime;
    @BindView(R.id.tv_change_star_time)
    TextView tvChangeStarTime;
    @BindView(R.id.tv_change_end_time)
    TextView tvChangeEndTime;
    @BindView(R.id.tv_true_star_time)
    TextView tvTrueStarTime;
    @BindView(R.id.tv_true_end_time)
    TextView tvTrueEndTime;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    private Map<String, Object> param;
    private List<MesServiceCheckBillBean.ListBean> dataList;
    private MesServiceCheckDetailAdapter dataAdapter;
    private ServiceCheckBean.ListBean info;
    private int page = 1;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        rvData.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvData.setNestedScrollingEnabled(false);
    }


    @Override
    protected void initData() {
        info = JSON.parseObject(getIntent().getStringExtra("info"), ServiceCheckBean.ListBean.class);
        changeView();

        dataList = new ArrayList<>();
        dataAdapter = new MesServiceCheckDetailAdapter(R.layout.item_mes_service_check, dataList);
        dataAdapter.bindToRecyclerView(rvData);
        param = WorkUtils.pageMesKey();
        Map<String, Object> paramMap = WorkUtils.simpleParam();
        param.put("paramMap", paramMap);
        paramMap.put("billId", info.getId());
        param.put("pageNum", page);
        getDetail();
        getData();
    }

    private void changeView() {
        tvNo.setText(info.getBillNum());
        tvStatus.setText(info.getStatus() + "");
        tvPerson.setText(info.getSpotCheckPersonName());
        tvNewPerson.setText(info.getCreateUserName());
        tvNewTime.setText(info.getCreateDateTime());
        tvMachine.setText(info.getAstAssetName());
        tvFactory.setText(info.getSiteName());
        tvRemark.setText(info.getBillDescription());

        tvPlanStarTime.setText(info.getPlanBeginTime());
        tvPlanEndTime.setText(info.getPlanEndTime());
        tvChangeStarTime.setText(info.getAdjustBeginTime());
        tvChangeEndTime.setText(info.getAdjustEndTime());
        tvTrueStarTime.setText(info.getActualBeginTime());
        tvTrueEndTime.setText(info.getActualEndTime());
    }

    private void getDetail() {
        RtfUtils.getMesRtf().spotCheckDetail(info.getId(), "1").compose(Transformer.schedule()).subscribe(new WorkObserver<BaseMesBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseMesBean data) {
                hideLoading();
                if (!data.isSuccess()) {
                    toast(data.getMessage());
                    return;
                }
                info = JSON.parseObject(data.getData().toString(), ServiceCheckBean.ListBean.class);
                changeView();
            }
        });
    }


    private void getData() {
        RtfUtils.getMesRtf().spotCheckBill("00d12c310f4411ea8334fa163e1166bd", WorkUtils.convertMapToBody(param)).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseMesBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseMesBean data) {
                hideLoading();
                if (!data.isSuccess()) {
                    toast(data.getMessage());
                    return;
                }
                List<MesServiceCheckBillBean.ListBean> getList = JSON.parseObject(data.getData().toString(), MesServiceCheckBillBean.class).getList();
                if (page == 1) {
                    dataList.clear();
                    dataList.addAll(getList);
                    dataAdapter.notifyDataSetChanged();
                    dataAdapter.loadMoreComplete();
                } else {
                    dataAdapter.loadMoreComplete();
                    dataList.addAll(getList);
                    dataAdapter.notifyDataSetChanged();
                }
                if (getList.size() == 0 || getList.size() < Config.SIZE) {
                    dataAdapter.loadMoreEnd();
                } else {
                    page++;
                }
            }
        });
    }

    @Override
    protected void initEvent() {
        dataAdapter.setOnLoadMoreListener(() -> {
            param.put("pageNum", page);
            getData();
        }, rvData);
    }


    @Override
    protected int layoutId() {
        return R.layout.activity_mes_service_check_detail;
    }

}
