import { app, BrowserWindow, ipcMain, shell, Menu } from 'electron'
import path, { dirname } from 'path'
import { fileURLToPath } from 'url'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

let win

function createWindow() {
  win = new BrowserWindow({
    width: 1200,
    height: 800,
    frame: false,
    titleBarStyle: 'hiddenInset',
    trafficLightPosition: { x: 14, y: 12 },
    backgroundColor: '#141414',
    icon: path.join(__dirname, './logo512.png'),
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false
    }
  })

  // Set up custom menu bar
  const env = (process.env.NODE_ENV || 'development').trim()
  if (env === 'development') {
    const devMenu = [
      {
        label: 'Developer',
        submenu: [
          {
            label: 'Toggle Developer Tools',
            accelerator:
              process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
            click: () => {
              win.webContents.toggleDevTools()
            }
          }
        ]
      }
    ]
    const menu = Menu.buildFromTemplate(devMenu)
    Menu.setApplicationMenu(menu)
  } else {
    Menu.setApplicationMenu(null)
  }

  // For development, load from localhost; for production, load the built index.html
  if (process.env.ELECTRON_START_URL) {
    win.loadURL(process.env.ELECTRON_START_URL)
  } else {
    win.loadFile(path.join(__dirname, '../build/index.html'))
  }

  setupWindowEvents()
}

app.whenReady().then(createWindow)

// IPC handler to get OS
ipcMain.handle('os-info', () => {
  return {
    platform: process.platform
  }
})

// IPC handler to get window state
ipcMain.handle('window-state', () => {
  return {
    isFullScreen: win ? win.isFullScreen() : false,
    isMaximized: win ? win.isMaximized() : false
  }
})

// Emit events to renderer when window is maximized/unmaximized
function setupWindowEvents() {
  if (!win) return
  win.on('maximize', () => {
    win.webContents.send('window-state', {
      isMaximized: true
    })
  })
  win.on('enter-full-screen', () => {
    console.log('Entered fullscreen')
    win.webContents.send('window-state', {
      isFullScreen: true
    })
  })
  win.on('leave-full-screen', () => {
    win.webContents.send('window-state', {
      isFullScreen: false
    })
  })
  win.on('unmaximize', () => {
    win.webContents.send('window-state', {
      isMaximized: false
    })
  })
}

// IPC handlers for window controls
ipcMain.on('window-control', (event, action) => {
  if (!win) return
  switch (action) {
    case 'minimize':
      win.minimize()
      break
    case 'maximize':
      if (win.isMaximized()) {
        win.unmaximize()
      } else {
        win.maximize()
      }
      break
    case 'close':
      win.close()
      break
    default:
      break
  }
})

// Add this after other ipcMain handlers
ipcMain.handle('open-external-url', (event, url) => {
  console.log('Opening external url...')
  shell.openExternal(url)
})

app.on('open-url', (event, url) => {
  event.preventDefault()
  console.log('App opened with URL:', url)
  if (url.startsWith('farmcontrol://app')) {
    // Extract the path/query after 'farmcontrol://app'
    const redirectPath = url.replace('farmcontrol://app', '') || '/'
    if (win && win.webContents) {
      win.webContents.send('navigate', redirectPath)
    }
  }
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit()
})

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) createWindow()
})

const env = (process.env.NODE_ENV || 'development').trim()
console.log(env)
if (env == 'development') {
  console.log('Starting development auth web server...')
  import('express').then(({ default: express }) => {
    const app = express()
    const port = 3500

    app.use((req, res) => {
      const redirectPath = req.originalUrl
      res.send(
        `Open Farmcontrol to continue... (Redirect path: ${redirectPath})`
      )
      if (win && win.webContents) {
        win.webContents.send('navigate', redirectPath)
        win.show()
        win.focus()
      }
    })

    app.listen(port, () => {
      console.log(`Dev auth server running on http://localhost:${port}`)
    })
  })
} else {
  console.log('Will use url scheme instead of auth server.')
}
