package com.example.jmastermind.Game;

import android.content.SharedPreferences;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceManager;

import com.example.jmastermind.Events.ColorSelectorEvent;
import com.example.jmastermind.Events.ControlEvent;
import com.example.jmastermind.Geometrics.Circle;
import com.example.jmastermind.R;
import java.util.List;

public class Vuew {
    private boolean canVoid;
    private ColorSelectorEvent event;
    private List<Circle> normalCircle;
    private List<Circle> checkedCircle;
    private AppCompatActivity context;
    private ControlEvent controlEvent;

    /**
     * La vue du jeu.
     * @param context Le context.
     * @param normalCircle Les references vers les cercles normaux.
     * @param checkCircle Les references vers les cercles de verification.
     * @param event L'event de la selection de couleur.
     * @param controlEvent L'event vers les deux boutons Valider et Effacer.
     * */
    public Vuew(AppCompatActivity context, List<Circle> normalCircle, List<Circle> checkCircle, ColorSelectorEvent event, ControlEvent controlEvent) {
        this.context = context;
        this.normalCircle = normalCircle;
        this.checkedCircle = checkCircle;
        this.event = event;
        this.controlEvent = controlEvent;

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this.context);
        this.canVoid  = sp.getBoolean("canVoid", false);
    }

    /**
     * Parametrage du plateau de jeu
     * */
    public Boolean init() {
        try {
            Circle yellow = (Circle) this.context.findViewById(R.id.yellow);
            yellow.setColor(Color.YELLOW);
            yellow.setOnClickListener(this.event);

            Circle red = (Circle) this.context.findViewById(R.id.red);
            red.setColor(Color.RED);
            red.setOnClickListener(this.event);

            Circle green  = (Circle) this.context.findViewById(R.id.green);
            green.setColor(Color.GREEN);
            green.setOnClickListener(this.event);

            Circle blue = (Circle) this.context.findViewById(R.id.blue);
            blue.setColor(Color.BLUE);
            blue.setOnClickListener(this.event);

            Circle white = (Circle) this.context.findViewById(R.id.white);
            white.setColor(Color.WHITE);
            white.setOnClickListener(this.event);

            Circle black = (Circle) this.context.findViewById(R.id.black);
            black.setColor(Color.BLACK);
            black.setOnClickListener(this.event);

            Circle gray = (Circle) this.context.findViewById(R.id.gray);
            if(this.canVoid) {
                gray.setColor(Color.GRAY);
                gray.setOnClickListener(this.event);
            } else {
                gray.setVisibility(View.GONE);
            }

            Button validate = (Button) this.context.findViewById(R.id.validate);
            validate.setOnClickListener(this.controlEvent);

            Button reset = (Button) this.context.findViewById(R.id.reset);
            reset.setOnClickListener(this.controlEvent);

            Button leave = (Button) this.context.findViewById(R.id.leave);
            leave.setOnClickListener(this.controlEvent);

            return true;
        } catch(NullPointerException e) {
            System.out.println("[!] Probleme lors de l'initiliation de la vue (NullPointerException).");
            return false;
        } catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("[!] Probleme lors de l'initiliation de la vue (ArrayIndexOutOfBoundsException).");
            return false;
        }
    }
}
