package com.unis.sxzcl.ui.aeration;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.unis.model.DataCallback;
import com.unis.model.HouseModel;
import com.unis.model.ProcessModel;
import com.unis.model.UserModel;
import com.unis.model.bean.HouseListBean;
import com.unis.model.bean.aeration.AerationBean;
import com.unis.model.event.AerationEvent;
import com.unis.sxzcl.R;
import com.unis.sxzcl.base.BaseListFragment;
import com.unis.sxzcl.databinding.FragmentBaseListTitleBinding;
import com.xyxsbj.mylibrary.tool.ToastTool;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

/**
 * 包名: com.unis.sxzcl.ui.aeration
 * 类名: AerationListFragment
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/14 17:50
 * 更新人:
 * 更新时间: 2020/12/14 17:50
 * 更新说明:
 * 版本号: 1.0
 */
public class AerationListFragment extends BaseListFragment<FragmentBaseListTitleBinding> implements AerationListAdapter.Callback {


    private AerationListAdapter mAdapter;

    @Override
    protected FragmentBaseListTitleBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentBaseListTitleBinding.inflate(inflater, container, false);
    }

    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        HouseListBean house = HouseModel.get().getSelectHouse();
        if (house == null) {
            ToastTool.showLong("未选择仓房");
            onBack();
            return;
        }

        mBinding.bar.setTitle("机械通风(" + house.houseName + ")")
                .setLeftClickListener(v -> onBack())
                .setRightVisible(true).setRightText("新增")
                .setRightClickListener(v -> goAdd());

        mAdapter = new AerationListAdapter();
        mAdapter.setCallback(this);
        initBaseListView(mBinding.refreshLayout, mBinding.recyclerView, mAdapter, true);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onFirstShow() {
        onRefresh();
    }

    @Override
    public void onRefreshData() {
        getData(true);
    }

    @Override
    public void onMoreData() {
        super.onMoreData();
        getData(false);
    }

    private int page = 1;

    public void getData(boolean refresh) {

        // 修改页数
        if (refresh) page = 1;
        else page++;

        // 请求接口
        ProcessModel.get().getProcessList(page, "tf", new DataCallback<List<AerationBean>>() {
            @Override
            public void onSuccess(List<AerationBean> data) {
                if (!isActive()) return;

                // 填充数据
                if (page == 1) mAdapter.setList(data);
                else mAdapter.addData(data);

                // 修改页面状态
                if (data.size() == 0) loadEnd();
                else loadComplete();

            }

            @Override
            public void onError(String error) {
                super.onError(error);
                if (!isActive()) return;
                loadError();
            }
        });
    }

    private void goAdd() {
        Bundle bundle = new Bundle();
        bundle.putString("pageType", "add");
        navigation(R.id.aerationDetailFragment, bundle);
    }


    @Override
    public void onItemClickListener(AerationBean item) {
        Bundle bundle = new Bundle();
        bundle.putString("data", new Gson().toJson(item));


        String auditState = item.getAuditState();
        String uid = UserModel.get().getUser().getUid().toString();

        if (auditState != null && auditState.equals("4") || !uid.equals(item.getApplyPeople())) {
            // 状态是已完成 登录人不是申请人  跳转详情
            bundle.putString("pageType", "detail");
        } else {
            // 跳转编辑
            bundle.putString("pageType", "edit");
        }
        navigation(R.id.aerationDetailFragment, bundle);
    }


    @Subscribe
    public void onEvent(AerationEvent event) {
        onRefresh();
    }
}
