package com.reactlibrary;

import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.yodo1.advert.callback.VideoCallback;
import com.yodo1.advert.entity.AdErrorCode;
import com.yodo1.advert.open.Yodo1Advert;

public class MasModule extends ReactContextBaseJavaModule {

    private final ReactApplicationContext reactContext;
    private final DeviceEventManagerModule.RCTDeviceEventEmitter mEventEmitter;
    private final VideoCallback videoCallback;

    public enum Events {

        DID_CLICKED("rewardedVideoDidClicked"), DID_SHOW("rewardedVideoDidShow"),
        DID_FAIL_TO_SHOW("rewardedVideoDidFailToShow"), DID_CLOSE("rewardedVideoDidClose");

        private final String mName;

        Events(final String name) {
            mName = name;
        }

        @Override
        public String toString() {
            return mName;
        }
    }

    public MasModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
        this.mEventEmitter = this.reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
        this.videoCallback = new VideoCallback() {

            /**
             * Called after an video has been dismissed.
             *
             * @param isFinished
             */
            public void onVideoClosed(boolean isFinished) {
                sendEvent(Events.DID_CLOSE.toString(), null);
            }

            ;

            /**
             * Called after an video has been displayed on the screen.
             *
             */
            public void onVideoShow() {
                sendEvent(Events.DID_SHOW.toString(), null);
            }

            ;

            /**
             * Called after an video has attempted to show but failed.
             *
             * @param errorCode The reason for the error
             */
            public void onVideoShowFailed(AdErrorCode errorCode) {
                sendEvent(Events.DID_FAIL_TO_SHOW.toString(), null);
            }

            ;

            /**
             * Called after an video has been clicked.
             */
            public void onVideoClicked() {
                sendEvent(Events.DID_CLICKED.toString(), null);
            }

            ;

        };
    }

    @Override
    public String getName() {
        return "Mas";
    }

    // @ReactMethod
    // public void sampleMethod(String stringArgument, int numberArgument, Callback
    // callback) {
    // // TODO: Implement some actually useful functionality
    // callback.invoke("Received numberArgument: " + numberArgument + "
    // stringArgument: " + stringArgument);
    // }

    @Override
    public void initialize() {
        Yodo1Advert.initSDK(getCurrentActivity(), "jROArT6belr");
    }

    @ReactMethod
    public void getIsReady(final Promise promise) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                boolean isReady = Yodo1Advert.videoIsReady(getCurrentActivity());
                promise.resolve(isReady);
            }
        });
    }

    private void sendEvent(String eventName, @Nullable WritableMap params) {
        mEventEmitter.emit(eventName, params);
    }

    @ReactMethod
    public void showAds(final Promise promise) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Yodo1Advert.showVideo(getCurrentActivity(), videoCallback);
                promise.resolve(true);
            }
        });

    }

}
