package com.xunao.effectdemo.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.xunao.effectdemo.R;

import java.util.List;

public class RolePlayAdapter extends RecyclerView.Adapter<RolePlayAdapter.ViewHolder> {
    private List<String> roleList;
    private final Context context;
    private int currentIndex = 0;

    public RolePlayAdapter(List<String> mList, Context context) {
        this.currentIndex = currentIndex;
        this.roleList = mList;
        this.context = context;
    }

    @NonNull
    @Override
    public RolePlayAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_role,viewGroup,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RolePlayAdapter.ViewHolder viewHolder, int i) {
        String data = roleList.get(i);
        viewHolder.tvRoleName.setText(data);
        if(currentIndex == i){
            viewHolder.llRole.setBackground(ResourcesCompat.getDrawable(context.getResources(),R.drawable.role_border_yellow_bg,null));
        }else{
            viewHolder.llRole.setBackground(ResourcesCompat.getDrawable(context.getResources(),R.drawable.role_bg,null));
        }
    }


    @Override
    public int getItemCount() {
        return roleList.isEmpty() ? 0 : roleList.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvRoleName;
        private LinearLayout llRole;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvRoleName = itemView.findViewById(R.id.tv_role_name);
            llRole = itemView.findViewById(R.id.ll_role);
        }
    }

    public void changeIndex(){
        currentIndex = currentIndex + 1;
        if (currentIndex >= roleList.size()) {
            currentIndex = 0;
        }
        notifyItemRangeChanged(0, roleList.size());
    }

    public int getCurrentIndex(){
        return currentIndex;
    }
}
