import { useCallback } from 'react'
import { useMeasurementToolsState } from './provider/provider'
import type { MeasurementToolsState } from './types'

export function useMeasurementTools({ mapId }: { mapId: string }) {
  const modes = useMeasurementToolsState(
    (state) => state.measurements[mapId]!.modes,
  )

  const units = useMeasurementToolsState(
    (state) => state.measurements[mapId]!.units,
  )

  const distance = useMeasurementToolsState(
    (state) => state.measurements[mapId]!.distance,
  )
  const _setDistance = useMeasurementToolsState((state) => state.setDistance)

  const setDistance = useCallback(
    (distance: number) => _setDistance(mapId, distance),
    [_setDistance, mapId],
  )

  const clearLayerOnStart = useMeasurementToolsState(
    (state) => state.measurements[mapId]!.clearLayerOnStart,
  )

  const enabled = useMeasurementToolsState(
    (state) => state.measurements[mapId]!.enabled,
  )
  const _setVisibility = useMeasurementToolsState((state) => state.setEnabled)

  const setEnabled = useCallback(
    (enabled: MeasurementToolsState['enabled']) =>
      _setVisibility(mapId, enabled),
    [_setVisibility, mapId],
  )

  const modeSelected = useMeasurementToolsState(
    (state) => state.measurements[mapId]!.modeSelected,
  )

  const _setModeSelected = useMeasurementToolsState(
    (state) => state.setModeSelected,
  )

  const setModeSelected = useCallback(
    (mode: MeasurementToolsState['modeSelected']) => {
      setEnabled(!!mode)
      _setModeSelected(mapId, mode)
    },
    [_setModeSelected, mapId, setEnabled],
  )

  const finished = useMeasurementToolsState(
    (state) => state.measurements[mapId]!.finished,
  )
  const _setFinished = useMeasurementToolsState((state) => state.setFinished)

  const setFinished = useCallback(
    (finished: MeasurementToolsState['finished']) =>
      _setFinished(mapId, finished),
    [_setFinished, mapId],
  )

  const locale = useMeasurementToolsState(
    (state) => state.measurements[mapId]!.locale,
  )
  const _setLocale = useMeasurementToolsState((state) => state.setLocale)

  const setLocale = (locale: MeasurementToolsState['locale']) =>
    _setLocale(mapId, locale)

  const modeInstance = useMeasurementToolsState(
    (state) => state.measurements[mapId]!.modeInstance,
  )

  const _setInstance = useMeasurementToolsState(
    (state) => state.setModeInstance,
  )

  const setModeInstance = useCallback(
    (modeInstance: MeasurementToolsState['modeInstance']) =>
      _setInstance(mapId, modeInstance),
    [_setInstance, mapId],
  )

  const positions = useMeasurementToolsState(
    (state) => state.measurements[mapId]!.positions,
  )
  const _setPositions = useMeasurementToolsState((state) => state.setPositions)

  const setPositions = useCallback(
    (positions: MeasurementToolsState['positions']) =>
      _setPositions(mapId, positions),
    [mapId, _setPositions],
  )

  const unit = useMeasurementToolsState(
    (state) => state.measurements[mapId]!.unit,
  )
  const _setUnit = useMeasurementToolsState((state) => state.setUnit)

  const setUnit = useCallback(
    (unit: MeasurementToolsState['unit']) => _setUnit(mapId, unit),
    [_setUnit, mapId],
  )

  const clearLayer = useCallback(() => {
    modeInstance?.resetClickSequence()
    setPositions([])
  }, [modeInstance, setPositions])

  const toggleLayer = useCallback(
    (enabled: boolean) => {
      setEnabled(enabled)
    },
    [setEnabled],
  )

  return {
    modes,
    units,
    distance,
    clearLayerOnStart,
    modeSelected,
    enabled,
    finished,
    locale,
    modeInstance,
    positions,
    unit,
    setDistance,
    setModeSelected,
    setFinished,
    setLocale,
    setPositions,
    setUnit,
    setEnabled,
    toggleLayer,
    clearLayer,
    setModeInstance,
  }
}
