import { create } from 'zustand';
import { ToolType } from '../types/tools';
import { Point } from 'ol/geom';
import Map from 'ol/Map';
import { Coordinate } from 'ol/coordinate';
import { SatelliteMapsProvider } from '../interfaces/map';

interface MapState {
    currentTool: ToolType;
    measureType: "LineString" | "Polygon";
    measureShowSegments: boolean;
    measureClearPrevious: boolean;
    tipPoint: Point | null;
    map: Map | null;
    currentZ: number | undefined;
    currentX: number | undefined;
    currentY: number | undefined;
    currentCoordinate: Coordinate | null;
    statusText: string;
    satMapsProvider: SatelliteMapsProvider;
    selectedObjectType: number | null;
    alignMode: boolean;
}

export const useMapStore = create<MapState>(() => ({
    currentTool: null,
    measureType: "LineString",
    measureShowSegments: true,
    measureClearPrevious: true,
    tipPoint: null,
    map: null,
    currentZ: undefined,
    currentX: undefined,
    currentY: undefined,
    currentCoordinate: null,
    statusText: '',
    satMapsProvider: 'google',
    selectedObjectType: null,
    alignMode: false
}));

const setCurrentZ = (z: number | undefined) => useMapStore.setState(() => ({ currentZ: z }))
const setCurrentX = (x: number | undefined) => useMapStore.setState(() => ({ currentX: x }))
const setCurrentY = (y: number | undefined) => useMapStore.setState(() => ({ currentY: y }))
const setCurrentCoordinate = (c: Coordinate | null) => useMapStore.setState(() => ({ currentCoordinate: c }))
const setStatusText = (t: string) => useMapStore.setState(() => ({ statusText: t }))
const setSatMapsProvider = (p: SatelliteMapsProvider) => useMapStore.setState(() => ({ satMapsProvider: p }))
const setSelectedObjectType = (t: number | null) => useMapStore.setState(() => ({ selectedObjectType: t }))
const setMap = (m: Map | null) => useMapStore.setState(() => ({ map: m }))
const setAlignMode = (m: boolean) => useMapStore.setState(() => ({ alignMode: m }))

const setTipPoint = (tipPoint: Point | null) => {
    useMapStore.setState(() => ({ tipPoint: tipPoint }))
}

const getTipPoint = () => useMapStore.getState().tipPoint

const getAlignMode = () => useMapStore.getState().alignMode

const getMap = () => useMapStore.getState().map

const setMeasureType = (tool: "LineString" | "Polygon") => useMapStore.setState(() => ({ measureType: tool }))

const getMeasureType = () => useMapStore.getState().measureType

const setCurrentTool = (tool: ToolType) => {
    tool === useMapStore.getState().currentTool
        ? useMapStore.setState(() => ({ currentTool: null }))
        : useMapStore.setState(() => ({ currentTool: tool }))
}

const getCurrentTool = () => useMapStore.getState().currentTool

const getMeasureShowSegments = () => useMapStore.getState().measureShowSegments

const getMeasureClearPrevious = () => useMapStore.getState().measureClearPrevious

const setMeasureShowSegments = (bool: boolean) => {
    useMapStore.setState(() => ({ measureShowSegments: bool }))
}

const setMeasureClearPrevious = (bool: boolean) => {
    useMapStore.setState(() => ({ measureClearPrevious: bool }))
}

export {
    setCurrentTool,
    getCurrentTool,
    setMeasureShowSegments,
    setMeasureClearPrevious,
    getMeasureShowSegments,
    getMeasureClearPrevious,
    setMeasureType,
    getMeasureType,
    getTipPoint,
    setTipPoint,
    setCurrentZ,
    setCurrentX,
    setCurrentY,
    setCurrentCoordinate,
    setStatusText,
    setSatMapsProvider,
    setSelectedObjectType,
    setMap,
    getMap,
    setAlignMode,
    getAlignMode
}