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

import java.util.WeakHashMap;

import org.jivesoftware.smack.XMPPConnection;

import org.jivesoftware.smackx.jingle.JingleSession;
import org.jivesoftware.smackx.jingle.component.JingleContentImpl;
import org.jivesoftware.smackx.jingle.component.JingleTransport;
import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
import org.jivesoftware.smackx.jingle.provider.JingleContentProviderManager;
import org.jivesoftware.smackx.jingle.transports.JingleTransportManager;
import org.jivesoftware.smackx.jingle.transports.JingleTransportSession;
import org.jivesoftware.smackx.jingle.transports.jingle_ibb.element.JingleIBBTransport;
import org.jivesoftware.smackx.jingle.transports.jingle_ibb.provider.JingleIBBTransportProvider;

/**
 * Manager for Jingle InBandBytestream transports (XEP-0261).
 *
 * @author Paul Schaub
 * @author Eng Chong Meng
 */
public final class JingleIBBTransportManager extends JingleTransportManager<JingleIBBTransport> {
    private static final WeakHashMap<XMPPConnection, JingleIBBTransportManager> INSTANCES = new WeakHashMap<>();

    private JingleIBBTransportManager(XMPPConnection connection) {
        super(connection);
        JingleContentProviderManager.addJingleContentTransportProvider(getNamespace(), new JingleIBBTransportProvider());
        JingleContentProviderManager.addJingleTransportAdapter(new JingleIBBTransportAdapter());
    }

    public static synchronized JingleIBBTransportManager getInstanceFor(XMPPConnection connection) {
        JingleIBBTransportManager manager = INSTANCES.get(connection);
        if (manager == null) {
            manager = new JingleIBBTransportManager(connection);
            INSTANCES.put(connection, manager);
        }
        return manager;
    }

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

    @Override
    public JingleTransportSession<JingleIBBTransport> transportSession(JingleSession jingleSession) {
        return new JingleIBBTransportSession(jingleSession);
    }

    @Override
    public void authenticated(XMPPConnection connection, boolean resumed) {
        // Nothing to do.
    }

    @Override
    public int getPriority() {
        return -1;
    }

    @Override
    public JingleTransport<?> createTransportForInitiator(JingleContentImpl content) {
        return new JingleIBBTransportImpl();
    }

    @Override
    public JingleTransport<?> createTransportForResponder(JingleContentImpl content, JingleContentTransport peersTransportElement) {
        JingleIBBTransportImpl other = new JingleIBBTransportAdapter().transportFromElement(peersTransportElement);
        return new JingleIBBTransportImpl((short) Math.min(other.getBlockSize(), JingleIBBTransportImpl.MAX_BLOCKSIZE), other.getStreamId());
    }
}
