import { createContext, useCallback, useEffect, useRef, useState } from 'react'
import PropTypes from 'prop-types'
import { useNavigate } from 'react-router-dom'

const electron = window.require ? window.require('electron') : null
const ipcRenderer = electron ? electron.ipcRenderer : null
const desktopAPI = window.electronAPI

// eslint-disable-next-line react-refresh/only-export-components
export function isElectron() {
  if (desktopAPI?.isDesktop) {
    return true
  }

  if (
    typeof window !== 'undefined' &&
    window.__electrobunWebviewId &&
    window.__electrobunRpcSocketPort
  ) {
    return true
  }

  if (
    typeof window !== 'undefined' &&
    window.process &&
    window.process.type === 'renderer'
  ) {
    return true
  }

  if (
    typeof navigator === 'object' &&
    typeof navigator.userAgent === 'string' &&
    navigator.userAgent.indexOf('Electron') >= 0
  ) {
    return true
  }

  return false
}

const ElectronContext = createContext()

function isInHorizontalScrollContainer(element) {
  let node = element
  while (node && node !== document.body) {
    const { overflowX } = window.getComputedStyle(node)
    if (
      (overflowX === 'auto' || overflowX === 'scroll') &&
      node.scrollWidth > node.clientWidth
    ) {
      return true
    }
    node = node.parentElement
  }
  return false
}

