/*
 * 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;

import org.teleal.cling.binding.annotations.UpnpAction;
import org.teleal.cling.binding.annotations.UpnpInputArgument;
import org.teleal.cling.binding.annotations.UpnpOutputArgument;
import org.teleal.cling.binding.annotations.UpnpService;
import org.teleal.cling.binding.annotations.UpnpServiceId;
import org.teleal.cling.binding.annotations.UpnpServiceType;
import org.teleal.cling.binding.annotations.UpnpStateVariable;
import org.teleal.cling.binding.annotations.UpnpStateVariables;
import org.teleal.cling.model.action.ActionException;

/**
 *
 */
@UpnpService(
        serviceId = @UpnpServiceId("ConnectionManager"),
        serviceType = @UpnpServiceType(value = "ConnectionManager", version = 1)
)
@UpnpStateVariables({
        @UpnpStateVariable(name = "SourceProtocolInfo", datatype = "string", sendEvents = false),
        @UpnpStateVariable(name = "SinkProtocolInfo", datatype = "string", sendEvents = false)
})
public abstract class AbstractSingleStreamConnectionManagerService {

    @UpnpStateVariable
    private String currentConnectionIDs = "0"; // We support only one connection with the '0' ID

    @UpnpStateVariable(name = "A_ARG_TYPE_RcsID", sendEvents = false)
    private int rcsID = 0; // TODO: Rendering control ID is '0'?

    @UpnpStateVariable(name = "A_ARG_TYPE_AVTransportID", sendEvents = false)
    private int avTransportID = 0; // TODO: AVTransport ID is '0'?

    @UpnpStateVariable(name = "A_ARG_TYPE_ProtocolInfo", sendEvents = false)
    private String protocolInfo = ""; // Info for current connection is "unknown"

    @UpnpStateVariable(name = "A_ARG_TYPE_ConnectionManager", sendEvents = false)
    private String peerConnectionManager = "";

    @UpnpStateVariable(name = "A_ARG_TYPE_ConnectionID", sendEvents = false)
    private int peerConnectionID = -1;

    @UpnpStateVariable(name = "A_ARG_TYPE_Direction", sendEvents = false)
    private Direction direction = Direction.Input;

    @UpnpStateVariable(name = "A_ARG_TYPE_ConnectionStatus", sendEvents = false)
    private ConnectionStatus connectionStatus = ConnectionStatus.OK;

    public int getRcsID() {
        return rcsID;
    }

    public int getAvTransportID() {
        return avTransportID;
    }

    public String getProtocolInfo() {
        return protocolInfo;
    }

    public String getPeerConnectionManager() {
        return peerConnectionManager;
    }

    public int getPeerConnectionID() {
        return peerConnectionID;
    }

    public Direction getDirection() {
        return direction;
    }

    public ConnectionStatus getConnectionStatus() {
        return connectionStatus;
    }

    @UpnpAction(
            name = "GetProtocolInfo",
            out = {
                    @UpnpOutputArgument(name = "Source", stateVariable = "SourceProtocolInfo", getterName = "getSourceProtocolInfo"),
                    @UpnpOutputArgument(name = "Sink", stateVariable = "SinkProtocolInfo", getterName = "getSinkProtocolInfo")
            }
    )
    public void getProtocolInfoAction() throws ActionException {

    }

    @UpnpAction
    public String getCurrentConnectionIDs() throws ActionException {
        return currentConnectionIDs;
    }

    @UpnpAction(
            out = {
                    @UpnpOutputArgument(name = "RcsID", getterName = "getRcsID"),
                    @UpnpOutputArgument(name = "AVTransportID", getterName = "getAvTransportID"),
                    @UpnpOutputArgument(name = "ProtocolInfo", getterName = "getProtocolInfo"),
                    @UpnpOutputArgument(name = "PeerConnectionManager", stateVariable = "A_ARG_TYPE_ConnectionManager", getterName = "getPeerConnectionManager"),
                    @UpnpOutputArgument(name = "PeerConnectionID", stateVariable = "A_ARG_TYPE_ConnectionID", getterName = "getPeerConnectionID"),
                    @UpnpOutputArgument(name = "Direction", getterName = "getDirection"),
                    @UpnpOutputArgument(name = "Status", stateVariable = "A_ARG_TYPE_ConnectionStatus", getterName = "getConnectionStatus")
            }
    )
    public void getCurrentConnectionInfo(@UpnpInputArgument(name = "ConnectionID") int connectionId) throws ActionException {
        if (connectionId != 0) {
            throw new ConnectionManagerException(
                    ConnectionManagerErrorCode.INVALID_CONNECTION_REFERENCE,
                    "This service only supports one connection with ID '0'"
            );
        }
    }

    public abstract String getSourceProtocolInfo();

    public abstract String getSinkProtocolInfo();

}
