package com.unis.sxzcl.ui.announce;

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

import com.unis.model.AnnounceModel;
import com.unis.model.DataCallback;
import com.unis.model.bean.AnnounceBean;
import com.unis.sxzcl.R;
import com.unis.sxzcl.base.BaseListFragment;
import com.unis.sxzcl.databinding.FragmentBaseListTitleBinding;

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

import java.util.List;


/**
 * 公告列表
 * 创建人: mQ
 * 时间: 2020/12/5 11:07
 */
public class AnnounceListFragment extends BaseListFragment<FragmentBaseListTitleBinding> {

    private AnnounceListAdapter 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() {
        EventBus.getDefault().register(this);
        mBinding.bar.setTitle("公告列表")
                .setLeftClickListener(v -> onBack());

        mAdapter = new AnnounceListAdapter();
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            AnnounceBean item = mAdapter.getData().get(position);

            // 跳转到详情
            Bundle bundle = new Bundle();
            bundle.putString("data", getGson().toJson(item));
            navigation(R.id.announceDetailFragment, bundle);
        });

        initBaseListView(mBinding.refreshLayout, mBinding.recyclerView, mAdapter, true);
    }

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

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

    private int page = 1;

    private void getData(boolean refresh) {
        if (refresh) page = 1;
        else page++;
        AnnounceModel.get().getList(page, new DataCallback<List<AnnounceBean>>() {
            @Override
            public void onSuccess(List<AnnounceBean> 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();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReadEvent(AnnounceBean event) {
        if (event == null) {
            onRefresh();
            return;
        }
        for (AnnounceBean bean : mAdapter.getData()) {
            if (bean.getId().equals(event.getId())) {
                // 将刷新的选项置为已读
                bean.setReadStatus(true);
                mAdapter.notifyItemChanged(mAdapter.getItemPosition(bean));
                break;
            }
        }
    }

}
