import { create } from 'zustand'
import { persist } from 'zustand/middleware'

export interface DetectorType {
  detector_id: number
  name: string
  serial_number: string
  object: string
  status: string
  type: string
  location: string
  floor: number
  checked: boolean
  notifications: Array<{
    id: number
    type: string
    message: string
    timestamp: string
    acknowledged: boolean
    priority: string
  }>
}

interface NotificationType {
  id: number
  detector_id: number
  detector_name: string
  type: string
  status: string
  message: string
  timestamp: string
  location: string
  object: string
  acknowledged: boolean
  priority: string
}

export interface NavigationStore {
  currentObject: { id: string | undefined; title: string | undefined }
  navigationHistory: string[]
  currentSubmenu: string | null
   
  showMonitoring: boolean
  showFloorNavigation: boolean
  showNotifications: boolean
   
  selectedDetector: DetectorType | null
  showDetectorMenu: boolean
  selectedNotification: NotificationType | null
  showNotificationDetectorInfo: boolean
    
  setCurrentObject: (id: string | undefined, title: string | undefined) => void
  clearCurrentObject: () => void
  addToHistory: (path: string) => void
  goBack: () => string | null
   
  setCurrentSubmenu: (submenu: string | null) => void
  clearSubmenu: () => void
   
  openMonitoring: () => void
  closeMonitoring: () => void
  openFloorNavigation: () => void
  closeFloorNavigation: () => void
  openNotifications: () => void
  closeNotifications: () => void
   
  setSelectedDetector: (detector: DetectorType | null) => void
  setShowDetectorMenu: (show: boolean) => void
  setSelectedNotification: (notification: NotificationType | null) => void
  setShowNotificationDetectorInfo: (show: boolean) => void
   
  isOnNavigationPage: () => boolean
  getCurrentRoute: () => string | null
  getActiveSidebarItem: () => number
}

const useNavigationStore = create<NavigationStore>()(
  persist(
    (set, get) => ({ 
      currentObject: {
        id: undefined,
        title: undefined,
      },
      navigationHistory: [],
      currentSubmenu: null,
       
      showMonitoring: false,
      showFloorNavigation: false,
      showNotifications: false,
       
      selectedDetector: null,
      showDetectorMenu: false,
      selectedNotification: null,
      showNotificationDetectorInfo: false,
       
      setCurrentObject: (id: string | undefined, title: string | undefined) => 
        set({ currentObject: { id, title } }),
      
      clearCurrentObject: () => 
        set({ currentObject: { id: undefined, title: undefined } }),
      
      addToHistory: (path: string) => {
        const { navigationHistory } = get()
        const newHistory = [...navigationHistory, path] 
        if (newHistory.length > 10) {
          newHistory.shift()
        }
        set({ navigationHistory: newHistory })
      },
      
      goBack: () => {
        const { navigationHistory } = get()
        if (navigationHistory.length > 1) {
          const newHistory = [...navigationHistory]
          newHistory.pop()  
          const previousPage = newHistory.pop() 
          set({ navigationHistory: newHistory })
          return previousPage || null
        }
        return null
      },
      
      setCurrentSubmenu: (submenu: string | null) => 
        set({ currentSubmenu: submenu }),
      
      clearSubmenu: () => 
        set({ currentSubmenu: null }),
       
      openMonitoring: () => set({ 
        showMonitoring: true, 
        showFloorNavigation: false, 
        showNotifications: false,
        currentSubmenu: 'monitoring',
        showDetectorMenu: false,
        selectedDetector: null,
        showNotificationDetectorInfo: false,
        selectedNotification: null
      }),
      
      closeMonitoring: () => set({ 
        showMonitoring: false, 
        currentSubmenu: null 
      }),
      
      openFloorNavigation: () => set({ 
        showFloorNavigation: true, 
        showMonitoring: false, 
        showNotifications: false,
        currentSubmenu: 'floors',
        showNotificationDetectorInfo: false,
        selectedNotification: null
      }),
      
      closeFloorNavigation: () => set({ 
        showFloorNavigation: false, 
        showDetectorMenu: false,
        selectedDetector: null,
        currentSubmenu: null 
      }),
      
      openNotifications: () => set({ 
        showNotifications: true, 
        showMonitoring: false, 
        showFloorNavigation: false,
        currentSubmenu: 'notifications',
        showDetectorMenu: false,
        selectedDetector: null
      }),
      
      closeNotifications: () => set({ 
        showNotifications: false, 
        showNotificationDetectorInfo: false,
        selectedNotification: null,
        currentSubmenu: null 
      }),
       
      setSelectedDetector: (detector: DetectorType | null) => set({ selectedDetector: detector }),
      setShowDetectorMenu: (show: boolean) => set({ showDetectorMenu: show }),
      setSelectedNotification: (notification: NotificationType | null) => set({ selectedNotification: notification }),
      setShowNotificationDetectorInfo: (show: boolean) => set({ showNotificationDetectorInfo: show }),
 
      isOnNavigationPage: () => {
        const { navigationHistory } = get()
        const currentRoute = navigationHistory[navigationHistory.length - 1]
        return currentRoute === '/navigation'
      },
      
      getCurrentRoute: () => {
        const { navigationHistory } = get()
        return navigationHistory[navigationHistory.length - 1] || null
      },
      
      getActiveSidebarItem: () => {
        const { showMonitoring, showFloorNavigation, showNotifications } = get()
        if (showMonitoring) return 3 // Зоны Мониторинга
        if (showFloorNavigation) return 4 // Навигация по этажам
        if (showNotifications) return 5 // Уведомления
        return 2 // Навигация (базовая)
      }
    }),
    {
      name: 'navigation-store',
    }
  )
)

export default useNavigationStore
 