import { app, shell, BrowserWindow, ipcMain } from 'electron'
import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
const { loginUser, forgotPassword } = require('../../database/Models/Users')

let mainWindow
let splashWindow

function createSplashWindow() {
  // Create the splash screen window
  splashWindow = new BrowserWindow({
    width: 400, // Width of the splash screen
    height: 300, // Height of the splash screen
    frame: false, // Remove window frame for a cleaner look
    alwaysOnTop: true, // Keep the splash screen on top of the main window
    transparent: true, // Make the window background transparent (optional)
    resizable: false,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false
    }
  })

  // Load your splash screen HTML or content
  splashWindow.loadFile(join('src/renderer/splash.html')) // Adjust path to your splash HTML file
}

function createMainWindow() {
  // Create the main browser window
  mainWindow = new BrowserWindow({
    width: 1000,
    minWidth: 1000,
    height: 670,
    minHeight: 670,
    show: false,
    autoHideMenuBar: true,
    fullscreen: true, // This will make the window fullscreen when opened
    ...(process.platform === 'linux' ? { icon } : {}),
    webPreferences: {
      preload: join(__dirname, '../preload/index.js'),
      nodeIntegration: true,
      contextIsolation: true,
      sandbox: false
    }
  })

  mainWindow.on('ready-to-show', () => {
    // Show the main window after it is ready
    mainWindow.show()
    splashWindow.close() // Close the splash screen
  })

  mainWindow.webContents.setWindowOpenHandler((details) => {
    shell.openExternal(details.url)
    return { action: 'deny' }
  })

  // Load the remote URL for development or the local HTML file for production
  if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
    mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
  } else {
    mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
  }
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
  // Set app user model ID for Windows
  electronApp.setAppUserModelId('com.electron')

  // Create splash screen
  createSplashWindow()

  // Create main window
  createMainWindow()

  app.on('activate', function () {
    // On macOS, recreate a window in the app when the dock icon is clicked
    if (BrowserWindow.getAllWindows().length === 0) createMainWindow()
  })
})

// Quit when all windows are closed, except on macOS.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

// In this file you can include the rest of your app"s specific main process
// code. You can also put them in separate files and require them here.

// Event for handling login
ipcMain.handle('login', async (event, credentials) => {
  const { username, password } = credentials

  const users = await loginUser(username, password)

  if (users) {
    return { success: true, user: users }
  } else {
    return { success: false }
  }
})

// event for handlign forgot password
ipcMain.handle('forgotPassword', async (event, credentials) => {
  const { email, password, passwordConfirmation } = credentials

  const updated = await forgotPassword(email, password, passwordConfirmation)

  if (updated) {
    return updated
  }
})
