package com.zhaohaoting.framework.ui.helper;

import android.view.View;

import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.OnPullListener;
import com.shizhefei.view.coolrefreshview.SimpleOnPullListener;
import com.zhaohaoting.framework.mvchelper.mvc.ILoadViewFactory.ILoadMoreView;
import com.zhaohaoting.framework.mvchelper.mvc.ILoadViewFactory.ILoadView;
import com.zhaohaoting.framework.mvchelper.mvc.IRefreshView;
import com.zhaohaoting.framework.mvchelper.mvc.MVCHelper;

/**
 * author：Created by ZHT on 2017/11/1 10:35
 * email：526309416@qq.com
 * desc：适配任意view的上拉刷新
 */
public class MVCCoolHelper<DATA> extends MVCHelper<DATA> {

    public MVCCoolHelper(CoolRefreshView coolRefreshView) {
        super(new RefreshView(coolRefreshView));
    }

    public MVCCoolHelper(CoolRefreshView coolRefreshView, ILoadView loadView, ILoadMoreView loadMoreView) {
        super(new RefreshView(coolRefreshView), loadView, loadMoreView);
    }

    private static class RefreshView implements IRefreshView {
        private CoolRefreshView coolRefreshView;

        public RefreshView(CoolRefreshView coolRefreshView) {
            this.coolRefreshView = coolRefreshView;
            coolRefreshView.addOnPullListener(onPullListener);
        }

        @Override
        public View getContentView() {
            return coolRefreshView.getContentView();
        }

        @Override
        public View getSwitchView() {
            return coolRefreshView;
        }

        private OnRefreshListener onRefreshListener;

        @Override
        public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
            this.onRefreshListener = onRefreshListener;
        }

        @Override
        public void showRefreshComplete() {
            coolRefreshView.removeOnPullListener(onPullListener);
            coolRefreshView.setRefreshing(false);
            coolRefreshView.addOnPullListener(onPullListener);
        }

        @Override
        public void showRefreshing() {
            coolRefreshView.removeOnPullListener(onPullListener);
            coolRefreshView.setRefreshing(true);
            coolRefreshView.addOnPullListener(onPullListener);
        }

        private OnPullListener onPullListener = new SimpleOnPullListener() {
            @Override
            public void onRefreshing(CoolRefreshView coolRefreshView) {
                if (onRefreshListener != null) {
                    onRefreshListener.onRefresh();
                }
            }
        };
    }
}
