package com.example.jmastermind.Events;

import android.graphics.Color;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import com.example.jmastermind.Geometrics.Circle;
import com.example.jmastermind.R;
import java.util.LinkedList;
import java.util.List;

public class DefendCombiSelectorEvent implements View.OnClickListener{
    private AppCompatActivity context;
    private int pos;
    private List<Integer> refs;

    /**
     * Event relier a la selection de la combinason gagnte du mode multi.
     * @param context Le context.
     * */
    public DefendCombiSelectorEvent(AppCompatActivity context) {
        this.context = context;
        this.refs = new LinkedList<>();
        this.pos = 0;

        this.refs.add(R.id.receiveOne);
        this.refs.add(R.id.receiveTwo);
        this.refs.add(R.id.receiveThree);
        this.refs.add(R.id.receiveFour);
    }

    /**
     * Remettre le compteur a zero si le joueur reset sa combinaison.
     * */
    public void reset() {
        this.pos = 0;
    }

    /**
     * Recuperer la combinaison
     * @return La liste des couleurs choisit par le defenseur
     * */
    public List<Integer> getCombinaison() {
        if(this.pos == 4) {
            List<Integer> tmp = new LinkedList<>();

            for(int i = 0; i <= this.pos-1; i++) {
                Circle c = this.context.findViewById(this.refs.get(i));
                tmp.add(c.getColor());
            }

            return tmp;
        } else {
            return null;
        }
    }

    @Override
    public void onClick(View view) {
        if(this.pos <= 3) {
            int id = view.getId();
            Circle c = this.context.findViewById(id);
            Integer chose = c.getColor();

            Circle toModify = (Circle) this.context.findViewById(this.refs.get(this.pos));

            switch(chose) {
                case Color.BLACK: {
                    toModify.setColor(Color.BLACK);
                    break;
                }

                case Color.WHITE: {
                    toModify.setColor(Color.WHITE);
                    break;
                }

                case Color.GREEN: {
                    toModify.setColor(Color.GREEN);
                    break;
                }
                case Color.BLUE: {
                    toModify.setColor(Color.BLUE);
                    break;
                }

                case Color.RED: {
                    toModify.setColor(Color.RED);
                    break;
                }

                case Color.YELLOW: {
                    toModify.setColor(Color.YELLOW);
                    break;
                }

                default: {
                    break;
                }
            }

            this.pos++;
        }
    }
}
