package com.kfzs.duanduan.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.kfzs.duanduan.utils.ListViewUtil;


/**
 * @ Created by Dlg
 * @ <p>TiTle:  ListMore</p>
 * @ <p>Description: 加载更多和上拉更多</p>
 * @ date:  2015/11/15 0:08
 * @ QQ:    315096953
 */
public class ListMore extends SwipeRefreshLayout {

    private ListView mListView;
    private boolean mRefreshEnable;
    private boolean mPullMoreEnable;
    private OnListMoreListener mOnListMoreListener;
    private boolean mInitOver;
    private ListViewUtil mListViewUtil = new ListViewUtil();
    private boolean mIsSetEmptyView = false;

    public ListMore(Context context) {
        super(context);
        init();
    }

    public ListMore(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        if (mInitOver) {
            return;
        }
        mInitOver = true;
        //只初始化一次。

        FrameLayout layout = new FrameLayout(getContext());
        layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams
                .MATCH_PARENT));

        mListView = new ListView(getContext());
        mListView.setOnScrollListener(cls);
        this.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (mOnListMoreListener != null) {
                    mOnListMoreListener.onRefresh();
                }
            }
        });
        mListView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams
                .MATCH_PARENT));

        layout.addView(mListView);
        this.addView(layout);
    }

    public ListView getmListView() {
        return mListView;
    }

    public boolean ismPullMoreEnable() {
        return mPullMoreEnable;
    }

    public void setmPullMoreEnable(boolean mPullMoreEnable) {
        this.mPullMoreEnable = mPullMoreEnable;
    }

    public boolean ismRefreshEnable() {
        return mRefreshEnable;
    }

    @Override
    public void setRefreshing(boolean refreshing) {
        super.setRefreshing(refreshing);
        if (!refreshing && !mIsSetEmptyView) {
            mIsSetEmptyView = true;
            mListViewUtil.setEmptyView(getContext(), mListView);
        }
    }

    public void setmRefreshEnable(boolean mRefreshEnable) {
        this.mRefreshEnable = mRefreshEnable;
    }

    public OnListMoreListener getmOnListMoreListener() {
        return mOnListMoreListener;
    }

    public void setmOnListMoreListener(OnListMoreListener mOnListMoreListener) {
        this.mOnListMoreListener = mOnListMoreListener;
    }

    private AbsListView.OnScrollListener cls = new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//            LogUtil.e(firstVisibleItem + "|" + visibleItemCount + "|" + totalItemCount + "|" +
//                    isRefreshing() + "|" + ismPullMoreEnable());
//            if (mOnListMoreListener != null && totalItemCount > 0
//                    && ismPullMoreEnable() && !isRefreshing()
//                    && firstVisibleItem + visibleItemCount > totalItemCount - 1) {
//                setRefreshing(true);
//                mOnListMoreListener.onLoadMore();
//            }
            if(mOnListMoreListener != null && !isRefreshing()){
                if(firstVisibleItem == 0){
                    ListMore.this.setEnabled(true);
                    return;
//                    setRefreshing(true);
//                    mOnListMoreListener.onRefresh();
                }else if( totalItemCount > 0 && ismPullMoreEnable() && firstVisibleItem + visibleItemCount > totalItemCount - 1){

                    setRefreshing(true);
                    mOnListMoreListener.onLoadMore();
                    ListMore.this.setEnabled(true);
                    return;
                }
            }
            if (view.getChildCount() < 1) {
                ListMore.this.setEnabled(true);
                return;
            }
            ListMore.this.setEnabled(view.getFirstVisiblePosition() == 0
                    && view.getChildAt(0).getTop() > view.getPaddingTop() - 3);
        }
    };

    /**
     * 上拉和下拉的刷新监听
     */
    public interface OnListMoreListener {
        void onRefresh();

        void onLoadMore();
    }

    public ListViewUtil getmListViewUtil() {
        return mListViewUtil;
    }

    /**
     * 设置间隔
     */
    public void setDrivers(Drawable drawable) {

        mListView.setDivider(drawable);
    }
}
