package io.reactivex.remote.internal;

import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;

/**
 * @hide Used internally
 * <p>
 * Wraps a remote {@link IBinder} that implements {@link RemoteEventListener} interface
 * <p>
 * Autogenerated by <a href="https://bit.ly/Remoter">Remoter</a>
 * @see RemoteEventListener_Stub
 */
public class RemoteEventListener_Proxy implements RemoteEventListener {
    private static final String DESCRIPTOR = "polycom.conference.RemoteEventListener";

    private static final int TRANSACTION_onRemoteEvent_0 = android.os.IBinder.FIRST_CALL_TRANSACTION + 0;

    private static final int TRANSACTION_onCompleted_1 = android.os.IBinder.FIRST_CALL_TRANSACTION + 1;

    private static final int TRANSACTION_onError_2 = android.os.IBinder.FIRST_CALL_TRANSACTION + 2;

    private IBinder mRemote;

    /**
     * Initialize this {@link RemoteEventListener_Proxy} with the given {@link IBinder}
     *
     * @param binder An {@link IBinder} that exposes a remote {@link RemoteEventListener}
     */
    public RemoteEventListener_Proxy(IBinder binder) {
        this.mRemote = binder;
    }

    @Override
    public void onRemoteEvent(Bundle remoteData) {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            if (remoteData != null) {
                data.writeInt(1);
                remoteData.writeToParcel(data, 0);
            } else {
                data.writeInt(0);
            }
            mRemote.transact(TRANSACTION_onRemoteEvent_0, data, reply, android.os.IBinder.FLAG_ONEWAY);
        } catch (RemoteException re) {
            throw new RuntimeException(re);
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public void onError(Exception exception) {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            if (exception != null) {
                data.writeString(exception.getMessage());
            } else {
                data.writeString("");
            }
            mRemote.transact(TRANSACTION_onError_2, data, reply, android.os.IBinder.FLAG_ONEWAY);
        } catch (RemoteException re) {
            throw new RuntimeException(re);
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    /**
     * Register a {@link android.os.IBinder.DeathRecipient} to know of binder connection lose
     */
    public void linkToDeath(IBinder.DeathRecipient deathRecipient) {
        try {
            mRemote.linkToDeath(deathRecipient, 0);
        } catch (Exception ignored) {
        }
    }

    /**
     * UnRegister a {@link android.os.IBinder.DeathRecipient} to know of binder connection lose
     */
    public void unLinkToDeath(IBinder.DeathRecipient deathRecipient) {
        try {
            mRemote.unlinkToDeath(deathRecipient, 0);
        } catch (Exception ignored) {
        }
    }


    @Override
    public void onCompleted() {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_onCompleted_1, data, reply, android.os.IBinder.FLAG_ONEWAY);
        } catch (RemoteException re) {
            throw new RuntimeException(re);
        } finally {
            reply.recycle();
            data.recycle();
        }
    }
}