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

import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
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.ServiceStandardBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.MesServiceStandardAdapter;
import com.wd.workoffice.util.WorkUtils;

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

import androidx.annotation.NonNull;
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 ServiceStandardActivity extends WorkToolBarActivity {

    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    private Map<String, Object> param;
    private List<ServiceStandardBean.ListBean> dataList;
    private MesServiceStandardAdapter dataAdapter;
    private int page = 1;

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


    @Override
    protected void initData() {
        dataList = new ArrayList<>();
        dataAdapter = new MesServiceStandardAdapter(R.layout.item_mes_service_standard, dataList, getIntent().getStringExtra("type"));
        dataAdapter.bindToRecyclerView(rvData);
        dataAdapter.setEmptyView(R.layout.view_empty_content, rvData);
        param = WorkUtils.pageMesKey();
        Map<String, Object> paramMap = WorkUtils.simpleParam();
        param.put("paramMap", paramMap);

        param.put("pageNum", page);
        if (!TextUtils.isEmpty(getIntent().getStringExtra("type"))) {//维修
            paramMap.put("inspoProId", getIntent().getStringExtra("id"));
            getServiceData();
        } else {
            paramMap.put("projectId", getIntent().getStringExtra("id"));
            getData();
        }

    }

    private void getData() {
        RtfUtils.getMesRtf().spotCheckStandardList("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<ServiceStandardBean.ListBean> getList = JSON.parseObject(data.getData().toString(), ServiceStandardBean.class).getList();
                if (page == 1) {
                    dataList.clear();
                    dataList.addAll(getList);
                    dataAdapter.notifyDataSetChanged();
                    dataAdapter.loadMoreComplete();
                    srlRefresh.finishRefresh();
                } else {
                    dataAdapter.loadMoreComplete();
                    dataList.addAll(getList);
                    dataAdapter.notifyDataSetChanged();
                }
                if (getList.size() < Config.SIZE) {
                    dataAdapter.loadMoreEnd();
                } else {
                    page++;
                }
            }
        });
    }


    private void getServiceData() {
        RtfUtils.getMesRtf().serviceStandardList("afb5fde8eb1c11e98334fa163e1166bd", 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<ServiceStandardBean.ListBean> getList = JSON.parseObject(data.getData().toString(), ServiceStandardBean.class).getList();
                if (page == 1) {
                    dataList.clear();
                    dataList.addAll(getList);
                    dataAdapter.notifyDataSetChanged();
                    dataAdapter.loadMoreComplete();
                    srlRefresh.finishRefresh();
                } else {
                    dataAdapter.loadMoreComplete();
                    dataList.addAll(getList);
                    dataAdapter.notifyDataSetChanged();
                }
                if (getList.size() < Config.SIZE) {
                    dataAdapter.loadMoreEnd();
                } else {
                    page++;
                }
            }
        });
    }

    @Override
    protected void initEvent() {
        srlRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                param.put("pageNum", 1);
                if (!TextUtils.isEmpty(getIntent().getStringExtra("type"))) {//维修
                    getServiceData();
                } else {
                    getData();
                }
            }
        });

        dataAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                param.put("pageNum", page);
                if (!TextUtils.isEmpty(getIntent().getStringExtra("type"))) {//维修
                    getServiceData();
                } else {
                    getData();
                }
            }
        }, rvData);
        dataAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                startActivity(ServiceStandardDetailActivity.class, "info", JSON.toJSONString(dataList.get(i)),
                        "type", getIntent().getStringExtra("type"));
            }
        });
    }

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

}
