package com.wdkl.ncs.janus.rtc.observer;

import android.util.Log;


import com.wdkl.ncs.janus.rtc.Peer;

import org.webrtc.SdpObserver;
import org.webrtc.SessionDescription;

public class AnswerSdpObserver implements SdpObserver {
    private final static String TAG = AnswerSdpObserver.class.getSimpleName();

    Peer peer;
    CreateAnswerCallback callback;

    public AnswerSdpObserver(Peer peer, CreateAnswerCallback callback){
        this.peer = peer;
        this.callback = callback;
    }

    @Override
    public void onCreateSuccess(SessionDescription sdp) {
        Log.d(TAG, "createOffer onCreateSuccess " + sdp.toString());
        peer.getPeerConnection().setLocalDescription(new SdpObserver() {
            @Override
            public void onCreateSuccess(SessionDescription sdp) {
                Log.d(TAG, "createAnswer setLocalDescription onCreateSuccess");
            }

            @Override
            public void onSetSuccess() {
                Log.d(TAG, "createAnswer setLocalDescription onSetSuccess");
                // send answer sdp
                if (callback != null) {
                    callback.onSetAnswerSuccess(sdp);
                }
            }

            @Override
            public void onCreateFailure(String error) {
                Log.d(TAG, "createAnswer setLocalDescription onCreateFailure " + error);
                if (callback != null) {
                    callback.onSetAnswerFailed(error);
                }
            }

            @Override
            public void onSetFailure(String error) {
                Log.d(TAG, "createAnswer setLocalDescription onSetFailure " + error);
                if (callback != null) {
                    callback.onSetAnswerFailed(error);
                }
            }
        }, sdp);
    }

    @Override
    public void onSetSuccess() {
        Log.d(TAG, "createAnswer onSetSuccess");
    }

    @Override
    public void onCreateFailure(String s) {
        Log.d(TAG, "createAnswer onCreateFailure " + s);
    }

    @Override
    public void onSetFailure(String s) {
        Log.d(TAG, "createAnswer onSetFailure " + s);
    }
}
