package com.sheep.jiuyan.samllsheep.page.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.sheep.jiuyan.samllsheep.R;


/**
 * @ 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 boolean mIsSetEmptyView = false;
    private int dividerHeight = 0;//divider的高度

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

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

    private void init(AttributeSet attrs) {
        if (mInitOver) {
            return;
        }
        //只初始化一次。
        mInitOver = true;
        mListView = new ListView(getContext());
        if (attrs != null) {
            TypedArray attribute = getContext().obtainStyledAttributes(attrs,
                    R.styleable.ListMore);
            if (attribute.hasValue(R.styleable.ListMore_listSelector)) {
                mListView.setSelector(attribute.getDrawable(R.styleable.ListMore_listSelector));
            }
            if (attribute.hasValue(R.styleable.ListMore_dividerDrawable)) {
                mListView.setDivider(attribute.getDrawable(R.styleable.ListMore_dividerDrawable));
            }
            dividerHeight = attribute.getDimensionPixelOffset(R.styleable.ListMore_dividerHeight, 0);
            attribute.recycle();
        }

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


        mListView.setOnScrollListener(cls);
        mListView.setDividerHeight(dividerHeight);
        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 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 (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();
    }

}
