/**
 *
 * Copyright 2017 Paul Schaub
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements;

import java.util.ArrayList;
import java.util.List;

import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
import org.jivesoftware.smackx.jingle.element.JingleContentTransportCandidate;
import org.jivesoftware.smackx.jingle.element.JingleContentTransportInfo;

/**
 * Socks5Bytestream transport element.
 */
public class JingleS5BTransport extends JingleContentTransport {
    public static final String NAMESPACE_V1 = "urn:xmpp:jingle:transports:s5b:1";
    public static final String ATTR_DSTADDR = "dstaddr";
    public static final String ATTR_MODE = "mode";
    public static final String ATTR_SID = "sid";

    private final String streamId;
    private final String dstAddr;
    private final Bytestream.Mode mode;

    protected JingleS5BTransport(List<JingleContentTransportCandidate> candidates, JingleContentTransportInfo info, String streamId, String dstAddr, Bytestream.Mode mode) {
        super(candidates, info);
        StringUtils.requireNotNullOrEmpty(streamId, "sid MUST be neither null, nor empty.");
        this.streamId = streamId;
        this.dstAddr = dstAddr;
        this.mode = mode;
    }

    public String getStreamId() {
        return streamId;
    }

    public String getDestinationAddress() {
        return dstAddr;
    }

    public Bytestream.Mode getMode() {
        return mode == null ? Bytestream.Mode.tcp : mode;
    }

    @Override
    public String getNamespace() {
        return NAMESPACE_V1;
    }

    @Override
    protected void addExtraAttributes(XmlStringBuilder xml) {
        xml.optAttribute(ATTR_DSTADDR, dstAddr);
        xml.optAttribute(ATTR_MODE, mode);
        xml.attribute(ATTR_SID, streamId);
    }

    public boolean hasCandidate(String candidateId) {
        return getCandidate(candidateId) != null;
    }

    public JingleS5BTransportCandidate getCandidate(String candidateId) {
        for (JingleContentTransportCandidate c : candidates) {
            JingleS5BTransportCandidate candidate = (JingleS5BTransportCandidate) c;
            if (candidate.getCandidateId().equals(candidateId)) {
                return candidate;
            }
        }
        return null;
    }

    public static Builder getBuilder() {
        return new Builder();
    }

    public static class Builder {
        private String streamId;
        private String dstAddr;
        private Bytestream.Mode mode;
        private ArrayList<JingleContentTransportCandidate> candidates = new ArrayList<>();
        private JingleContentTransportInfo info;

        public Builder setStreamId(String sid) {
            this.streamId = sid;
            return this;
        }

        public Builder setDestinationAddress(String dstAddr) {
            this.dstAddr = dstAddr;
            return this;
        }

        public Builder setMode(Bytestream.Mode mode) {
            this.mode = mode;
            return this;
        }

        public Builder addTransportCandidate(JingleS5BTransportCandidate candidate) {
            this.candidates.add(candidate);
            return this;
        }

        public Builder setTransportInfo(JingleContentTransportInfo info) {
            this.info = info;
            return this;
        }

        public Builder setCandidateUsed(String candidateId) {
            return setTransportInfo(JingleS5BTransportInfo.CandidateUsed(candidateId));
        }

        public Builder setCandidateActivated(String candidateId) {
            return setTransportInfo(JingleS5BTransportInfo.CandidateActivated(candidateId));
        }

        public Builder setCandidateError() {
            return setTransportInfo(JingleS5BTransportInfo.CandidateError());
        }

        public Builder setProxyError() {
            return setTransportInfo(JingleS5BTransportInfo.ProxyError());
        }

        public JingleS5BTransport build() {
            return new JingleS5BTransport(candidates, info, streamId, dstAddr, mode);
        }
    }
}

@@ -3234,16 +3234,22 @@
 private 
+final 
 ArrayLis
@@ -3841,150 +3841,786 @@
     
-this.candidates.add(candidate);%0A            return this;%0A        %7D%0A%0A        public Builder setTransportInfo(JingleContentTransportInfo info) %7B
+if (info != null) %7B%0A                throw new IllegalStateException(%22Builder has already an info set. %22 +%0A                        %22The transport can only have either an info or transport candidates, not both.%22);%0A            %7D%0A            this.candidates.add(candidate);%0A            return this;%0A        %7D%0A%0A        public Builder setTransportInfo(JingleContentTransportInfo info) %7B%0A            if (!candidates.isEmpty()) %7B%0A                throw new IllegalStateException(%22Builder has already at least one candidate set. %22 +%0A                        %22The transport can only have either an info or transport candidates, not both.%22);%0A            %7D%0A            if (this.info != null) %7B%0A                throw new IllegalStateException(%22Builder has already an info set.%22);%0A            %7D
 %0A   

Enforce jingle s5b transport invariants.