package com.tencent.liteav.demo.player.ui;

import android.content.Context;

import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Color;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tencent.liteav.demo.player.R;
import com.tencent.liteav.demo.superplayer.SuperPlayerModel;

import java.util.ArrayList;

/**
 * Created by liyuejiao on 2018/7/3.
 */

public class PlayerListAdapter extends RecyclerView.Adapter<PlayerListAdapter.ViewHolder> {
    private static final int activeColor = Color.parseColor("#FF0048B1");
    private static final int inactiveColor = Color.parseColor("#FF495568");

    private Context mContext;

    private OnItemClickListener mOnItemClickListener;

    private ArrayList<SuperPlayerModel> mVideoModelList;
    private ArrayList<Boolean> clickMark;

    public PlayerListAdapter(Context context) {
        mContext = context;
        mVideoModelList = new ArrayList<>();
        clickMark = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.superplayer_item_vod, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final SuperPlayerModel videoModel = mVideoModelList.get(position);
        if (TextUtils.isEmpty(videoModel.cover)) {
            Glide.with(mContext)
                    .load(R.mipmap.default_cover_light)
                    .into(holder.thumb);
        } else {
            Glide.with(mContext)
                    .load(videoModel.cover)
                    .into(holder.thumb);
        }

        if (videoModel.title != null) {
            holder.title.setText(videoModel.title);
        }

        if (clickMark.get(position)) {
            holder.title.setTextColor(activeColor);
            holder.title.setTypeface(Typeface.DEFAULT_BOLD);
        } else {
            holder.title.setTextColor(inactiveColor);
            holder.title.setTypeface(Typeface.DEFAULT);
        }

        holder.title.setOnClickListener(v -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(videoModel,position);
            }
        });

        holder.thumb.setOnClickListener(v -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(videoModel,position);
            }
        });
    }

    public void setIndex(int index){
        for (int i = 0; i < clickMark.size(); i++) {
            clickMark.set(i, false);
        }

        clickMark.set(index, true);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mVideoModelList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView title;
        private ImageView thumb;

        public ViewHolder(final View itemView) {
            super(itemView);
            thumb = (ImageView) itemView.findViewById(R.id.superplayer_iv);
            title = (TextView) itemView.findViewById(R.id.superplayer_tv);
        }
    }

    /**
     * 添加一个SuperPlayerModel
     *
     * @param videoListModel
     */
    public void addSuperPlayerModel(SuperPlayerModel videoListModel) {
        notifyItemInserted(mVideoModelList.size());
        mVideoModelList.add(videoListModel);
        clickMark.add(false);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public void clear() {
        mVideoModelList.clear();
    }

    public interface OnItemClickListener {

        void onItemClick(SuperPlayerModel videoModel,int index);
    }
}
