package com.example.jmastermind.Game;

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

public class Model extends View {
    private List<Circle> listOfNormalCircle;
    private List<Circle> listOfCheckCircle;
    private DeckWinner winner;
    private AppCompatActivity context;

    /**
     * Le model du jeu
     * @param context Le contexte.
     * */
    public Model(AppCompatActivity context) {
        super(context);
        this.context = context;
        this.listOfNormalCircle = new LinkedList<>();
        this.listOfCheckCircle = new LinkedList<>();
        this.winner = null;
    }

    /**
     * Genere une combinaison aleatoire.
     * */
    public void genWinner() {
        Integer[] color = new Integer[]{0, 0, 0, 0};

        for(int i = 0; i <= 3; i++) {
            Random rand = new Random();
            int ran = rand.nextInt(6) + 1;

            switch(ran) {
                case 1: {
                    color[i] = Color.RED;
                    break;
                }

                case 2: {
                    color[i] = Color.YELLOW;
                    break;
                }

                case 3: {
                    color[i] = Color.BLUE;
                    break;
                }

                case 4: {
                    color[i] = Color.GREEN;
                    break;
                }

                case 5: {
                    color[i] = Color.WHITE;
                    break;
                }

                case 6: {
                    color[i] = Color.BLACK;
                    break;
                }

                case 7: {
                    color[i] = Color.GRAY;
                    break;
                }

                default: {
                    break;
                }
            }
        }

        this.winner = new DeckWinner(color[1], color[0], color[2], color[3]);
    }

    /**
     * Recupere le deck gagant du jeu.
     * @return DeckWinner Le deck gagant du jeu.
     * */
    public DeckWinner getWinner() {
        return this.winner;
    }

    /**
     * Demarrage du Model.
     * @return Si oui ou non ca a fonctionné.
     * */
    public Boolean init() {
        try {
            for(int i = 1; i <= 10; i++) {
                for(int j = 1; j <= 4; j++) {
                    int normalId = this.context.getResources().
                            getIdentifier("normalCircle" + i + j, "id", this.context.getPackageName());
                    Circle normal = this.context.findViewById(normalId);

                    int checkId = this.context.getResources()
                            .getIdentifier("checkCircle" + i + j, "id", this.context.getPackageName());
                    Circle check = this.context.findViewById(checkId);

                    this.listOfNormalCircle.add(normal);
                    this.listOfCheckCircle.add(check);
                }
            }

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

    /**
     * Recuperation des cercles normaux du jeu (id="normalCircleXY" du xml).
     * @return La liste des references vers les cercles normaux.
     * */
    public List<Circle> getListOfNormalCircle() {
        return this.listOfNormalCircle;
    }

    /**
     * Recuperation des clercles de verification du jeu (noir et blanc) (id="checkCircleXY" du xml).
     * @return La liste des references vers les cercles de verification.
     * */
    public List<Circle> getListOfCheckCircle() {
        return this.listOfCheckCircle;
    }
}
