package net.cobem.grizzly.habbohotel.guild;

import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.communication.EventResponse;
import net.cobem.grizzly.communication.HeaderLibrary;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GuildPieceHandler {
    private List<GuildPiece> Bases = new ArrayList<>();
    private List<GuildPiece> Symbols = new ArrayList<>();

    private List<GuildPiece> First_Color = new ArrayList<>();
    private List<GuildPiece> Second_Color = new ArrayList<>();
    private List<GuildPiece> Third_Color = new ArrayList<>();

    public GuildPieceHandler() {
        ResultSet Pieces = Grizzly.getStorage().query("SELECT * FROM server_guild_pieces").getTable();

        try {
            while (Pieces.next()) {
                String Type = Pieces.getString("type");

                switch (Type) {
                    case "base":
                        Bases.add(new GuildPiece(Pieces.getInt("id"), Pieces.getString("data_piece_one"), Pieces.getString("data_piece_two")));
                        break;

                    case "symbol":
                        Symbols.add(new GuildPiece(Pieces.getInt("id"), Pieces.getString("data_piece_one"), Pieces.getString("data_piece_two")));
                        break;

                    case "first_color":
                        First_Color.add(new GuildPiece(Pieces.getInt("id"), Pieces.getString("data_piece_one"), Pieces.getString("data_piece_two")));
                        break;

                    case "second_color":
                        Second_Color.add(new GuildPiece(Pieces.getInt("id"), Pieces.getString("data_piece_one"), Pieces.getString("data_piece_two")));
                        break;

                    case "third_color":
                        Third_Color.add(new GuildPiece(Pieces.getInt("id"), Pieces.getString("data_piece_one"), Pieces.getString("data_piece_two")));
                        break;
                }
            }
        } catch (SQLException e) {
        }
    }

    public EventResponse Serialize() {
        EventResponse Message = new EventResponse();

        Message.Initialize(HeaderLibrary.ShowGuildPiecesEvent);

        Message.addInt(Bases.size());

        for (GuildPiece Piece : Bases) {
            Message.addInt(Piece.ID);
            Message.addString(Piece.FirstPiece);
            Message.addString(Piece.SecondPiece);
        }

        Message.addInt(Symbols.size());

        for (GuildPiece Piece : Symbols) {
            Message.addInt(Piece.ID);
            Message.addString(Piece.FirstPiece);
            Message.addString(Piece.SecondPiece);
        }

        Message.addInt(First_Color.size());

        for (GuildPiece Piece : First_Color) {
            Message.addInt(Piece.ID);
            Message.addString(Piece.FirstPiece);
        }

        Message.addInt(Second_Color.size());

        for (GuildPiece Piece : Second_Color) {
            Message.addInt(Piece.ID);
            Message.addString(Piece.FirstPiece);
        }

        Message.addInt(Third_Color.size());

        for (GuildPiece Piece : Third_Color) {
            Message.addInt(Piece.ID);
            Message.addString(Piece.FirstPiece);
        }
        return Message;
    }

    public List<GuildPiece> getBases() {
        return Bases;
    }

    public List<GuildPiece> getSymbols() {
        return Symbols;
    }

    public List<GuildPiece> getFirstColors() {
        return First_Color;
    }

    public List<GuildPiece> getSecondColors() {
        return Second_Color;
    }

    public List<GuildPiece> getThirdColors() {
        return Third_Color;
    }
}
