package com.mayi.fastdevelop.base;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mayi.fastdevelop.R;
import com.mayi.fastdevelop.view.CustomTitleBar;
import com.mayi.fastdevelop.view.OnMultiClickListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

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

//base列表页
public abstract class BaseListActivity<T, VH extends BaseViewHolder<T>> extends BaseActivity {

    protected TextView loadMsg;
    protected CustomTitleBar title;
    protected RecyclerView recyclerView;
    protected SmartRefreshLayout refreshLayout;
    protected Handler handler;
    protected int pageNumber = 1;//页码
    protected boolean isLoadMore;//是否正在加载更多
    protected LinearLayoutManager manager;
    protected long loadMoreInterval = 1000;
    protected List<T> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.base_list_activity);
        handler = new Handler();
        loadMsg = findViewById(R.id.tv_load_msg);
        title = findViewById(R.id.title);
        recyclerView = findViewById(R.id.recycler_view);
        refreshLayout = findViewById(R.id.refresh_layout);
        manager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(new RecyclerView.Adapter<VH>() {

            @NonNull
            @Override
            public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return createVH(parent, viewType);
            }

            @Override
            public void onBindViewHolder(@NonNull VH holder, int position) {
                holder.onBindView(list.get(position));
            }

            @Override
            public int getItemViewType(int position) {
                return getItemType(list.get(position));
            }

            @Override
            public int getItemCount() {
                return list.size();
            }
        });
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setEnableLoadmore(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                onRefreshData();
            }
        });
        title.setOnClickLeftViewListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                finish();
            }
        });
        startLoadMoreData();
    }

    public void showLoadMsg(String msg) {
        loadMsg.setVisibility(View.VISIBLE);
        loadMsg.setText(msg);
    }

    public void dismssLoadMsg() {
        if (loadMsg.getVisibility() == View.VISIBLE) {
            AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
            alphaAnimation.setDuration(2000);
            alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    loadMsg.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });
            loadMsg.startAnimation(alphaAnimation);
        }
    }

    //创建VH
    protected abstract VH createVH(ViewGroup parent, int viewType);

    //刷新数据
    public void notifyData() {
        recyclerView.getAdapter().notifyDataSetChanged();
    }

    //完成刷新
    public void finishRefresh() {
        refreshLayout.finishRefresh();
    }

    public void startLoadMoreData() {
        stopLoadMoreData();
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (!isLoadMore && recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_IDLE) {
                    if (manager.findLastVisibleItemPosition() + 5 > recyclerView.getAdapter().getItemCount()) {
                        isLoadMore = true;
                        showLoadMsg("加载更多数据中。。。");
                        loadMoreData(pageNumber);
                    }
                    if (manager.findLastVisibleItemPosition() == recyclerView.getAdapter().getItemCount()) {

                    } else {

                    }
                }
                handler.postDelayed(this, loadMoreInterval);
            }
        });
    }

    public void stopLoadMoreData() {
        handler.removeCallbacksAndMessages(null);
    }

    //根据item获取type,默认不需要重写
    public int getItemType(T t) {
        return 0;
    }

    //刷新数据
    protected abstract void onRefreshData();

    //根据页码加载更多数据
    protected abstract void loadMoreData(int pageNumber);

    @Override
    protected void onDestroy() {
        stopLoadMoreData();
        super.onDestroy();
    }
}
