package com.zhaohaoting.framework.abs;

import android.view.View;
import android.widget.AdapterView;

import com.shizhefei.view.coolrefreshview.header.MaterialHeader;
import com.zhaohaoting.framework.mvchelper.mvc.IAsyncDataSource;
import com.zhaohaoting.framework.mvchelper.mvc.IDataAdapter;
import com.zhaohaoting.framework.mvchelper.mvc.OnLoadMoreStateChangeListener;
import com.zhaohaoting.framework.mvchelper.mvc.OnRefreshStateChangeListener;
import com.zhaohaoting.framework.mvchelper.mvc.OnStateChangeListener;
import com.zhaohaoting.framework.R;
import com.zhaohaoting.framework.abs.presenter.BasePresenter;
import com.zhaohaoting.framework.databinding.WidgetListBinding;
import com.zhaohaoting.framework.ui.helper.MVCCoolHelper;

/**
 * author：Created by ZHT on 2017/11/1 17:15
 * email：526309416@qq.com
 * desc：列表类activity基类 RESULT_DATA为BaseListEntity<DATA>或者List<DATA>
 */
@Deprecated
@SuppressWarnings("unchecked")
public abstract class BaseListFragment<RESULT_DATA> extends AbsV4Fragment implements AdapterView.OnItemClickListener {

    private MVCCoolHelper mvcHelper;
    public IDataAdapter<RESULT_DATA> adapter;
    private boolean isVisibleToUser = false;
    protected boolean loaded;//是否已经加载了数据
    public boolean login = true;//登录标志，true:已经登录，false：未登录
    protected IAsyncDataSource<RESULT_DATA> dataSource;
    private WidgetListBinding binding;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        if (!isVisibleToUser || adapter == null || loaded)
            return;//若当前Fragment不在最前面，或者adapter为null或者已经加载过数据了，就直接返回出去
        mvcHelper.refresh();
        loaded = true;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.widget_list;
    }

    @Override
    protected void initialize() {
        binding = (WidgetListBinding) rootDataBinding;
        binding.funnyRefreshView.setPullHeader(new MaterialHeader(getActivity()),true);
//        binding.funnyRefreshView.setPullHeader(new DefaultHeader());
        mvcHelper = new MVCCoolHelper<>(binding.funnyRefreshView);
        getLifecycle().addObserver(mvcHelper);//添加LifecycleObserver
        onInit();
        mvcHelper.setDataSource(dataSource = getDataSource());
        adapter = getAdapter();
//        if (resultCls.isAssignableFrom(List.class)) {
//            mvcHelper.setAdapter2(adapter, null);
//        } else if (resultCls.isAssignableFrom(BaseListEntity.class)) {
//            mvcHelper.setAdapter2(adapter, adapter);
//        }
        mvcHelper.setAdapter(adapter);
        //https://github.com/LuckyJayce/MVCHelper/blob/master/Info.md
        if (isVisibleToUser) {
            mvcHelper.refresh();
            loaded = true;
        }
        binding.listView.setOnItemClickListener(this);
    }

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    /**
     * 刷新
     */
    public void refresh() {
        mvcHelper.refresh();
    }

    protected abstract void onInit();

    protected abstract IDataAdapter<RESULT_DATA> getAdapter();

    protected abstract IAsyncDataSource<RESULT_DATA> getDataSource();

    /**
     * 设置状态监听，监听开始刷新，刷新成功，开始加载更多，加载更多成功
     *
     * @param onStateChangeListener
     */
    public void setOnStateChangeListener(OnStateChangeListener<RESULT_DATA> onStateChangeListener) {
        mvcHelper.setOnStateChangeListener(onStateChangeListener);
    }

    /**
     * 设置状态监听，监听开始刷新，刷新成功
     *
     * @param onRefreshStateChangeListener
     */
    public void setOnStateChangeListener(OnRefreshStateChangeListener<RESULT_DATA> onRefreshStateChangeListener) {
        mvcHelper.setOnStateChangeListener(onRefreshStateChangeListener);
    }

    /**
     * 设置状态监听，监听开始加载更多，加载更多成功
     *
     * @param onLoadMoreStateChangeListener
     */
    public void setOnStateChangeListener(OnLoadMoreStateChangeListener<RESULT_DATA> onLoadMoreStateChangeListener) {
        mvcHelper.setOnStateChangeListener(onLoadMoreStateChangeListener);
    }

    @Override
    public final void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int headCount = binding.listView.getHeaderViewsCount();
        if (position < headCount) return;
        position = position - headCount;
        itemClicked(parent, view, position, id);
    }

    protected void itemClicked(AdapterView<?> parent, View view, int position, long id) {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
