/*
 * Copyright (C) 2010 Teleal GmbH, Switzerland
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.teleal.cling.support.connectionmanager.protocolinfo;

import org.teleal.common.util.MimeType;

/**
 *
 */
public class ProtocolInfo {

    public static final String WILDCARD = "*";

    protected Protocol protocol = Protocol.ALL;
    protected String network = WILDCARD;
    protected String contentFormat = WILDCARD;
    protected String additionalInfo = WILDCARD;

    public ProtocolInfo(String contentFormat) {
        this.protocol = Protocol.HTTP_GET;
        this.contentFormat = contentFormat;
    }

    public ProtocolInfo(MimeType contentFormatMimeType) {
        this.protocol = Protocol.HTTP_GET;
        this.contentFormat = contentFormatMimeType.toString();
    }

    public ProtocolInfo(Protocol protocol, String network, String contentFormat, String additionalInfo) {
        this.protocol = protocol;
        this.network = network;
        this.contentFormat = contentFormat;
        this.additionalInfo = additionalInfo;
    }

    public Protocol getProtocol() {
        return protocol;
    }

    public String getNetwork() {
        return network;
    }

    public String getContentFormat() {
        return contentFormat;
    }

    public MimeType getContentFormatMimeType() throws IllegalArgumentException {
        return MimeType.valueOf(contentFormat);
    }

    public String getAdditionalInfo() {
        return additionalInfo;
    }

    public static ProtocolInfo fromString(String s) throws IllegalArgumentException {
        if (s == null) throw new IllegalArgumentException("Can't parse ProtocolInfo string: " + s);
        s = s.trim();
        String[] split = s.split(":");
        return new ProtocolInfo(
                Protocol.getByProtocolString(split[0]),
                split[1],
                split[2],
                split[3]
        );
    }

    @Override
    public String toString() {
        return protocol.getProtocolString() + ":" +
                network + ":" +
                contentFormat + ":" +
                additionalInfo;

    }
}
