package com.zhaohaoting.framework.abs;

import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.shizhefei.view.coolrefreshview.CoolRefreshView;
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.ui.helper.MVCCoolHelper;

import java.lang.reflect.ParameterizedType;


/**
 * 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 BaseListActivity<RESULT_DATA> extends BaseActivity implements AdapterView.OnItemClickListener {

    protected MVCCoolHelper<RESULT_DATA> mvcHelper;
    private CoolRefreshView coolRefreshView;
    public LinearLayout ll_content;
    public FrameLayout fl_content;
    public ListView listView;
    public IDataAdapter<RESULT_DATA> adapter;
    protected IAsyncDataSource<RESULT_DATA> dataSource;

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

    @Override
    protected void initialize() {
        coolRefreshView = (CoolRefreshView) findViewById(R.id.funnyRefreshView);
        listView = (ListView) findViewById(R.id.listView);
        ll_content = (LinearLayout) findViewById(R.id.ll_content);
        fl_content = (FrameLayout) findViewById(R.id.fl_content);
        coolRefreshView.setPullHeader(new MaterialHeader(this),true);
//        coolRefreshView.setPullHeader(new DefaultHeader()); //DefaultHeader有显示异常问题 但影响不大 若要解决 网上找到 https://github.com/LuckyJayce/CoolRefreshView
        //重新导入library 注释掉DefaultHeader 第74行代码
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        getLifecycle().addObserver(mvcHelper);//添加LifecycleObserver
        onInit();
        Class<RESULT_DATA> resultCls = (Class<RESULT_DATA>) ((ParameterizedType) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1]).getRawType();
        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.setAdapter2(adapter, adapter);
        refresh();
        listView.setOnItemClickListener(this);
    }

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

    protected void refresh() {
        mvcHelper.refresh();
    }

    /**
     * 设置状态监听，监听开始刷新，刷新成功，开始加载更多，加载更多成功
     *
     * @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);
    }

    protected abstract void onInit();

    protected abstract IDataAdapter<RESULT_DATA> getAdapter();

    protected abstract IAsyncDataSource<RESULT_DATA> getDataSource();


    @Override
    public final void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int headCount = 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();
    }
}