const { contextBridge, ipcRenderer } = require('electron');
const fs = require('fs');
const path = require('path');
const os = require('os');

contextBridge.exposeInMainWorld('electronAPI', {
  minimize: () => ipcRenderer.send('window-minimize'),
  maximize: () => ipcRenderer.send('window-maximize'),
  close: () => ipcRenderer.send('window-close'),
  openChildWindow: (data) => ipcRenderer.send('open-child-window', data),

  onSetUrl: (callback) => ipcRenderer.on('set-url', (event, data) => callback(data)),

  // ✅ message.txt の URL を返すだけに変更
  getMessageUrl: () => {
    return 'http://busyuka.mydns.jp:6500/FFXI/launcher/message.json';
  },

  getUserDataPath: () => {
    return path.join(os.homedir(), 'AppData', 'Roaming', 'ぶぅたんのねごと');
  },

  saveFile: (filePath, content) => {
    try {
      const dir = path.dirname(filePath);
      if (!fs.existsSync(dir)) {
        fs.mkdirSync(dir, { recursive: true });
      }
      fs.writeFileSync(filePath, content, 'utf8');
    } catch (e) {
      console.error('[saveFile] ファイル保存エラー:', e);
    }
  },

  readJsonFile: (filePath) => {
    try {
      return JSON.parse(fs.readFileSync(filePath, 'utf8'));
    } catch {
      return null;
    }
  }
});
