package y;

import java.awt.*;
import java.io.*;
import java.util.List;

/**
 * @author Rajiv Shivane
 * @since Dec 27, 2007
 */
public class Main
{
    private static final boolean PRINT_HTML_FORMAT = true;

    public static void main(String[] args)
            throws Exception
    {
        process(new File("C:\\Program Files\\Yahoo!\\Messenger\\Profiles\\doofy\\Archive\\Messages\\"));
    }


    private static void process(File file) throws IOException
    {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File child : files) {
                process(child);
            }
        } else {
            processFile(file);
        }

    }

    private static void processFile(File file)
            throws IOException
    {
        System.err.println("Processing File: " + file.getAbsolutePath());
        String buddy = file.getParentFile().getName();
        String myId = file.getParentFile().getParentFile().getParentFile().getParentFile().getName();
        InputStream is = new BufferedInputStream(new FileInputStream(file));
        Parser parser = new Parser(buddy, myId, is);
        try {
            while (true) {
                Parser.Record record = parser.readRecord();
                printRecord(record, PRINT_HTML_FORMAT);
            }
        } catch (EOFException e) {
        }
    }

    private static void printRecord(Parser.Record record, boolean htmlFormat)
    {
        List<Parser.Token> tokens = record.getTokens();
        if (tokens.size() == 0) {
            if (htmlFormat) {
                System.out.print("<span style=\"color:gray;\">");
            }
            System.out.print("Opened new window on " + record.getDate());
            if (htmlFormat) {
                System.out.print("</span><br>");
            }
            System.out.println();
            return;
        }
        if (record.isFromBuddy()) {
            System.out.print(record.getAuthor() + " ");
        } else {
            System.out.print("You ");
        }
        System.out.print("(" + record.getDate() + "): ");
        CurrentStyle currentStyle = new CurrentStyle();
        for (Parser.Token token : tokens) {
            if (token.getType() == Parser.Token.TokenTypes.TEXT) {
                String mesg = (String) token.getValue();
                String style = null;
                if (htmlFormat) {
                    style = currentStyle.getHTMLStyle();
                    if (style != null) {
                        System.out.print("<span style=\"" + style + "\">");
                    }
//                    mesg = mesg.replace("&", "&amp;");
//                    mesg = mesg.replace(">", "&gt;");
//                    mesg = mesg.replace("<", "&lt;");
                    mesg = mesg.replace("\r", "<br>");
                }
                System.out.print(mesg);
                if (style != null) {
                    System.out.print("</span>");
                }
            } else {
                if (htmlFormat) {
                    currentStyle.setToken(token);
                }
            }
        }
        if (htmlFormat) {
            System.out.print("<br>");
        }
        System.out.println("");
    }


    private static class CurrentStyle
    {
        private Color color = null;
        private int fontFormat = 0;

        private static final int BOLD_MASK = 1;
        private static final int ITALIC_MASK = 2;
        private static final int UNDERLINE_MASK = 4;

        public void setToken(Parser.Token token)
        {
            Object value = token.getValue();
            switch (token.getType()) {
                case CUSTOM_COLOR:
                    if (value.equals(Color.black)) {
                        color = null;
                    } else {
                        color = (Color) value;
                    }
                    break;
                case STANDARD_COLOR:
                    if (value == Parser.StandardColors.BLACK) {
                        color = null;
                    } else {
                        color = ((Parser.StandardColors) value).getColor();
                    }
                    break;
                case FONT:
                    Parser.Token.FontFormats format = (Parser.Token.FontFormats) value;
                    switch (format) {
                        case BOLD:
                            fontFormat &= BOLD_MASK;
                            break;
                        case ITALIC:
                            fontFormat &= ITALIC_MASK;
                            break;
                        case UNDERLINE:
                            fontFormat &= UNDERLINE_MASK;
                            break;
                        case UNDO_BOLD:
                            fontFormat &= (~BOLD_MASK);
                            break;
                        case UNDO_ITALIC:
                            fontFormat &= (~UNDERLINE_MASK);
                            break;
                        case UNDO_UNDERLINE:
                            fontFormat &= (~UNDERLINE_MASK);
                            break;
                    }
                    break;
                case BEGIN_LINK:
                    System.out.print("<a>");
                    //TODO add href
                    break;
                case END_LINK:
                    System.out.print("</a>");
                    break;
                case UNKNOWN:
                    System.out.print("<<0x"+Integer.toHexString((Integer)value)+">>");
                    break;
                default:
                    throw new IllegalStateException();
            }
        }

        public String getHTMLStyle()
        {
            StringBuffer style = new StringBuffer();
            if (color != null) {
                style.append("color:#").append(toHTML(color)).append(';');
            }
            if (fontFormat != 0) {
                if ((fontFormat & BOLD_MASK) != 0) {
                    style.append("font-weight: bold;");
                }
                if ((fontFormat & ITALIC_MASK) != 0) {
                    style.append("font-style: italic;");
                }
                if ((fontFormat & UNDERLINE_MASK) != 0) {
                    style.append("text-decoration: underline;");
                }
            }

            return style.length() == 0 ? null : style.toString();
        }

        private String toHTML(Color color)
        {
            StringBuffer buff = new StringBuffer();
            int rgb = (color.getRed() << 16) | (color.getGreen() << 8) | color.getBlue();
            buff.append(Integer.toHexString(rgb).toUpperCase());
            while (buff.length() != 6) {
                buff.insert(0, '0');
            }
            return buff.toString();
        }
    }

}
