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.MesServiceBillBean;
import com.wd.workoffice.bean.mesBean.ServiceBean;
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.MesServiceDetailAdapter;
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 ServiceDetailActivity 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_plan)
    TextView tvPlan;
    @BindView(R.id.tv_machine)
    TextView tvMachine;
    @BindView(R.id.tv_person)
    TextView tvPerson;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @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<MesServiceBillBean.ListBean> dataList;
    private MesServiceDetailAdapter dataAdapter;
    private ServiceBean.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"),ServiceBean.ListBean.class);
        changeView();
        dataList = new ArrayList<>();
        dataAdapter = new MesServiceDetailAdapter(R.layout.item_mes_service, dataList);
        dataAdapter.bindToRecyclerView(rvData);
        param = WorkUtils.pageMesKey();
        Map<String, Object> paramMap = WorkUtils.simpleParam();
        param.put("paramMap", paramMap);
        paramMap.put("inspoId", info.getId());
        param.put("pageNum", page);
        getData();
    }

    private void changeView() {
        tvNo.setText(info.getInspoCode());
        tvStatus.setText(info.getStatus() + "");
        tvPerson.setText(info.getPrincipalName());
        tvPlan.setText(info.getPlanDesc());
        tvLocation.setText(info.getLocationName());
        tvMachine.setText(info.getAssetDesc());
        tvFactory.setText(info.getSiteDesc());
        tvRemark.setText(info.getDescription());

//        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 getData() {
        RtfUtils.getMesRtf().modinspoproject("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<MesServiceBillBean.ListBean> getList = JSON.parseObject(data.getData().toString(), MesServiceBillBean.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_detail;
    }

}
