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

import android.os.Bundle;
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.WorkBaseFg;
import com.wd.workoffice.bean.mesBean.OpmreportErrorBean;
import com.wd.workoffice.bean.mesBean.WokrOrderBean;
import com.wd.workoffice.bean.workEnum.WorkOrder;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.WorkOrderErrorAdapter;
import com.wd.workoffice.util.WorkUtils;

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

import androidx.annotation.Nullable;
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 flexible.xd.android_base.utils.TimeUtils;
import io.reactivex.disposables.Disposable;

/**
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class QualityWorkFragment extends WorkBaseFg {
    @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_name)
    TextView tvName;
    @BindView(R.id.tv_parent)
    TextView tvParent;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.tv_position)
    TextView tvPosition;
    @BindView(R.id.tv_fault)
    TextView tvFault;
    @BindView(R.id.tv_problem_code)
    TextView tvProblemCode;
    @BindView(R.id.tv_work_type)
    TextView tvWorkType;
    @BindView(R.id.tv_link_no)
    TextView tvLinkNo;
    @BindView(R.id.tv_create_by)
    TextView tvCreateBy;
    @BindView(R.id.tv_create_time)
    TextView tvCreateTime;
    @BindView(R.id.tv_off)
    TextView tvOff;
    @BindView(R.id.tv_grant)
    TextView tvGrant;
    @BindView(R.id.tv_detail_info)
    TextView tvDetailInfo;
    @BindView(R.id.tv_off_time)
    TextView tvOffTime;
    @BindView(R.id.tv_on_time)
    TextView tvOnTime;
    @BindView(R.id.tv_plan_star_time)
    TextView tvPlanStarTime;
    @BindView(R.id.tv_plan_end_time)
    TextView tvPlanEndTime;
    @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;
    @BindView(R.id.tv_position_code)
    TextView tvPositionCode;
    @BindView(R.id.tv_machine_code)
    TextView tvMachineCode;
    @BindView(R.id.tv_machine_name)
    TextView tvMachineName;
    private Map<String, Object> param;
    private List<OpmreportErrorBean.ListBean> dataList;
    private WorkOrderErrorAdapter dataAdapter;
    private WokrOrderBean.ListBean info;
    private int page = 1;
    private Map<String, Object> paramMap;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            info = JSON.parseObject(arguments.getString("info"), WokrOrderBean.ListBean.class);
        }
    }

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


    @Override
    protected void initData() {
        tvNo.setText(info.getWoCode());
        tvName.setText(info.getDescription());
        tvParent.setText(info.getParentName());
        tvType.setText("计划工单");
        tvStatus.setText(WorkOrder.getNameByCode(info.getStatus()));
        tvLocation.setText(info.getSiteName());
        tvPosition.setText(info.getLocationName());
        tvPositionCode.setText(info.getLocationCode());
        tvMachineCode.setText(info.getAssetName());
        tvMachineName.setText(info.getAssetCode());

        tvFault.setText(info.getFailureName());
        tvProblemCode.setText(info.getQuestionCode());
        tvWorkType.setText(getWorkType(info.getWorkType()));// 1 2 4  5
        tvLinkNo.setText(info.getLinkedNumber());


        tvCreateBy.setText(info.getCreateName());
        tvCreateTime.setText(TimeUtils.millis2String(info.getCreateDateTime(), new SimpleDateFormat("yyyy-MM-dd")));
        tvOff.setText(info.isShutDown()?"是":"否");
        tvGrant.setText(info.isMateriel()?"是":"否");

        tvDetailInfo.setText(info.getDetailedInformation());
        tvOffTime.setText(info.getShutdownTime());
        tvOnTime.setText(info.getRecoveryTime());


        tvPlanStarTime.setText(info.getSchedStart() ==0L?"":TimeUtils.millis2String(info.getSchedStart(),new SimpleDateFormat("yyyy-MM-dd")));
        tvPlanEndTime.setText(info.getSchedFinish() ==0L?"":TimeUtils.millis2String(info.getSchedFinish(),new SimpleDateFormat("yyyy-MM-dd")) );
        tvTrueStarTime.setText(info.getActStart() ==0L?"":TimeUtils.millis2String(info.getActStart(),new SimpleDateFormat("yyyy-MM-dd")));
        tvTrueEndTime.setText(info.getActFinish() ==0L?"":TimeUtils.millis2String(info.getActFinish(),new SimpleDateFormat("yyyy-MM-dd")));



        dataList = new ArrayList<>();
        dataAdapter = new WorkOrderErrorAdapter(R.layout.item_work_order_error, dataList);
        dataAdapter.bindToRecyclerView(rvData);
        param = WorkUtils.pageMesKey();
        param.put("pageNum", 1);
        paramMap = WorkUtils.simpleParam();
        param.put("paramMap", paramMap);
        paramMap.put("assetId",info.getAssetId());

        getData();
    }

    /**
     * 获取工作类型
     *
     * @param workType
     * @return
     */
    private String getWorkType(int workType) {
        String name = "";
        switch (workType) {
            case 1:
                name = "检修";
                break;
            case 2:
                name = "点检";
                break;
            case 4:
                name = "故障";
                break;
            case 5:
                name = "项目";
                break;
        }
        return name;
    }


    private void getData() {
        RtfUtils.getMesRtf().opmreportError("1", 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<OpmreportErrorBean.ListBean> getList = JSON.parseObject(data.getData().toString(), OpmreportErrorBean.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) {
                    dataAdapter.loadMoreEnd(true);
                } else {
                    page++;
                }
            }
        });

    }

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


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


}
