package io.amarcruz.photoview;

import android.support.annotation.IntDef;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.Event;
import com.facebook.react.uimanager.events.RCTEventEmitter;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class ImageEvent extends Event<ImageEvent> {
  @IntDef({ON_ERROR, ON_LOAD, ON_LOAD_END, ON_LOAD_START, ON_TAP, ON_VIEW_TAP, ON_SCALE})
  @Retention(RetentionPolicy.SOURCE)
  @interface ImageEventType {}

  static final int ON_ERROR = 1;
  static final int ON_LOAD = 2;
  static final int ON_LOAD_END = 3;
  static final int ON_LOAD_START = 4;
  static final int ON_TAP = 5;
  static final int ON_VIEW_TAP = 6;
  static final int ON_SCALE = 7;

  private final int mEventType;
  private WritableMap mMap;

  ImageEvent(int viewId, @ImageEventType int eventType) {
    super(viewId);
    mEventType = eventType;
    mMap = null;
  }

  static String eventNameForType(@ImageEventType int eventType) {
    switch(eventType) {
      case ON_ERROR:
        return "photoViewError";
      case ON_LOAD:
        return "photoViewLoad";
      case ON_LOAD_END:
        return "photoViewLoadEnd";
      case ON_LOAD_START:
        return "photoViewLoadStart";
      case ON_TAP:
        return "photoViewTap";
      case ON_VIEW_TAP:
        return "photoViewViewTap";
      case ON_SCALE:
        return "photoViewScale";
      default:
        throw new IllegalStateException("Invalid image event: " + eventType);
    }
  }

  @Override
  public String getEventName() {
    return ImageEvent.eventNameForType(mEventType);
  }

  @Override
  public short getCoalescingKey() {
    // Intentionally casting mEventType because it is guaranteed to be small
    // enough to fit into short.
    return (short) mEventType;
  }

  @Override
  public void dispatch(RCTEventEmitter rctEventEmitter) {
    rctEventEmitter.receiveEvent(getViewTag(), getEventName(), mMap);
  }

  ImageEvent setExtras(WritableMap map){
    this.mMap = map;
    return this;
  }
}
