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

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.DeviceBean;
import com.wd.workoffice.bean.workEnum.PagePermissionType;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.DeviceAdapter;
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 butterknife.OnClick;
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 WorkDeviceNextActivity extends WorkToolBarActivity {

    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    @BindView(R.id.et_key)
    EditText etKey;
    @BindView(R.id.btn_search)
    Button btnSearch;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    private int page = 1;
    private Map<String, Object> param;
    private DeviceAdapter dataAdapter;
    private List<DeviceBean.RecordsBean> dataList;

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


    @Override
    protected void initData() {
        param = WorkUtils.pageKey();
        param.put("parentId", getIntent().getStringExtra("id"));
        page = 1;
        param.put("current", page);
        dataList = new ArrayList<>();
        dataAdapter = new DeviceAdapter(R.layout.item_device, dataList);
        dataAdapter.bindToRecyclerView(rvData);
        dataAdapter.setEmptyView(R.layout.view_empty_content, rvData);
        llSearch.setVisibility(View.GONE);
        if (WorkUtils.hasPermission(PagePermissionType.EQUIPMENT_LIST.getPermission())) {
            getData();
        } else {
            toast(R.string.permission);
        }
    }


    private void getData() {
        RtfUtils.getRtf().equipmentList(param).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

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

            @Override
            public void onSuccess(BaseBean data) {
                hideLoading();
                if (data.getCode() != 0) {
                    toast(data.getMessage());
                    return;
                }
                List<DeviceBean.RecordsBean> getList = JSON.parseObject(data.getData().toString(), DeviceBean.class).getRecords();
                if (page == 1) {
                    srlRefresh.finishRefresh();
                    dataList.clear();
                    dataList.addAll(getList);
                    dataAdapter.notifyDataSetChanged();
                    dataAdapter.loadMoreComplete();
                } else {
                    dataAdapter.loadMoreComplete();
                    dataList.addAll(getList);
                    dataAdapter.notifyDataSetChanged();
                }
                if (getList.size() == 0) {
                    dataAdapter.loadMoreEnd();
                } else {
                    page++;
                }
            }
        });
    }

    @Override
    protected void initEvent() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            if (WorkUtils.hasPermission(PagePermissionType.EQUIPMENT_LIST.getPermission())) {
                page = 1;
                param.put("current", page);
                getData();
            } else {
                toast(R.string.permission);
            }
        });
        dataAdapter.setOnLoadMoreListener(() -> {
            param.put("current", page);
            getData();
        }, rvData);
        dataAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(WorkDeviceDetailActivity.class,
                        "info", JSON.toJSONString(dataList.get(position)));
            }
        });
        dataAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_component:
                        startActivity(WorkDeviceNextActivity.class,
                                "id", dataList.get(position).getId()+"");
                        break;
                    case R.id.tv_agree:
                        break;

                }
            }
        });
    }


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

    @OnClick(R.id.btn_search)
    public void onViewClicked() {
        if (WorkUtils.hasPermission(PagePermissionType.EQUIPMENT_LIST.getPermission())) {
            page = 1;
            param.put("searchValue", etKey.getText().toString());
            param.put("current", page);
            getData();
        } else {
            toast(R.string.permission);
        }
    }

}
