package com.elgassia.bridge.view.tui.commands.team.lobby;

import com.elgassia.bridge.Model.Card;
import com.elgassia.bridge.Model.ChooseStrategy;
import com.elgassia.bridge.Model.RandomStrategy;
import com.elgassia.bridge.adapter.LobbyAdapter;
import com.elgassia.bridge.utils.Cards;
import com.elgassia.bridge.view.tui.Command;

import java.util.ArrayList;
import java.util.List;

public class SetDeck extends Command {
    private final LobbyAdapter currentLobbyAdapter;

    public SetDeck(LobbyAdapter currentLobbyAdapter) {
        this.currentLobbyAdapter = currentLobbyAdapter;
    }

    private void usage() {
        System.out.println("usage: random | <deck>");
        System.out.println("  deck: 13*4*card");
        System.out.println("  card: <rank> <suit>");
    }

    @Override
    public void execute() {
        if (args.length == 2) {
            if ("random".equals(args[1])) {
                currentLobbyAdapter.setDeckStrategy(new RandomStrategy());
                return;
            }
        }
        System.out.println(args.length);
        System.out.println(13*4*2+1);
        if (args.length == 13*4*2 + 1) {
            Card[] cards = new Card[13*4];
            for (int i = 1; i < 13*4*2 + 1; i += 2) {
                cards[i/2] = Cards.fromString(args[i], args[i + 1]);
                if (cards[i/2] == null) {
                    System.out.println("invalid card: " + args[i] + " " + args[i+1]);
                    return;
                }
            }

            @SuppressWarnings("unchecked") List<Card>[] decks = new List[4];
            for (int i = 0; i < 13*4; ++i) {
                if (decks[i/13] == null) {
                    decks[i/13] = new ArrayList<>();
                }
                decks[i/13].add(cards[i]);
            }
            currentLobbyAdapter.setDeckStrategy(new ChooseStrategy(decks));
            return;
        }
        usage();
    }

    @Override
    public String description() {
        return "Set deck strategy";
    }
}

@@ -238,16 +238,75 @@
 dapter;%0A
+import com.elgassia.bridge.exception.BridgeLogicException;%0A
 import c
@@ -337,16 +337,16 @@
 .Cards;%0A
-
 import c
@@ -1080,87 +1080,8 @@
   %7D%0A
-        System.out.println(args.length);%0A        System.out.println(13*4*2+1);%0A
     
@@ -1757,32 +1757,54 @@
 ;%0A            %7D%0A
+            try %7B%0A    
             curr
@@ -1859,24 +1859,161 @@
 gy(decks));%0A
+            %7D catch (BridgeLogicException e) %7B%0A                System.out.println(%22Invalid deck%22);%0A                return;%0A            %7D%0A
             

print message on invalid deck strategy setting