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

import android.util.Log;

import org.webrtc.DataChannel;
import org.webrtc.IceCandidate;
import org.webrtc.MediaStream;
import org.webrtc.PeerConnection;
import org.webrtc.RtpReceiver;

import java.util.concurrent.ScheduledExecutorService;

public class CustomPCObserver implements PeerConnection.Observer {
    private final static String TAG = CustomPCObserver.class.getSimpleName();

    private ScheduledExecutorService executor;
    private CreatePeerConnectionCallback callback;

    public CustomPCObserver(ScheduledExecutorService executor, CreatePeerConnectionCallback callback) {
        this.executor = executor;
        this.callback = callback;
    }
    @Override
    public void onIceCandidate(final IceCandidate candidate) {
        Log.d(TAG,"onIceCandidate");
        executor.execute(new Runnable() {
            @Override
            public void run() {
                if (callback != null) {
                    callback.onIceCandidate(candidate);
                }
            }
        });
    }

    @Override
    public void onIceCandidatesRemoved(final IceCandidate[] candidates) {
        Log.d(TAG, "onIceCandidatesRemoved");
        executor.execute(new Runnable() {
            @Override
            public void run() {
                if (callback != null) {
                    callback.onIceCandidatesRemoved(candidates);
                }
            }
        });
    }

    @Override
    public void onSignalingChange(PeerConnection.SignalingState newState) {
        Log.d(TAG, "SignalingState: " + newState);
    }

    @Override
    public void onIceConnectionChange(final PeerConnection.IceConnectionState newState) {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "IceConnectionState: " + newState);
                if (newState == PeerConnection.IceConnectionState.CONNECTED) {
                    callback.onIceConnected();
                } else if (newState == PeerConnection.IceConnectionState.DISCONNECTED) {
                    callback.onIceDisconnected();
                } else if (newState == PeerConnection.IceConnectionState.FAILED) {
                    callback.onIceConnectFail();
                }
            }
        });
    }

    @Override
    public void onIceGatheringChange(PeerConnection.IceGatheringState newState) {
        Log.d(TAG, "onIceGatheringChange " + newState);
        if (newState == PeerConnection.IceGatheringState.COMPLETE) {
            if (callback != null) {
                callback.onIceGatheringComplete();
            }
        }
    }

    @Override
    public void onIceConnectionReceivingChange(boolean receiving) {
        Log.d(TAG, "onIceConnectionReceivingChange changed to " + receiving);
    }

    @Override
    public void onAddStream(final MediaStream stream) {
        Log.d(TAG, "onAddStream");
        executor.execute(new Runnable() {
            @Override
            public void run() {
                if (callback != null) {
                    callback.onAddStream(stream);
                }
            }
        });
    }

    @Override
    public void onRemoveStream(final MediaStream stream) {
        Log.d(TAG, "onRemoveStream");
        executor.execute(new Runnable() {
            @Override
            public void run() {
                if (callback != null) {
                    callback.onRemoveStream(stream);
                }
            }
        });
    }

    @Override
    public void onDataChannel(final DataChannel dc) {
        Log.d(TAG, "New Data channel " + dc.label());

    }

    @Override
    public void onRenegotiationNeeded() {
        // No need to do anything; AppRTC follows a pre-agreed-upon
        // signaling/negotiation protocol.
    }

    @Override
    public void onAddTrack(RtpReceiver rtpReceiver, MediaStream[] mediaStreams) {

    }
}
