package com.unis.sxzcl.ui.notice;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.unis.model.DataCallback;
import com.unis.model.NoticeModel;
import com.unis.model.bean.notice.NoticeBean;
import com.unis.sxzcl.base.BaseListFragment;
import com.unis.sxzcl.databinding.FragmentBaseListTitleBinding;
import com.xyxsbj.mylibrary.tool.ToastTool;

import java.util.List;

/**
 * 包名: com.unis.sxzcl.ui.notice
 * 类名: NoticeListFragment
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/11/24 11:27
 * 更新人:
 * 更新时间: 2020/11/24 11:27
 * 更新说明:
 * 版本号: 1.0
 */
public class NoticeListFragment extends BaseListFragment<FragmentBaseListTitleBinding> {

    private NoticeListAdapter mAdapter;


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

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

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

    @Override
    protected void init() {
        mBinding.bar.setTitle("通知列表")
                .setLeftClickListener(v -> onBack());

        mAdapter = new NoticeListAdapter();
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            // TODO: 2020/12/5 跳转业务页面
        });
        mAdapter.setReadCallback(item -> {
            // 处理已读

            showProgress("正在阅读...", false);
            String id = "";
            if (item.getId() != null) id = item.getId().toString();
            NoticeModel.get().read(id, new DataCallback<String>() {
                @Override
                public void onSuccess(String data) {
                    if (!isActive()) return;
                    hideProgress();
                    ToastTool.showLong("已读成功");
                    item.setReadStatus(true);
                    mAdapter.notifyItemChanged(mAdapter.getItemPosition(item));
                }

                @Override
                public void onError(String error) {
                    super.onError(error);
                    if (!isActive()) return;
                    hideProgress();
                }
            });
        });
        initBaseListView(mBinding.refreshLayout, mBinding.recyclerView, mAdapter, true);
    }

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


    private int page = 1;

    private void getData(boolean refresh) {
        if (refresh) page = 1;
        else page++;

        NoticeModel.get().getList(page, new DataCallback<List<NoticeBean>>() {
            @Override
            public void onSuccess(List<NoticeBean> data) {
                if (!isActive()) return;

                if (refresh) 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();
            }
        });
    }

}
