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

import android.os.Bundle;
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.WokrOrderBean;
import com.wd.workoffice.bean.mesBean.WorkOrderSon;
import com.wd.workoffice.bean.mesBean.WorkTaskBean;
import com.wd.workoffice.bean.mesBean.WorkTaskMaterialsBean;
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.WorkOrderSonAdapter;
import com.wd.workoffice.ui.adapter.WorkOrderTaskAdapter;
import com.wd.workoffice.util.WorkUtils;

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 io.reactivex.disposables.Disposable;

/**
 * 详情计划
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class QualityWorkPlanFragment extends WorkBaseFg {
    @BindView(R.id.tv_no)
    TextView tvNo;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.rv_son)
    RecyclerView rvSon;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    private Map<String, Object> param;
    private List<WorkOrderSon.ListBean> sonList;
    private List<WorkTaskBean.ListBean> taskList;
    private WorkOrderSonAdapter sonAdapter;
    private WorkOrderTaskAdapter taskAdapter;
    private WokrOrderBean.ListBean info;
    private int page = 1;
    private int taskPage = 1;
    private Map<String, Object> paramMap;
    private Map<String, Object> taskParam;
    private Map<String, Object> taskParamMap;

    @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));
        rvSon.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
    }


    @Override
    protected void initData() {
        tvNo.setText(info.getWoCode());
        tvDesc.setText(info.getDescription());
        tvStatus.setText(WorkOrder.getNameByCode(info.getStatus()));
        taskList = new ArrayList<>();
        taskAdapter = new WorkOrderTaskAdapter(R.layout.item_work_order_task, taskList);
        taskAdapter.bindToRecyclerView(rvData);

        sonList = new ArrayList<>();
        sonAdapter = new WorkOrderSonAdapter(R.layout.item_work_order_son, sonList);
        sonAdapter.bindToRecyclerView(rvSon);
        param = WorkUtils.pageMesKey();
        param.put("pageNum", 1);
        paramMap = WorkUtils.simpleParam();
        param.put("paramMap", paramMap);
        paramMap.put("parentId", info.getId());


        taskParam = WorkUtils.pageMesKey();
        taskParam.put("pageNum", 1);
        taskParamMap = WorkUtils.simpleParam();
        taskParam.put("paramMap", taskParamMap);
        taskParamMap.put("woId", info.getId());

        getData();
        getTask();
    }



    private void getData() {
        RtfUtils.getMesRtf().workOrderChild("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<WorkOrderSon.ListBean> getList = JSON.parseObject(data.getData().toString(), WorkOrderSon.class).getList();

                if (page == 1) {
                    sonList.clear();
                    sonList.addAll(getList);
                    sonAdapter.notifyDataSetChanged();
                    sonAdapter.loadMoreComplete();
                } else {
                    sonAdapter.loadMoreComplete();
                    sonList.addAll(getList);
                    sonAdapter.notifyDataSetChanged();
                }
                if (getList.size() == 0) {
                    sonAdapter.loadMoreEnd(true);
                } else {
                    page++;
                }
            }
        });

    }
    private void getTask() {
        RtfUtils.getMesRtf().pageActivity("1", WorkUtils.convertMapToBody(taskParam)).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<WorkTaskBean.ListBean> getList = JSON.parseObject(data.getData().toString(), WorkTaskBean.class).getList();

                if (taskPage == 1) {
                    taskList.clear();
                    taskList.addAll(getList);
                    taskAdapter.notifyDataSetChanged();
                    taskAdapter.loadMoreComplete();
                } else {
                    taskAdapter.loadMoreComplete();
                    taskList.addAll(getList);
                    taskAdapter.notifyDataSetChanged();
                }
                if (getList.size() == 0) {
                    taskAdapter.loadMoreEnd(true);
                } else {
                    taskPage++;
                }
            }
        });

    }
    @Override
    protected void initEvent() {
        sonAdapter.setOnLoadMoreListener(() -> {
            param.put("pageNum", page);
            getData();
        }, rvSon);
        taskAdapter.setOnLoadMoreListener(() -> {
            taskParam.put("pageNum", taskPage);
            getTask();
        }, rvData);
        //TODO 子工单任务详情跳转
    }


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


}
