package com.bokecc.livemodule.replay.summary.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bokecc.livemodule.R;
import com.bokecc.livemodule.utils.TimeUtil;

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


public class SummaryListAdapter extends RecyclerView.Adapter<SummaryListAdapter.SummaryViewHolder> {

    private LayoutInflater mInflater;
    private List<SummaryItemBean> list;
    private Context context;
    private OnItemClickListener onItemClickListener;
    private int currentSelectPosition = -1;


    public SummaryListAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        list = new ArrayList<>();
    }

    public void setList(List<SummaryItemBean> list) {
        this.list = list;
        if (list != null && list.size() > 0) {
            selectItem(0);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    private static final String TAG = "SummaryListAdapter";
    public void selectItem(int position) {

        Log.i(TAG, "selectItem:" + position);
        if (currentSelectPosition == position) {
            return;
        }
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setSelect(false);
            }
            currentSelectPosition = position;
            list.get(position).setSelect(true);
            notifyDataSetChanged();
        }
    }

    public void selectItemByTime(long time) {

        if (list != null && list.size() > 0) {
            int selectPosition = 0;
            for (int i = 0; i < list.size(); i++) {
                if (time >= list.get(i).getTime()) {
                    selectPosition = i;
                }else {
                    break;
                }
            }
            selectItem(selectPosition);
        }
    }

    @Override
    public SummaryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = null;
        itemView = mInflater.inflate(R.layout.item_summary_list, parent, false);
        return new SummaryViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final SummaryViewHolder holder, int position) {

        final int pos = position;
        final SummaryItemBean summaryItemBean = list.get(pos);
        resetItemLayout(holder);

        if (summaryItemBean != null) {
            if (!TextUtils.isEmpty(summaryItemBean.getTitle())) {
                holder.title.setText(summaryItemBean.getTitle());
            }
            holder.time.setText(TimeUtil.getFormatTime(summaryItemBean.getTime()));

            if (summaryItemBean.isSelect()) {
                holder.title.setTextColor(context.getResources().getColor(R.color.text_list_item_select));
                holder.time.setTextColor(context.getResources().getColor(R.color.text_list_item_select));
            } else {
                holder.title.setTextColor(context.getResources().getColor(R.color.text_primary));
                holder.time.setTextColor(context.getResources().getColor(R.color.text_primary));
            }
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectItem(pos);
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(pos, summaryItemBean);
                }
            }
        });

    }

    private void resetItemLayout(SummaryViewHolder holder) {
        holder.title.setText("");
        holder.time.setText("");

        holder.title.setTextColor(context.getResources().getColor(R.color.text_primary));
    }


    public interface OnItemClickListener {
        void onItemClick(int position, SummaryItemBean summaryItemBean);
    }


    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    final class SummaryViewHolder extends RecyclerView.ViewHolder {

        TextView title;
        TextView time;


        SummaryViewHolder(View itemView) {
            super(itemView);

            time = itemView.findViewById(R.id.tv_summary_time);
            title = itemView.findViewById(R.id.tv_summary_title);

        }
    }
}
