package com.africastalking;


import java.net.URL;

public class VoiceMessage {


    private String xml;

    private VoiceMessage(String xml) {
        this.xml = xml;
    }

    @Override
    public String toString() {
        return this.xml;
    }

    public static class Builder{

        private StringBuilder xml;

        public Builder() {
            xml = new StringBuilder();
            xml.append("<Response>");
        }

        public VoiceMessage build() {
            xml.append("</Response>");
            return new VoiceMessage(xml.toString());
        }

        public Builder say(String text, boolean playBeep, String voice) {
            xml.append("<Say playBeep=\""+ String.valueOf(playBeep) +"\" voice=\""+voice+"\"></Say>");
            return this;
        }

        public Builder say(String text) {
            return say(text, false, "woman");
        }

        public Builder play(URL url) {
            xml.append("<Play url=\""+ url.toString() +"\" />");
            return this;
        }

        public Builder getDigits(String text, URL url, int numDigits, long timeout, String finishOnKey, URL callbackUrl) {

            String str = "<GetDigits timeout=\"" + timeout + "\" finishOnKey=\""+finishOnKey+"\" " +
                    "numDigits=\"" + numDigits + "\" callbackUrl=\""+ callbackUrl +"\">";
            if (url == null) {
                str += "<Say>" + text + "</Say>";
            } else {
                str += "<Play url=\""+ url.toString() +"\" />";
            }

            str += "</GetDigits>";
            xml.append(str);

            return this;
        }

        public Builder getDigits(String text) {
            return getDigits(text, null, -1, -1, null, null);
        }

        public Builder getDigits(URL url) {
            return getDigits(null, url, -1, -1, null, null);
        }

        public Builder dial(String phoneNumbers, String ringbackTone, boolean record, boolean sequential, String callerId, long maxDuration) {
            xml.append("<Dial phoneNumbers=\"" + phoneNumbers + "\" ringbackTone=\"" + ringbackTone + "\" " +
                    "record=\""+String.valueOf(record)+"\" sequential=\"" + String.valueOf(sequential) + "\" " +
                    "callerId=\"" + callerId + "\" maxDuration=\"" + maxDuration + "\"/>");
            return this;
        }

        public Builder dial(String phoneNumbers) {
            return dial(phoneNumbers, null, false, false, null, -1 /* FIXME: What?? */);
        }

        public Builder enqueue(URL holdMusic, String name) {
            xml.append("<Dequeue holdMusic=\"" + holdMusic.toString() +"\" name=\"" + name + "\" />");
            return this;
        }

        public Builder enqueue(URL holdMusic) {
            return enqueue(holdMusic, null);
        }

        public Builder dequeue(String number, String name) {
            xml.append("<Dequeue phoneNumber=\""+number+"\" name=\"" + name + "\" />");
            return this;
        }

        public Builder dequeue(String number) {
            return dequeue(number, null);
        }

        public Builder conference() {
            xml.append("<Conference />");
            return this;
        }

        public Builder redirect(URL url) {
            xml.append("<Redirect>" + url.toString() + "</Redirect>");
            return this;
        }

        public Builder reject() {
            xml.append("<Reject />");
            return this;
        }

    }
}

@@ -3464,20 +3464,657 @@
 %0A        %7D%0A%0A
+        public Builder record(String text, URL url, int maxLength, long timeout, String finishOnKey, boolean trimSilence, boolean playBeep, URL callbackUrl) %7B%0A            xml.append(%22%3CRecord finishOnKey=%5C%22%22+finishOnKey+%22%5C%22 maxLength=%5C%22%22+ maxLength +%22%5C%22 timeout=%5C%22%22+timeout+%22%5C%22 trimSilence=%5C%22%22+ String.valueOf(trimSilence) + %22%5C%22%22 +%0A            %22playBeep=%5C%22%22 + String.valueOf(playBeep) + %22%5C%22 callbackUrl=%5C%22%22+ callbackUrl +%22%5C%22%3E%22);%0A            this.say(text);%0A            xml.append(%22%3C/Record%3E%22);%0A            return this;%0A        %7D%0A%0A        public Builder record() %7B%0A            xml.append(%22%3CRecord /%3E%22);%0A            return this;%0A        %7D%0A%0A
     %7D%0A%7D%0A

Add record to voice xml builder