package com.example.jmastermind.Events;

import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.jmastermind.Deck.Deck;
import com.example.jmastermind.Deck.DeckCheck;
import com.example.jmastermind.Deck.DeckLambda;
import com.example.jmastermind.Deck.DeckWinner;
import com.example.jmastermind.Game.Popup;
import com.example.jmastermind.Geometrics.Circle;
import com.example.jmastermind.R;
import java.util.LinkedList;
import java.util.List;

public class ColorSelectorEvent extends View implements View.OnClickListener {
    private boolean canVoid;
    private AppCompatActivity context;
    /**
     * [0] = x
     * [1] = y
     * */
    private int[] coords;
    private DeckWinner winner;
    private int tentative;

    /**
     * Evenement declanché lorsque le joueur selectionne une couleur.
     * @param context Le contexte de l'app.
     * @param winner La combinaison gagnante.
     * */
    public ColorSelectorEvent(AppCompatActivity context, DeckWinner winner) {
        super(context);
        this.context = context;
        this.winner = winner;
        this.coords = new int[]{1, 1};
        this.tentative = 10;

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

        Log.d("test", this.canVoid + "");
    }

    /**
     * Relier indirectement au bouton "Valider".
     * Changer de ligne et lancer la verification des combinaisons.
     * */
    public void goNext() {
        List<Integer> listOfColors = new LinkedList<>();

        for(int i = 1; i <= 4; i++) {
            int id = this.context.getResources()
                    .getIdentifier("normalCircle" + this.coords[0] + i, "id", this.context.getPackageName());
            Circle c = this.context.findViewById(id);

            listOfColors.add(c.getColor());
        }

        if(listOfColors.contains(Color.GRAY) && !this.canVoid) {
            return;
        }

        Deck lambda = new DeckLambda(
                listOfColors.get(0),
                listOfColors.get(1),
                listOfColors.get(2),
                listOfColors.get(3)
        );

        DeckCheck checker = new DeckCheck(lambda, this.winner);
        Deck checkerPions = checker.doCheck();
        List<Integer> checkColors = checkerPions.getDeck();
        int countOfBlack = 0;

        for(int i = 1; i <= 4; i++) {
            int id = this.context.getResources()
                    .getIdentifier("checkCircle" + this.coords[0] + i, "id", this.context.getPackageName());
            Circle c = this.context.findViewById(id);

            c.setColor(checkColors.get(i-1));

            if(checkColors.get(i-1) == Color.BLACK) {
                countOfBlack++;
            }
        }

        String[] opts = new String[] {
                "Rejouer",
                "Voir votre jeu",
                "Retourner au menu"
        };

        this.tentative--;

        if(countOfBlack == 4) {
            Popup popup = new Popup(this.context, opts, this.winner.getIsMulti(), (10 - this.tentative));
            popup.show();
        } else if(this.tentative == 0) {
            Popup popup = new Popup(this.context, opts, this.winner.getIsMulti(), -1);
            popup.show();
        }

        TextView tx = (TextView) this.context.findViewById(R.id.counter);
        tx.setText("Tentative rest. \n" + this.tentative);
        tx.invalidate();

        this.coords[1] = 1;
        this.coords[0]++;
    }

    /**
     * Relier indirectement au bouton "Effacer".
     * Permet d'effacer la combinaison non validé par le joueur.
     * */
    public void resetCombi() {
        for(int i = 1; i <= this.coords[1]; i++) {
            int id = this.context.getResources()
                    .getIdentifier("normalCircle" + this.coords[0] + i, "id", this.context.getPackageName());
            Circle c = this.context.findViewById(id);

            c.setColor(Color.GRAY);
        }

        this.coords[1] = 1;
    }

    @Override
    public void onClick(View view) {
        int idOfClickedColor = view.getId();
        Circle circleClicked = (Circle) this.context.findViewById(idOfClickedColor);
        int colorChoosed = circleClicked.getColor();

        int id = this.context.getResources()
                .getIdentifier("normalCircle" + this.coords[0] + this.coords[1], "id", this.context.getPackageName());
        Circle c = this.context.findViewById(id);

        switch(colorChoosed) {
            case Color.RED: {
                c.setColor(Color.RED);
                break;
            }

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

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

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

            case Color.BLACK: {
                c.setColor(Color.BLACK);
                break;
            }

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

            case Color.GRAY: {
                c.setColor(Color.GRAY);
                break;
            }

            default: {
                break;
            }
        }

        if(this.coords[1] < 4) {
            this.coords[1]++;
        }
    }
}