/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.text.format;

import java.awt.Color;
import java.awt.Font;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class AnsiSchemeTest {
    
    public AnsiSchemeTest() {
    }

    @Test
    public void testConstruct() {
        AnsiScheme scheme1 = new AnsiScheme();
        
        assertEquals(0, scheme1.colors().size());
        assertEquals(0, scheme1.fonts().size());
        
        scheme1.addColor(Color.black);
        scheme1.addColor(Color.white);
        scheme1.addColor(Color.blue);
        scheme1.addColor(Color.red);
        
        scheme1.addFont(new Font("Courier New", Font.PLAIN, 11));
        scheme1.addFont(new Font("Verdana", Font.PLAIN, 11));
        scheme1.addFont(new Font("Arial", Font.PLAIN, 11));
        
        assertEquals(4, scheme1.colors().size());
        assertEquals(3, scheme1.fonts().size());
        
        scheme1.setColor(2, Color.yellow);
        scheme1.setColor(6, Color.pink);
        scheme1.setFont(1, new Font("Tahoma", Font.PLAIN, 11));
        scheme1.setFont(5, new Font("Courier", Font.PLAIN, 11));
        
        assertEquals(5, scheme1.colors().size());
        assertEquals(4, scheme1.fonts().size());
        
        assertEquals(7, scheme1.addColor(Color.orange));
        assertEquals(6, scheme1.addFont(new Font("Courier", Font.PLAIN, 17)));
        
        try {
            scheme1.setColor(0, Color.yellow);
            fail("exception expected");
        } catch(IllegalArgumentException ex) {
            assertTrue(true);
        }
        
        Font font = scheme1.getFont(1);
        try {
            scheme1.setFont(0, font);
            fail("exception expected");
        } catch(IllegalArgumentException ex) {
            assertTrue(true);
        }
        
        AnsiScheme scheme2 = new AnsiScheme(scheme1);
        assertEquals(scheme1, scheme2);
    }
    
    @Test
    public void testGetters() {
        AnsiScheme scheme = new AnsiScheme(AnsiGenerator.SCHEME);
        assertEquals(new Color(0xFF0000), scheme.getColor(4));
        assertNull(scheme.getColor(0));
        assertNull(scheme.getColor(90));
        
        assertEquals("courier", scheme.getFont(2).getName());
        assertNull(scheme.getFont(0));
        assertNull(scheme.getFont(90));
    }
    
    @Test
    public void testCompute() {
        AnsiScheme scheme = new AnsiScheme(AnsiGenerator.SCHEME);
        AnsiAttr a1 = scheme.compute(AnsiStrings.attr("1 2 3 +b"));
        
        assertEquals(AnsiStrings.attr("1 2 3 +b"), a1);
        assertEquals(new Color(0x888888), a1.getComputedBack());
        assertEquals(new Color(0xFFFFFF), a1.getComputedFore());
        assertEquals("verdana", a1.getComputedFont().getName());
    }
    
    @Test
    public void testStringConnection() {
        AnsiScheme scheme = new AnsiScheme(AnsiGenerator.SCHEME);
        
        AnsiBuilder builder = new AnsiBuilder();
        builder.append("Hello ", AnsiStrings.attr("1 2 3"));
        builder.append("world!", AnsiStrings.attr("6 7 +b"));
        
        AnsiCharSequence text = builder.result();
        text.setScheme(scheme);
        
        assertEquals(AnsiGenerator.SCHEME, text.getScheme());
        assertSame(scheme, text.getScheme());
        
        assertEquals("verdana", text.getAttrAt(2).getComputedFont().getName());
        assertEquals(new Color(0x888888), text.getAttrAt(2).getComputedBack());
        assertEquals(new Color(0x000088), text.getAttrAt(9).getComputedFore());
        
        assertEquals("verdana", text.subSequence(4, 8).getAttrAt(1).getComputedFont().getName());
        assertEquals(new Color(0x888888), text.subSequence(4, 8).getAttrAt(1).getComputedBack());
        assertEquals(new Color(0x000088), text.subSequence(4, 8).getAttrAt(3).getComputedFore());
        
    }
    
}
