package terminal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static terminal.SelectGraphicRendition.GREEN;
import static terminal.SelectGraphicRendition.PURPLE;
import static terminal.SelectGraphicRendition.RED;
import static terminal.SelectGraphicRendition.RESET;
import static terminal.SelectGraphicRendition.YELLOW;

import java.util.HashSet;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;

import com.googlecode.lanterna.TextColor;
import com.googlecode.lanterna.terminal.virtual.DefaultVirtualTerminal;
import com.googlecode.lanterna.terminal.virtual.VirtualTerminal;

import terminal.ControlSequence.NullControlSequence;

public class ControlSequenceTest {

    private Set<String> indicatorSet;

    private VirtualTerminal createTerminalWithIndicators() {
        return new DefaultVirtualTerminal() {

            @Override
            public void resetColorAndSGR() {
                indicatorSet.add("RESET");
            }

            @Override
            public void setForegroundColor(TextColor color) {
                indicatorSet.add(color.toString());
            }
        };
    }

    @Before
    public void setUp() {
        indicatorSet = new HashSet<>();
    }

    @Test
    public void nullControlSequenceDoesNothing() {
        ControlSequence nullControlSequence = new NullControlSequence();
        VirtualTerminal terminal = createTerminalWithIndicators();
        nullControlSequence.applyTo(terminal);
        assertTrue(indicatorSet.isEmpty());
    }

    @Test
    public void controlSequenceUpdatesTerminal_SGR_Reset() {
        VirtualTerminal terminal = createTerminalWithIndicators();
        RESET.applyTo(terminal);
        assertTrue(indicatorSet.contains("RESET"));
    }

    @Test
    public void controlSequenceUpdatesTerminal_SGR_Red() {
        VirtualTerminal terminal = createTerminalWithIndicators();
        RED.applyTo(terminal);
        assertTrue(indicatorSet.contains("RED"));
    }

    @Test
    public void controlSequenceUpdatesTerminal_SGR_Green() {
        VirtualTerminal terminal = createTerminalWithIndicators();
        GREEN.applyTo(terminal);
        assertTrue(indicatorSet.contains("GREEN"));
    }

    @Test
    public void controlSequenceUpdatesTerminal_SGR_Yellow() {
        VirtualTerminal terminal = createTerminalWithIndicators();
        YELLOW.applyTo(terminal);
        assertTrue(indicatorSet.contains("YELLOW"));
    }

    @Test
    public void controlSequenceUpdatesTerminal_SGR_Purple() {
        VirtualTerminal terminal = createTerminalWithIndicators();
        PURPLE.applyTo(terminal);
        assertTrue(indicatorSet.contains("MAGENTA"));
    }

    @Test
    public void nullControlSequenceHasCorrectCode() {
        ControlSequence nullControlSequence = new NullControlSequence();
        assertEquals("", nullControlSequence.getCode());
    }

    @Test
    public void SelectGraphicRenditionEnumCoverage() {
        for (SelectGraphicRendition sgr : SelectGraphicRendition.values())
            SelectGraphicRendition.valueOf(sgr.toString());
    }

}
