const { app, BrowserWindow, ipcMain } = require('electron'); const path = require('path'); const sqlite3 = require('sqlite3').verbose(); app.commandLine.appendSwitch('remote-debugging-port', '9222'); let mainWindow; let db; function createWindow() { mainWindow = new BrowserWindow({ width: 1200, height: 800, webPreferences: { nodeIntegration: true, contextIsolation: false } }); mainWindow.loadFile('index.html'); db = new sqlite3.Database('../pokemon_forms.db', (err) => { if (err) { console.error('Database opening error: ', err); } }); } app.whenReady().then(createWindow); app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); } }); app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow(); } }); ipcMain.on('load-forms-data', (event) => { db.all(` SELECT pf.PFIC, pf.name, pf.form_name, pf.national_dex, pf.generation, ps.storable_in_home FROM pokemon_forms pf LEFT JOIN pokemon_storage ps ON pf.PFIC = ps.PFIC `, (err, rows) => { if (err) { console.error('Error fetching data: ', err); event.reply('forms-data-response', { error: err.message }); } else { event.reply('forms-data-response', { data: rows }); } }); }); ipcMain.on('search-evolution', (event, searchTerm) => { db.all(` SELECT DISTINCT name, PFIC FROM pokemon_forms WHERE LOWER(name) LIKE ? `, [`%${searchTerm.toLowerCase()}%`], (err, rows) => { if (err) { console.error('Error searching evolution: ', err); event.reply('evolution-search-response', { error: err.message }); } else { event.reply('evolution-search-response', { data: rows }); } }); }); ipcMain.on('get-evolution-chain', (event, pfic) => { function getEvolutions(currentPfic) { return new Promise((resolve, reject) => { db.all(` SELECT ec.to_pfic as pfic, pf.name, pf.form_name, ec.method, ec.from_pfic FROM evolution_chains ec JOIN pokemon_forms pf ON ec.to_pfic = pf.PFIC WHERE ec.from_pfic = ? `, [currentPfic], (err, rows) => { if (err) { reject(err); } else { resolve(rows); } }); }); } async function buildChain(pfic) { const pokemon = await new Promise((resolve, reject) => { db.get(`SELECT PFIC as pfic, name, form_name FROM pokemon_forms WHERE PFIC = ?`, [pfic], (err, row) => { if (err) reject(err); else resolve(row); }); }); if (!pokemon) return null; const evolutions = await getEvolutions(pfic); pokemon.evolutions = await Promise.all(evolutions.map(evo => buildChain(evo.pfic))); return pokemon; } buildChain(pfic) .then(chain => { event.reply('evolution-chain-response', { data: chain }); }) .catch(err => { console.error('Error building evolution chain:', err); event.reply('evolution-chain-response', { error: err.message }); }); }); ipcMain.on('edit-pokemon', (event, data) => { // Implement the logic to update the Pokémon in the database console.log('Editing Pokémon:', data); // Update the database and send a response back to the renderer }); ipcMain.on('remove-pokemon', (event, data) => { // Implement the logic to remove the Pokémon from the evolution chain in the database console.log('Removing Pokémon:', data); // Update the database and send a response back to the renderer }); ipcMain.on('add-stage', (event, data) => { // Implement the logic to add a new stage to the evolution chain in the database console.log('Adding new stage:', data); // Update the database and send a response back to the renderer }); ipcMain.on('save-evolution-changes', (event, data) => { // Implement the logic to save all changes to the evolution chain in the database console.log('Saving evolution changes:', data); // Here you would update the database with the new evolution chain data // This is a placeholder implementation setTimeout(() => { event.reply('save-evolution-changes-response', { success: true }); }, 1000); // If there's an error, you would reply with: // event.reply('save-evolution-changes-response', { error: 'Error message' }); }); // Add more IPC handlers for other database operations // Add this IPC handler ipcMain.on('load-all-pokemon', (event) => { db.all(` SELECT PFIC, name, form_name FROM pokemon_forms ORDER BY CAST(SUBSTR(PFIC, 1, 4) AS INTEGER), -- National Dex number CAST(SUBSTR(PFIC, 6, 2) AS INTEGER), -- Region code CAST(SUBSTR(PFIC, 9, 3) AS INTEGER), -- Form index CAST(SUBSTR(PFIC, 13, 1) AS INTEGER) -- Gender code `, (err, rows) => { if (err) { console.error('Error fetching all Pokémon:', err); event.reply('all-pokemon-response', { error: err.message }); } else { event.reply('all-pokemon-response', { data: rows }); } }); }); ipcMain.on('get-pokemon-details', (event, pfic) => { db.get(` SELECT pf.PFIC, pf.name, pf.form_name, pf.national_dex, pf.generation, ps.storable_in_home FROM pokemon_forms pf LEFT JOIN pokemon_storage ps ON pf.PFIC = ps.PFIC WHERE pf.PFIC = ? `, [pfic], (err, row) => { if (err) { console.error('Error fetching Pokémon details:', err); event.reply('pokemon-details-response', { error: err.message }); } else { event.reply('pokemon-details-response', { data: row }); } }); }); ipcMain.on('update-storable-in-home', (event, data) => { db.run(` UPDATE pokemon_storage SET storable_in_home = ? WHERE PFIC = ? `, [data.storable ? 1 : 0, data.pfic], (err) => { if (err) { console.error('Error updating storable in home:', err); event.reply('update-storable-in-home-response', { error: err.message }); } else { event.reply('update-storable-in-home-response', { success: true }); } }); });