package org.teleal.cling.support.messagebox.model;

import org.teleal.cling.support.messagebox.parser.MessageDOM;
import org.teleal.cling.support.messagebox.parser.MessageDOMParser;
import org.teleal.cling.support.messagebox.parser.MessageElement;
import org.teleal.common.xml.ParserException;

/**
 * https://sourceforge.net/apps/mediawiki/samygo/index.php?title=MessageBoxService_request_format
 * 
 * @author Christian Bauer
 */
public abstract class Message implements ElementAppender {

    public enum Category {
        SMS("SMS"),
        INCOMING_CALL("Incoming Call"),
        SCHEDULE_REMINDER("Schedule Reminder");

        public String text;

        Category(String text) {
            this.text = text;
        }
    }

    public enum DisplayType {

        MINIMUM("Minimum"),
        MAXIMUM("Maximum");

        public String text;

        DisplayType(String text) {
            this.text = text;
        }
    }

    private final Category category;
    private DisplayType displayType;

    public Message(Category category, DisplayType displayType) {
        this.category = category;
        this.displayType = displayType;
    }

    public Category getCategory() {
        return category;
    }

    public DisplayType getDisplayType() {
        return displayType;
    }

    @Override
    public String toString() {
        try {
            MessageDOMParser mp = new MessageDOMParser();
            MessageDOM dom = mp.createDocument();

            MessageElement root = dom.createRoot(mp.createXPath(), "Message");
            root.createChild("Category").setContent(getCategory().text);
            root.createChild("DisplayType").setContent(getDisplayType().text);
            appendMessageElements(root);

            String s = mp.print(dom, 0, false);

            // Cut the root element, what we send to the TV is not really XML, just
            // random element soup which I'm sure the Samsung guys think is XML...
            return s.replaceAll("<Message xmlns=\"urn:samsung-com:messagebox-1-0\">", "")
                    .replaceAll("</Message>", "");

            
        } catch (ParserException ex) {
            throw new RuntimeException(ex);
        }
    }
}
