import { makeAutoObservable } from "mobx";

interface ContextData {
  routeId?: string;
  routeNumber?: string;
  startStopId?: string;
  endStopId?: string;
  nearestSightId?: string;
  nearestStationId?: string;
  maintenanceModeOn?: boolean;
  routeProgress?: {
    startStopId: string;
    endStopId: string;
    percentageCompleted: number;
  };
  rawCoordinates?: {
    latitude: number;
    longitude: number;
  };
  currentCoordinates?: {
    latitude: number;
    longitude: number;
  };
  error?: string | boolean | { message?: string };
  message?: string;
  status?: string;
}

interface GeolocationStore {
  contextData: ContextData | null;
  nearestSightId: string | null;
  selectedSightId: string | null;
  selectedLanguage: string;
  selectedLanguageRight: string;
  nearestStationId: string | null;
  currentStationId: string | null;
  isLoading: boolean;
  error: string | null;
  isLeftWidgetOpen: boolean;
  isTransferWidgetOpen: boolean;
  isManualSelection: boolean;
  isRightWidgetSelectorOpen: boolean;
  isGovernorWidgetOpen: boolean;
  activeClusterId: string | null;
  setIsManualSelection: (val: boolean) => void;
  setIsRightWidgetSelectorOpen: (isOpen: boolean) => void;
  setIsGovernorWidgetOpen: (isOpen: boolean) => void;
  setActiveClusterId: (id: string | null) => void;
  setIsTransferWidgetOpen: (isOpen: boolean) => void;
  setCurrentStationId: (id: string | null) => void;
}

class GeolocationStore implements GeolocationStore {
  contextData: ContextData | null = null;
  nearestSightId: string | null = null;
  selectedSightId: string | null = null;
  selectedLanguage: string = "ru";
  selectedLanguageRight: string = "ru";
  nearestStationId: string | null = null;
  currentStationId: string | null = null;
  isLoading: boolean = true;
  error: string | null = null;
  isLeftWidgetOpen: boolean = false;
  isTransferWidgetOpen: boolean = false;
  isManualSelection: boolean = false;
  isRightWidgetSelectorOpen: boolean = false;
  isGovernorWidgetOpen: boolean = false;
  activeClusterId: string | null = null;

  constructor() {
    makeAutoObservable(this);
  }

  setContextData = (data: ContextData) => {
    this.contextData = data;
  };

  setNearestSightId = (id: string | null) => {
    this.nearestSightId = id;
  };

  setSelectedSightId = (id: string | null) => {
    this.selectedSightId = id;
  };

  setSelectedLanguage = (language: string) => {
    this.selectedLanguage = language;
  };

  setSelectedLanguageRight = (language: string) => {
    this.selectedLanguageRight = language;
  };

  setNearestStationId = (id: string | null) => {
    this.nearestStationId = id;
  };

  setCurrentStationId = (id: string | null) => {
    this.currentStationId = id;
  };

  setIsLoading = (loading: boolean) => {
    this.isLoading = loading;
  };

  setError = (error: string | null) => {
    this.error = error;
  };

  setIsLeftWidgetOpen = (isOpen: boolean) => {
    this.isLeftWidgetOpen = isOpen;
  };

  setIsTransferWidgetOpen = (isOpen: boolean) => {
    this.isTransferWidgetOpen = isOpen;
  };

  setIsManualSelection = (val: boolean) => {
    this.isManualSelection = val;
  };

  setIsRightWidgetSelectorOpen = (isOpen: boolean) => {
    this.isRightWidgetSelectorOpen = isOpen;
  };

  setIsGovernorWidgetOpen = (isOpen: boolean) => {
    this.isGovernorWidgetOpen = isOpen;
  };

  closeGovernorModal = () => {
    this.isGovernorWidgetOpen = false;
  };

  setActiveClusterId = (id: string | null) => {
    this.activeClusterId = id;
  };

  sortingBy: string = "asc";

  setSortingBy = (sorting: string) => {
    this.sortingBy = sorting;
  };
}

export const geolocationStore = new GeolocationStore();
export { GeolocationStore };
