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

// Only available in Electron renderer
const electron = window.require ? window.require('electron') : null
const ipcRenderer = electron ? electron.ipcRenderer : null

// Utility to check if running in Electron
export function isElectron() {
  // Renderer process

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

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

const ElectronContext = createContext()

const ElectronProvider = ({ children }) => {
  const [platform, setPlatform] = useState('unknown')
  const [isMaximized, setIsMaximized] = useState(false)
  const [isFullScreen, setIsFullScreen] = useState(false)
  const [electronAvailable] = useState(isElectron())
  const navigate = useNavigate()

  // Function to open external URL via Electron
  const openExternalUrl = (url) => {
    if (electronAvailable && ipcRenderer) {
      ipcRenderer.invoke('open-external-url', url)
      return true
    }
    return false
  }

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

    // Get initial platform
    ipcRenderer.invoke('os-info').then((info) => {
      if (info && info.platform) setPlatform(info.platform)
    })

    // Get initial window state
    ipcRenderer.invoke('window-state').then((state) => {
      if (state && typeof state.isMaximized === 'boolean') {
        setIsMaximized(state.isMaximized)
      }
      if (state && typeof state.isFullScreen === 'boolean') {
        setIsFullScreen(state.isFullScreen)
      }
    })

    // Listen for window state changes
    const windowStateHandler = (event, state) => {
      if (state && typeof state.isMaximized === 'boolean') {
        setIsMaximized(state.isMaximized)
      }
      if (state && typeof state.isFullScreen === 'boolean') {
        setIsFullScreen(state.isFullScreen)
      }
    }
    ipcRenderer.on('window-state', windowStateHandler)

    // Listen for navigate
    const navigateHandler = (event, url) => {
      console.log('Navigating to:', url)
      navigate(url)
    }
    ipcRenderer.on('navigate', navigateHandler)

    return () => {
      ipcRenderer.removeListener('navigate', navigateHandler)
      ipcRenderer.removeListener('window-state', windowStateHandler)
    }
  }, [])

  // Window control handler
  const handleWindowControl = (action) => {
    if (electronAvailable && ipcRenderer) {
      ipcRenderer.send('window-control', action)
    }
  }

  return (
    <ElectronContext.Provider
      value={{
        platform,
        isMaximized,
        isFullScreen,
        isElectron: electronAvailable,
        handleWindowControl,
        openExternalUrl
      }}
    >
      {children}
    </ElectronContext.Provider>
  )
}

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

export { ElectronContext, ElectronProvider }
