import { forwardRef, useEffect, useImperativeHandle, useState } from 'react';

import { HandleEvents } from '../helpers/event';
import { Overlay } from '../overlay';
import type { UIEventHandlers } from '../types/event';
import { useNavermaps } from '../use-navermaps';
import { omitUndefined } from '../utils/omit-undefined';

const primitiveKvoKeys = [
  'strokeWeight',
  'strokeOpacity',
  'strokeColor',
  'strokeStyle',
  'strokeLineCap',
  'strokeLineJoin',
  'fillColor',
  'fillOpacity',
  'clickable',
  'visible',
  'zIndex',
] as const;
const kvoKeys = [
  ...primitiveKvoKeys,
  'bounds',
] as const;
const kvoEvents = kvoKeys.map(key => `${key}_changed`);
const uiEvents = [
  'mousedown',
  'mouseup',
  'click',
  'dblclick',
  'rightclick',
  'mouseover',
  'mouseout',
  'mousemove',
] as const;
const events = [...uiEvents, ...kvoEvents];

type EllipseOptions = {
  /**
   * bounds
   * @type naver.maps.Bounds | naver.maps.BoundsLiteral
   */
  bounds: naver.maps.Bounds | naver.maps.BoundsLiteral;
  strokeWeight?: number;
  strokeOpacity?: number;
  strokeColor?: string;
  strokeStyle?: naver.maps.StrokeStyleType;
  strokeLineCap?: naver.maps.StrokeLineCapType;
  strokeLineJoin?: naver.maps.StrokeLineJoinType;
  fillColor?: string;
  fillOpacity?: number;
  clickable?: boolean;
  visible?: boolean;
  zIndex?: number;
};

export type Props = EllipseOptions & {
  onBoundsChanged?: (value: naver.maps.Bounds) => void;
  onStrokeWeightChanged?: (value: number) => void;
  onStrokeOpacityChanged?: (value: number) => void;
  onStrokeColorChanged?: (value: string) => void;
  onStrokeStyleChanged?: (value: naver.maps.StrokeStyleType) => void;
  onStrokeLineCapChanged?: (value: naver.maps.StrokeLineCapType) => void;
  onStrokeLineJoinChanged?: (value: naver.maps.StrokeLineJoinType) => void;
  onFillColorChanged?: (value: string) => void;
  onFillOpacityChanged?: (value: number) => void;
  onClickableChanged?: (event: boolean) => void;
  onVisibleChanged?: (event: boolean) => void;
  onZIndexChanged?: (event: number) => void;
} & UIEventHandlers<typeof uiEvents>;

export const Ellipse = forwardRef<naver.maps.Ellipse, Props>(function Ellipse(props, ref) {
  const { bounds } = props;
  const navermaps = useNavermaps();
  const [ellipse] = useState(() => new navermaps.Ellipse(omitUndefined({
    bounds,
    ...Object.fromEntries(kvoKeys.filter(key => key !== 'bounds').map(key => [key, props[key]])),
  }) as EllipseOptions));

  useImperativeHandle<naver.maps.Ellipse | undefined, naver.maps.Ellipse | undefined>(ref, () => ellipse);

  useEffect(() => {
    const options = {
      ...Object.fromEntries(primitiveKvoKeys.map(key => [key, props[key]])),
      bounds,
    };
    const cleanOptions = omitUndefined(options);
    if (Object.keys(cleanOptions).length > 0) {
      ellipse.setOptions(cleanOptions as EllipseOptions);
    }
  }, primitiveKvoKeys.map(key => props[key]));

  useEffect(() => {
    if (bounds && ellipse.getBounds().equals(bounds as naver.maps.Bounds)) {
      ellipse.setBounds(bounds);
    }
  }, [bounds]);

  return (
    <Overlay element={ellipse}>
      <HandleEvents events={events} listeners={props as any} />
    </Overlay>
  );
});
