package org.reactnative.camera.events;

import androidx.core.util.Pools;

import org.reactnative.camera.CameraViewManager;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.Event;
import com.facebook.react.uimanager.events.RCTEventEmitter;

public class PreviewColorEvent extends Event<PreviewColorEvent> {
  private static final Pools.SynchronizedPool<PreviewColorEvent> EVENTS_POOL = new Pools.SynchronizedPool<>(3);
  private PreviewColorEvent() {}
  private int color;
  private int r, g, b;

  public static PreviewColorEvent obtain(int viewTag, int color, int r, int g, int b) {
    PreviewColorEvent event = EVENTS_POOL.acquire();
    if (event == null) {
      event = new PreviewColorEvent();
    }
    event.init(viewTag, color, r, g, b);
    return event;
  }

  private void init(int viewTag, int color, int r, int g, int b) {
    super.init(viewTag);
    this.color = color;
    this.r = r;
    this.g = g;
    this.b = b;
  }

  @Override
  public short getCoalescingKey() {
    return 0;
  }

  @Override
  public String getEventName() {
    return CameraViewManager.Events.EVENT_PREVIEW_COLOR.toString();
  }

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

  private WritableMap serializeEventData() {

    WritableMap map = Arguments.createMap();
    map.putInt("color", color);
    map.putInt("r", r);
    map.putInt("g", g);
    map.putInt("b", b);
    return map;
  }
}
