package com.design.coderbeanliang.swipeloadrecyclerviewdemo;

import android.content.Context;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.design.coderbeanliang.swipeloadrecyclerview.CommAdapter;
import com.design.coderbeanliang.swipeloadrecyclerview.CommViewHolder;
import com.design.coderbeanliang.swipeloadrecyclerview.SwipeLoadRecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private SwipeLoadRecyclerView swipeLoadRecyclerView;
    private MyAdapter myAdapter;

    private List<Person> persons = new ArrayList<>();

    private TextView header;
    private TextView footer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContext = this;

        swipeLoadRecyclerView = (SwipeLoadRecyclerView) findViewById(R.id.swipeloadrecyclerview);

        header = new TextView(this);
        LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200);
        header.setLayoutParams(textParams);
        header.setText("Header");
        header.setBackgroundColor(getResources().getColor(R.color.colorAccent));

        footer = (TextView) LayoutInflater.from(this).inflate(R.layout.item_footer_end_my, swipeLoadRecyclerView, false);

        myAdapter = new MyAdapter(mContext, persons, R.layout.item_list_person);

        swipeLoadRecyclerView.setPageVolume(20);
        swipeLoadRecyclerView.setAdapter(myAdapter);
        swipeLoadRecyclerView.setAdapterHeader(header);
        swipeLoadRecyclerView.setAdapterFooter(footer);
        swipeLoadRecyclerView.setShowHeaderWhenNoData(false);
        swipeLoadRecyclerView.setOnLoadListener(new SwipeLoadRecyclerView.OnLoadListener() {
            @Override
            public void swipeLoadFromPage(final int page) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getData(page);
                    }
                }, 50);
            }
        });
        swipeLoadRecyclerView.onRefresh();
    }


    // Your personal adapter must extends 'CommAdapter'
    private class MyAdapter extends CommAdapter {
        public MyAdapter(Context context, List datas, int itemLayoutId) {
            super(context, datas, itemLayoutId);
        }

        @Override
        public void convert(CommViewHolder holder, Object t, int position) {
            if (t instanceof Person) {
                TextView tvId = holder.getView(R.id.tv_item_list_person_id);
                tvId.setText(((Person) t).getId());
                TextView tvName = holder.getView(R.id.tv_item_list_person_name);
                tvName.setText(((Person) t).getName());
            }
        }
    }

    private void getData(int page) {
        List<Person> datas = new ArrayList<>();
        for (int i = 0; i < 19; i++) {
            Person person = new Person("ID:"+(i + 20 * (page-1)), "Page:"+page);
            datas.add(person);
        }
        header.setText("Header"+page);

        int i = new Random().nextInt(2);
        if (i == 0) {
            footer.setText("没有更多了");
        } else {
            footer.setText("No more");
        }
        swipeLoadRecyclerView.update(datas);

//        if (page == 3) {
////            swipeLoadRecyclerView.setHeaderChangeable(false);
//            swipeLoadRecyclerView.netError();
//        } else {
//            swipeLoadRecyclerView.update(datas);
//        }
    }
}
