package com.android.chmo.ui.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.android.chmo.R;
import com.android.chmo.utils.CommonUtils;
import com.scwang.smartrefresh.header.WaterDropHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

/**
 * Created by Administrator on 2018/7/31.
 */

public class RefreshListView extends LinearLayout{
    private SmartRefreshLayout refreshLayout;
    private ListView listView;


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


    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.refresh_list,this);
        refreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
        listView = (ListView)findViewById(R.id.listView);

        refreshLayout.setRefreshHeader(new WaterDropHeader(getContext()));
        refreshLayout.setPrimaryColorsId(R.color.pink);
        ClassicsFooter footer = new ClassicsFooter(getContext());
        footer.setTextSizeTitle(12);
        refreshLayout.setRefreshFooter(footer);
        refreshLayout.setNoMoreData(true);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(ev.getAction() == MotionEvent.ACTION_DOWN) {
            CommonUtils.closeKeybord(this,getContext());
        }
        return super.onInterceptTouchEvent(ev);
    }

    public ListView getListView(){
        return listView;
    }

    public void setRefreshListener(final RefreshListener listener) {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                listener.onRefresh();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(final RefreshLayout refreshLayout) {
                listener.onLoadMore();
            }
        });
    }

    public void setEnableLoadMore(boolean enable) {
        refreshLayout.setEnableLoadMore(enable);
        refreshLayout.setNoMoreData(!enable);
    }


    public void finishRefresh() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();

    }

    public void autoRefresh() {
        refreshLayout.autoRefresh();
    }

    public void setNoMoreData() {
        refreshLayout.setNoMoreData(true);
    }

    public void setAdapter(ListAdapter adapter) {
        listView.setAdapter(adapter);
    }


    public interface RefreshListener {
        void onRefresh();
        void onLoadMore();
    }
}


