package it.playfellas.superapp.logic.master.game23;

import android.bluetooth.BluetoothDevice;

import it.playfellas.superapp.InternalConfig;
import it.playfellas.superapp.events.EventFactory;
import it.playfellas.superapp.events.game.StartGameEvent;
import it.playfellas.superapp.logic.Config3;
import it.playfellas.superapp.logic.RandomUtils;
import it.playfellas.superapp.logic.db.TileSelector;
import it.playfellas.superapp.logic.db.query.BinaryOperator;
import it.playfellas.superapp.logic.db.query.Color;
import it.playfellas.superapp.logic.db.query.Conjunction;
import it.playfellas.superapp.logic.db.query.Shape;
import it.playfellas.superapp.logic.db.query.Type;
import it.playfellas.superapp.logic.tiles.Tile;
import it.playfellas.superapp.logic.tiles.TileColor;
import it.playfellas.superapp.logic.tiles.TileShape;
import it.playfellas.superapp.logic.tiles.TileType;
import it.playfellas.superapp.network.TenBus;

/**
 * Created by affo on 02/09/15.
 */
public class Master3Controller extends Master23Controller {
    private Config3 conf;
    private TileSelector ts;

    public Master3Controller(TileSelector ts, Config3 conf) {
        super(conf);
        this.conf = conf;
        this.ts = ts;
    }

    @Override
    protected Tile[] newBaseTiles() {
        final int size = InternalConfig.NO_FIXED_TILES;
        TileColor[] colors = RandomUtils.choice(TileColor.values(), size).toArray(new TileColor[size]);
        TileShape[] shapes = RandomUtils.choice(TileShape.values(), size).toArray(new TileShape[size]);
        Tile[] tiles = new Tile[InternalConfig.NO_FIXED_TILES];

        for (int i = 0; i < size; i++) {
            tiles[i] = ts.random(1, new Conjunction(
                    new Shape(BinaryOperator.EQUALS, shapes[i]),
                    new Color(BinaryOperator.EQUALS, colors[i]),
                    new Type(BinaryOperator.EQUALS, TileType.ABSTRACT)
            )).get(0);
        }

        return tiles;
    }

    @Override
    protected void onBeginStage() {
        super.onBeginStage();
        BluetoothDevice firstPlayer = nextPlayer();
        TenBus.get().post(EventFactory.yourTurn(firstPlayer));
    }

    @Override
    protected void onEndStage() {
        // does nothing
    }

    @Override
    protected void onAnswer(boolean rw) {
        if (rw) {
            incrementScore();
        }
        TenBus.get().post(EventFactory.yourTurn(nextPlayer()));
    }

    @Override
    protected StartGameEvent getNewGameEvent() {
        return EventFactory.startGame3(conf);
    }
}

@@ -89,16 +89,61 @@
 evice;%0A%0A
+import org.apache.commons.lang3.ArrayUtils;%0A%0A
 import i
@@ -1377,66 +1377,438 @@
     
-TileColor%5B%5D colors = RandomUtils.choice(TileColor.values()
+// remove NONE...%0A        TileColor%5B%5D colors = TileColor.values();%0A        TileShape%5B%5D shapes = TileShape.values();%0A        int noneIndex;%0A%0A        noneIndex = ArrayUtils.indexOf(colors, TileColor.NONE);%0A        colors = ArrayUtils.remove(colors, noneIndex);%0A        %0A        noneIndex = ArrayUtils.indexOf(shapes, TileShape.NONE);%0A        shapes = ArrayUtils.remove(shapes, noneIndex);%0A%0A        colors = RandomUtils.choice(colors
 , si
@@ -1844,36 +1844,24 @@
 e%5D);%0A       
- TileShape%5B%5D
  shapes = Ra
@@ -1877,34 +1877,22 @@
 .choice(
-TileShape.values()
+shapes
 , size).

remove none before choosing base colors/shapes