package com.lcp.arecyclerview;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.View;
import android.widget.Toast;

import com.lcp.arecyclerview.adapter.RcListAdapter;
import com.lcp.arecyclerview.widget.ARecycleView;
import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.lcp.arecyclerview.widget.DividerItemDecoration;

import java.util.ArrayList;

/**
 * ListView and GridView
 * Created by Aislli on 2016/2/29.
 */
public class RecyclerListActivity extends Activity {
    private ARecycleView mRecyclerView;
    private ArrayList<String> list;
    private RcListAdapter mReListAdapter;
    private RecyclerView.LayoutManager linearLayoutManager;
    Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_list);
        initView();
        initData();
    }

    private void initView() {
        mRecyclerView = (ARecycleView) findViewById(R.id.recyclerview);
    }

    private void initData() {
        int flag = getIntent().getIntExtra("flag", 0);
        list = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            list.add("item:" + i);
        }
        if (flag == 0) {//线性布局，ListView效果
            linearLayoutManager = new LinearLayoutManager(this);
            mReListAdapter = new RcListAdapter(this, list);
            mRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.HORIZONTAL));
        } else if (flag == 1) {//GridView效果
            linearLayoutManager = new GridLayoutManager(this, 3);
            mReListAdapter = new RcListAdapter(this, list);
        }
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mReListAdapter);
        mRecyclerView.setTotal(list.size() + 6);//设置数据总数，加载完了就禁止掉加载更多事件
        setListener();
    }

    int moreNum = 1;
    int refreshNum = 1;

    private void setListener() {
        //下拉刷新
        mRecyclerView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mReListAdapter.insert(refreshNum++ + "  Refresh things", 0);
                        mRecyclerView.setRefreshing(false);
                    }
                }, 1500);
            }
        });
        //上拉加载更多
        mRecyclerView.setOnLoadMoreListener(new ARecycleView.OnLoadMoreListener() {
            @Override
            public void loadMore() {
                handler.postDelayed(new Runnable() {
                    public void run() {
                        mReListAdapter.insert("More " + moreNum++, mReListAdapter.getItemCount());
                        mRecyclerView.hideMoreProgress();
                    }
                }, 1500);
            }
        });
        //点击事件
        mReListAdapter.setOnItemClickListener(new RcListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(getApplicationContext(), "position:" + position, Toast.LENGTH_SHORT).show();
            }
        });
        //长按点击
        mReListAdapter.setOnItemLongClickListener(new ARecyclerBaseAdapter.OnItemLongClickListener() {
            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(getApplicationContext(), "long position:" + position, Toast.LENGTH_SHORT).show();
                mReListAdapter.remove(position);
            }
        });
    }
}
