package com.example.jmastermind.Events;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import com.example.jmastermind.Game.Game;
import com.example.jmastermind.Geometrics.Circle;
import com.example.jmastermind.Menu.Menu;
import com.example.jmastermind.R;
import java.util.ArrayList;
import java.util.List;

public class DefendValidateEvent implements View.OnClickListener {
    private DefendCombiSelectorEvent dce;
    private AppCompatActivity context;

    /**
     * Validation de la combinason du mode multi.
     * @param context Le contexte.
     * @param dce L'event permettant de selectionner des pions (qui ont pour attribut la combinaison faites par un des 2 joueurs.
     * */
    public DefendValidateEvent(AppCompatActivity context, DefendCombiSelectorEvent dce) {
        this.context = context;
        this.dce = dce;
    }

    @Override
    public void onClick(View view) {
        int idOfButton = view.getId();
        Button btnClicked = (Button) this.context.findViewById(idOfButton);
        CharSequence text = btnClicked.getText();

        if ("Valider".equals(text)) {
            if(this.dce.getCombinaison() != null) {
                List<Integer> defendColor = this.dce.getCombinaison();

                // Merci StackOverflow: https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application
                Bundle bundle = new Bundle();
                ArrayList<Integer> arr = new ArrayList<>(defendColor);
                bundle.putIntegerArrayList("DefendCombi", arr);

                Intent i = new Intent(this.context, Game.class);
                i.putExtras(bundle);

                this.context.startActivity(i);
            }
        } else if ("Effacer".equals(text)) {
            Circle one = this.context.findViewById(R.id.receiveOne);
            one.setColor(Color.GRAY);

            Circle two = this.context.findViewById(R.id.receiveTwo);
            two.setColor(Color.GRAY);

            Circle three = this.context.findViewById(R.id.receiveThree);
            three.setColor(Color.GRAY);

            Circle four = this.context.findViewById(R.id.receiveFour);
            four.setColor(Color.GRAY);

            this.dce.reset();
        } else if("Retour".equals(text)) {
            Intent i = new Intent(this.context, Menu.class);
            this.context.startActivity(i);
        }
    }
}