const ElectronProvider = ({ children }) => {
  const [platform, setPlatform] = useState('unknown')
  const [isMaximized, setIsMaximized] = useState(false)
  const [isFullScreen, setIsFullScreen] = useState(false)
  const [electronAvailable] = useState(isElectron())
  const useElectrobun = Boolean(desktopAPI?.isDesktop)
  const navigate = useNavigate()
  const lastNavigationAtRef = useRef(0)

  const navigateHistory = useCallback(
    (direction) => {
      const now = Date.now()
      if (now - lastNavigationAtRef.current < 300) return
      lastNavigationAtRef.current = now

      if (direction === 'back') {
        navigate(-1)
      } else if (direction === 'forward') {
        navigate(1)
      }
    },
    [navigate]
  )

  const applyWindowState = useCallback((state) => {
    if (state && typeof state.isMaximized === 'boolean') {
      setIsMaximized(state.isMaximized)
    }
    if (state && typeof state.isFullScreen === 'boolean') {
      setIsFullScreen(state.isFullScreen)
    }
  }, [])

  const openExternalUrl = (url) => {
    if (useElectrobun) {
      desktopAPI.openExternalUrl(url)
      return true
    }
    if (electronAvailable && ipcRenderer) {
      ipcRenderer.invoke('open-external-url', url)
      return true
    }
    return false
  }

  const openInternalUrl = (url) => {
    if (useElectrobun) {
      desktopAPI.openInternalUrl(url)
      return true
    }
    if (electronAvailable && ipcRenderer) {
      ipcRenderer.invoke('open-internal-url', url)
      return true
    }
    return false
  }

  useEffect(() => {
    if (!electronAvailable) return

    if (useElectrobun) {
      desktopAPI.getOsInfo().then((info) => {
        if (info?.platform) {
          setPlatform(info.platform)
          if (info.platform === 'darwin') {
            document.documentElement.classList.add('macos-vibrancy')
          }
        }
      })

      desktopAPI.getWindowState().then(applyWindowState)

      const unsubWindowState = desktopAPI.onMessage('windowState', applyWindowState)
      const unsubNavigate = desktopAPI.onMessage('navigate', (url) => {
        navigate(url)
      })
      const unsubNavigationGesture = desktopAPI.onMessage(
        'navigationGesture',
        navigateHistory
      )

      return () => {
        unsubWindowState()
        unsubNavigate()
        unsubNavigationGesture()
      }
    }

    if (!ipcRenderer) return

    ipcRenderer.invoke('os-info').then((info) => {
      if (info?.platform) {
        setPlatform(info.platform)
        if (info.platform === 'darwin') {
          document.documentElement.classList.add('macos-vibrancy')
        }
      }
    })

    ipcRenderer.invoke('window-state').then(applyWindowState)

    const windowStateHandler = (_event, state) => {
      applyWindowState(state)
    }
    ipcRenderer.on('window-state', windowStateHandler)

    const navigateHandler = (_event, url) => {
      navigate(url)
    }
    ipcRenderer.on('navigate', navigateHandler)

    const navigationGestureHandler = (_event, direction) => {
      navigateHistory(direction)
    }
    ipcRenderer.on('navigation-gesture', navigationGestureHandler)

    return () => {
      ipcRenderer.removeListener('navigate', navigateHandler)
      ipcRenderer.removeListener('navigation-gesture', navigationGestureHandler)
      ipcRenderer.removeListener('window-state', windowStateHandler)
    }
  }, [
    applyWindowState,
    electronAvailable,
    navigate,
    navigateHistory,
    useElectrobun
  ])

  useEffect(() => {
    if (!electronAvailable || platform !== 'darwin') return

    let accumulatedDeltaX = 0
    let resetTimer

    const handleWheel = (event) => {
      if (event.metaKey || event.ctrlKey || event.altKey || event.shiftKey) return
      if (Math.abs(event.deltaX) < Math.abs(event.deltaY)) return
      if (Math.abs(event.deltaX) < 2) return
      if (isInHorizontalScrollContainer(event.target)) return

      accumulatedDeltaX += event.deltaX
      clearTimeout(resetTimer)
      resetTimer = setTimeout(() => {
        if (Math.abs(accumulatedDeltaX) > 60) {
          navigateHistory(accumulatedDeltaX > 0 ? 'forward' : 'back')
        }
        accumulatedDeltaX = 0
      }, 80)
    }

    window.addEventListener('wheel', handleWheel, { passive: true })

    return () => {
      window.removeEventListener('wheel', handleWheel)
      clearTimeout(resetTimer)
    }
  }, [electronAvailable, navigateHistory, platform])

  const handleWindowControl = (action) => {
    if (useElectrobun) {
      desktopAPI.windowControl(action)
      return
    }
    if (electronAvailable && ipcRenderer) {
      ipcRenderer.send('window-control', action)
    }
  }

  const getAuthSession = async () => {
    if (!electronAvailable) return null
    if (useElectrobun) return await desktopAPI.getAuthSession()
    if (!ipcRenderer) return null
    return await ipcRenderer.invoke('auth-session-get')
  }

  const setAuthSession = async (session) => {
    if (!electronAvailable) return false
    if (useElectrobun) {
      const result = await desktopAPI.setAuthSession(session)
      return result?.ok ?? false
    }
    if (!ipcRenderer) return false
    return await ipcRenderer.invoke('auth-session-set', session)
  }

  const clearAuthSession = async () => {
    if (!electronAvailable) return false
    if (useElectrobun) {
      const result = await desktopAPI.clearAuthSession()
      return result?.ok ?? false
    }
    if (!ipcRenderer) return false
    return await ipcRenderer.invoke('auth-session-clear')
  }

  const getAppSettings = useCallback(async () => {
    if (!electronAvailable) return {}
    if (useElectrobun) return await desktopAPI.getAppSettings()
    if (!ipcRenderer) return {}
    return await ipcRenderer.invoke('app-settings-get')
  }, [electronAvailable, useElectrobun])

  const setAppSettings = useCallback(
    async (settings) => {
      if (!electronAvailable) return false
      if (useElectrobun) {
        const result = await desktopAPI.setAppSettings(settings)
        return result?.ok ?? false
      }
      if (!ipcRenderer) return false
      return await ipcRenderer.invoke('app-settings-set', settings)
    },
    [electronAvailable, useElectrobun]
  )

  const startAppUpdate = useCallback(
    async (update) => {
      if (!electronAvailable) return false
      if (useElectrobun) {
        const result = await desktopAPI.startAppUpdate(update)
        return result?.ok ?? false
      }
      if (!ipcRenderer) return false
      return await ipcRenderer.invoke('app-update-start', update)
    },
    [electronAvailable, useElectrobun]
  )

  const onAppUpdateProgress = useCallback(
    (handler) => {
      if (!electronAvailable || typeof handler !== 'function') {
        return () => {}
      }

      if (useElectrobun) {
        return desktopAPI.onMessage('appUpdateProgress', handler)
      }

      if (!ipcRenderer) return () => {}

      const progressHandler = (_event, progress) => {
        handler(progress)
      }

      ipcRenderer.on('app-update-progress', progressHandler)

      return () => {
        ipcRenderer.removeListener('app-update-progress', progressHandler)
      }
    },
    [electronAvailable, useElectrobun]
  )

  const getToken = async () => {
    const session = await getAuthSession()
    return session?.token || null
  }

  const setToken = async (token) => {
    const session = (await getAuthSession()) || {}
    return await setAuthSession({ ...session, token })
  }

  const resizeSpotlightWindow = async (height) => {
    if (!electronAvailable) return false
    try {
      if (useElectrobun) {
        const result = await desktopAPI.resizeSpotlightWindow(height)
        return result?.ok ?? false
      }
      if (!ipcRenderer) return false
      return await ipcRenderer.invoke('spotlight-window-resize', height)
    } catch (error) {
      console.warn(
        '[ElectronContext] Failed to resize spotlight window:',
        error
      )
      return false
    }
  }

  const setSidebarViewMenu = useCallback(
    async (sections) => {
      if (!electronAvailable) return false
      if (useElectrobun) {
        const result = await desktopAPI.setSidebarViewMenu(sections)
        return result?.ok ?? false
      }
      if (!ipcRenderer) return false
      return await ipcRenderer.invoke('set-sidebar-view-menu', sections)
    },
    [electronAvailable, useElectrobun]
  )

  const getElectronVersion = useCallback(async () => {
    if (!electronAvailable) return null
    if (useElectrobun) return await desktopAPI.getAppVersion()
    if (!ipcRenderer) return null
    return await ipcRenderer.invoke('electron-version')
  }, [electronAvailable, useElectrobun])

  return (
    <ElectronContext.Provider
      value={{
        platform,
        isMaximized,
        isFullScreen,
        isElectron: electronAvailable,
        handleWindowControl,
        openExternalUrl,
        openInternalUrl,
        getAuthSession,
        setAuthSession,
        clearAuthSession,
        getAppSettings,
        setAppSettings,
        startAppUpdate,
        onAppUpdateProgress,
        getToken,
        setToken,
        resizeSpotlightWindow,
        setSidebarViewMenu,
        getElectronVersion
      }}
    >
      {children}
    </ElectronContext.Provider>
  )
}

ElectronProvider.propTypes = {
  children: PropTypes.node.isRequired
}

export { ElectronContext, ElectronProvider }
