package com.bert.pixels.view;

import com.bert.pixels.models.Chamber;
import com.bert.pixels.models.Color;
import com.bert.pixels.models.Pixel;

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

/**
 * Textual representation of pixels going through a chamber
 */
public class TextVisualization {

  private final int maxSize;
  private int size;

  /**
   * @param maxSize the maximum size this textual visualization can support
   */
  public TextVisualization(int maxSize) {
    this.maxSize = maxSize;
  }

  /**
   * Initialized a chamber from an input.
   *
   * @param input the input string representing the initial state of the chamber.
   * @return a fully initialized chamber.
   */
  public Chamber from(String input) {
    this.size = input.length();
    if (this.size > this.maxSize) {
      throw new IllegalArgumentException(String.format("Input exceeds max size of '%d'", this.maxSize));
    }

    List<Pixel> pixels = new ArrayList<>(this.size);
    for (int i = 0; i < input.length(); i++) {
      final char character = input.charAt(i);
      try {
        pixels.add(new Pixel(Color.valueOf(String.valueOf(character).toUpperCase()), i));
      } catch (IllegalArgumentException e) {
        if (character != '.') {
          throw new IllegalArgumentException(String.format("'%c' not supported", character));
        }
      }
    }

    return new Chamber(pixels, this.size);
  }

  /**
   * Return the textual representation of the given chamber
   * @param chamber the chamber to visualize
   * @return a textual representation of the given chamber
   */
  public String to(Chamber chamber) {
    final char[] output = String.join("", Collections.nCopies(chamber.getSize(), ".")).toCharArray();
    for (Pixel pixel : chamber.pixels()) {
      final Integer position = pixel.getPosition();
      if (output[position] != '.') {
        output[position] = 'O';
      } else {
        output[position] = pixel.getColor().toChar();
      }
    }
    return new String(output);
  }

  /**
   * @return size of the input/output handled by this instance
   */
  public int size() {
    return this.size;
  }
}

@@ -166,16 +166,41 @@
 ayList;%0A
+import java.util.Arrays;%0A
 import j
@@ -243,16 +243,110 @@
 il.List;
+%0Aimport java.util.Map;%0Aimport java.util.function.Function;%0Aimport java.util.stream.Collectors;
 %0A%0A/**%0A *
@@ -490,16 +490,98 @@
 t size;%0A
+  private static final Map%3CCharacter, Color%3E TEXT_TO_COLOR = setupTextToColor();%0A%0A
 %0A  /**%0A 
@@ -1293,81 +1293,49 @@
     
-try %7B%0A        pixels.add(new Pixel(Color.valueOf(String.valueOf(c
+Color color = TEXT_TO_COLOR.get(C
 haracter
 ).to
@@ -1330,17 +1330,16 @@
 haracter
-)
 .toUpper
@@ -1347,74 +1347,48 @@
 ase(
-)), i));%0A      %7D catch (IllegalArgumentException e) %7B%0A 
+character));%0A
       
- 
 if (
+color == null && 
 char
@@ -1399,26 +1399,24 @@
 r != '.') %7B%0A
-  
         thro
@@ -1497,27 +1497,92 @@
 er));%0A      
-  %7D
+%7D else if (color != null) %7B%0A        pixels.add(new Pixel(color, i));
 %0A      %7D%0A   
@@ -2354,11 +2354,255 @@
 ize;%0A  %7D
+%0A%0A  private static Map%3CCharacter, Color%3E setupTextToColor() %7B%0A    return Collections.unmodifiableMap(Arrays.stream(Color.values())%0A                                             .collect(Collectors.toMap(Color::toChar, Function.identity())));%0A  %7D
 %0A%7D%0A

Rework visualization to not have to rely on Color being one lettered