import { useRef, useState } from "react";
import { UserLocation } from "../MainMapComponent/Map";
import { Landmark } from "../../../hooks/useLandmarks";
import MapView from "react-native-maps";

export const useMapState = () => {
    /**
     * Flag that toggles whether or not foreground location permission has been granted.
     */
     const [fgroundLocationPermission, toggleFgroundLocationPermission] = useState<boolean>(false);
    /**
     * Flag that toggles whether or not background location permission has been granted.
     */
     const [bgroundLocationPermission, toggleBgroundLocationPermission] = useState<boolean>(false);
    /**
     * Flag that toggles whether or not location is currently permitted (true if fground is granted is true and AppState is active, or if bground is active).
     */
    const [locationPermitted, toggleLocationPermitted] = useState<boolean>(false);
    /**
     * State that contains the new {@link Landmark} object which is passed down to the {@link AddLandmark} modal.
     */
    const [newLandmark, setNewLandmark] = useState<Landmark>(undefined);
    /**
     * State that contains the selected {@link Landmark} object which is passed down to the {@link LandmarkDetails} modal.
     */
    const [selectedLandmarkId, setSelectedLandmarkId] = useState<string>('');
    /**
     * State that contains the selected {@link Landmark} object which is passed down to the {@link LandmarkDetails} modal.
     */
    const [landmarksNearUser, setLandmarksNearUser] = useState<Landmark[]>([])
    /**
     * Holds the visibility state of the {@link AddLandmark} modal.
     */
    const [lmAddVisible, toggleLmAdd] = useState<boolean>(false);
    /**
     * Holds the visibility state of the {@link AddLandmark} modal.
     */
    const alertedLmVisibleState = false;
    const [alertedLmVisible, toggleAlertedLmPanel] = useState<boolean>(alertedLmVisibleState);
    /**
     * Holds the visibility state of the {@link LandmarkDetails} modal.
     */
    const [lmDetailsVisible, toggleLmDetails] = useState<boolean>(false);
    /**
     * Flag that toggles whether or not editing is enabled in the {@link LandmarkDetails} modal. 
     * The parent Map component has access to it so that it can disable closing the modal on backdrop press when it is enabled.
     */
    const [lmDetailsEditing, toggleLmDetailsEditing] = useState<boolean>(false);
    /**
     * Flag that toggles whether or not voice recording permission has been granted.
     */
     const [voicePermission, toggleVoicePermission] = useState<boolean>(false);
    /**
     * Holds the visibility state of the {@link VoicePanel} modal.
     */
     const [voiceVisible, toggleVoiceVisible] = useState<boolean>(false);
    /**
     * Holds the listening state of the {@link VoicePanel} modal.
     */
     const [voiceActive, toggleVoiceActive] = useState<boolean>(false);
    /**
     * State that holds a {@link UserLocation} object retrieved from location services.
     */
    const [userLocation, setUserLocation] = useState<UserLocation>(undefined);
    /**
     * Flag that determines whether the map should focus and follow the user's location.
     */
    const [followUser, toggleFollowUser] = useState<boolean>(false);
    /**
     * Flag that determines whether the filter options are shown
     */
    const [filterVisible, toggleFilter] = useState<boolean>(false)
    /**
     * State of minumum landmark rating filter
     */
    const [minLmRating, setMinLmRating] = useState<number>(0)
        /**
     * State of landmark types filter
     */
    const [lmFilteredTypes, setLmTypeFilter] = useState<number[]>([])
    /**
     * State of landmark "only show owned" filter
     */
    const [onlyOwned, toggleOnlyOwned] = useState<boolean>(false)
    /**
     * Ref that holds the loaded [MapView]{@link https://github.com/react-native-maps/react-native-maps/blob/master/docs/mapview.md} instance.
     */
     const mapRef = useRef<MapView>();

    return { 
        onlyOwned, toggleOnlyOwned,
        lmFilteredTypes, setLmTypeFilter,
        minLmRating, setMinLmRating,
        filterVisible, toggleFilter,
        followUser, toggleFollowUser,
        userLocation, setUserLocation,
        voicePermission, toggleVoicePermission,
        voiceVisible, toggleVoiceVisible,
        alertedLmVisible, toggleAlertedLmPanel,
        lmDetailsVisible, toggleLmDetails,
        lmDetailsEditing, toggleLmDetailsEditing,
        selectedLandmarkId, setSelectedLandmarkId,
        landmarksNearUser, setLandmarksNearUser,
        newLandmark, setNewLandmark,
        lmAddVisible, toggleLmAdd,
        fgroundLocationPermission, toggleFgroundLocationPermission,
        bgroundLocationPermission, toggleBgroundLocationPermission,
        locationPermitted, toggleLocationPermitted,
        mapRef
     }
}