/**
 * This code was generated by
 * \ / _    _  _|   _  _
 *  | (_)\/(_)(_|\/| |(/_  v1.0.0
 *       /       /
 */

package com.twilio.twiml.voice;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.twilio.converter.Promoter;
import com.twilio.twiml.TwiML;
import com.twilio.twiml.TwiMLException;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * TwiML wrapper for {@code <Play>}
 */
@JsonDeserialize(builder = Play.Builder.class)
public class Play extends TwiML {
    private final Integer loop;
    private final String digits;
    private final URI url;

    /**
     * For XML Serialization/Deserialization
     */
    private Play() {
        this(new Builder());
    }

    /**
     * Create a new {@code <Play>} element
     */
    private Play(Builder b) {
        super("Play", b);
        this.loop = b.loop;
        this.digits = b.digits;
        this.url = b.url;
    }

    /**
     * The body of the TwiML element
     *
     * @return Element body as a string if present else null
     */
    protected String getElementBody() {
        return this.getUrl() == null ? null : this.getUrl().toString();
    }

    /**
     * Attributes to set on the generated XML element
     *
     * @return A Map of attribute keys to values
     */
    protected Map<String, String> getElementAttributes() {
        // Preserve order of attributes
        Map<String, String> attrs = new HashMap<>();

        if (this.getLoop() != null) {
            attrs.put("loop", this.getLoop().toString());
        }
        if (this.getDigits() != null) {
            attrs.put("digits", this.getDigits());
        }

        return attrs;
    }

    /**
     * Times to loop media
     *
     * @return Times to loop media
     */
    public Integer getLoop() {
        return loop;
    }

    /**
     * Play DTMF tones for digits
     *
     * @return Play DTMF tones for digits
     */
    public String getDigits() {
        return digits;
    }

    /**
     * Media URL
     *
     * @return Media URL
     */
    public URI getUrl() {
        return url;
    }

    /**
     * Create a new {@code <Play>} element
     */
    public static class Builder extends TwiML.Builder<Builder> {
        /**
         * Create and return a {@code <Play.Builder>} from an XML string
         */
        public static Builder fromXml(final String xml) throws TwiMLException {
            try {
                return OBJECT_MAPPER.readValue(xml, Builder.class);
            } catch (final JsonProcessingException jpe) {
                throw new TwiMLException(
                    "Failed to deserialize a Play.Builder from the provided XML string: " + jpe.getMessage());
            } catch (final Exception e) {
                throw new TwiMLException("Unhandled exception: " + e.getMessage());
            }
        }

        private Integer loop;
        private String digits;
        private URI url;

        /**
         * Create a {@code <Play>} with url
         */
        public Builder(URI url) {
            this.url = url;
        }

        /**
         * Create a {@code <Play>} with url
         */
        public Builder(String url) {
            this.url = Promoter.uriFromString(url);
        }

        /**
         * Create a {@code <Play>} with child elements
         */
        public Builder() {
        }

        /**
         * Times to loop media
         */
        @JacksonXmlProperty(isAttribute = true, localName = "loop")
        public Builder loop(Integer loop) {
            this.loop = loop;
            return this;
        }

        /**
         * Play DTMF tones for digits
         */
        @JacksonXmlProperty(isAttribute = true, localName = "digits")
        public Builder digits(String digits) {
            this.digits = digits;
            return this;
        }

        /**
         * Media URL
         */
        @JacksonXmlProperty(isAttribute = true, localName = "url")
        public Builder url(URI url) {
            this.url = url;
            return this;
        }

        /**
         * Media URL
         */
        public Builder url(String url) {
            this.url = Promoter.uriFromString(url);
            return this;
        }

        /**
         * Create and return resulting {@code <Play>} element
         */
        public Play build() {
            return new Play(this);
        }
    }
}