import { RendererApi } from '../common/rendererApi';
import { BlocklyApp } from './blockly-app';
import * as impl from './impl';

// DOMの取得
function throwError(msg: string): never {
  throw new Error(msg);
}
const consoleDom = document.getElementById('console') ?? throwError('console Not Found');
function escapeHTML(text: string): string {
  const map:{[key: string]: string} = {
    '<': '&lt;',
    '>': '&gt;',
    '&': '&amp;',
  };
  return text.replace(/[<>&]/g, x => map[x]);}
// APIの実装
export const api: RendererApi = {
  cmd(c: string, args?: string[]): void {
    let text = c;
    if (args) {
      for (const a of args) {
        text += ' ' + a;
      }
    }
    consoleDom.innerHTML += `<p class='command'>${escapeHTML(text)}</p>`;
  },

  writeLine(text: string): void {
    consoleDom.innerHTML += `<p class='stdout'>${escapeHTML(text)}</p>`;
  },
  writeLineErr(text: string): void {
    consoleDom.innerHTML += `<p class='stderr'>${escapeHTML(text)}</p>`;
  },
  err(text: string): void {
    consoleDom.innerHTML += `<p class='error'>${escapeHTML(text)}</p>`;
  },
  log(text: string): void {
    consoleDom.innerHTML += `<p class='log'>${escapeHTML(text)}</p>`;
  }
};
app.register(api);

const blocklyApp = new BlocklyApp('blockly-area', require('./toolbox.xml?raw'));

// イベントハンドラ
const runDom = <HTMLInputElement>document.getElementById('run') ?? throwError('Run Button Not Found');
runDom.addEventListener('click', async () => {
  if (runDom.disabled) return;
  runDom.disabled = true;
  try {
    await impl.runAction(api, app.main, app.os);
  } finally {
    runDom.disabled = false;
  }
});

let defaultFilePath = 'Hello.xml';
document.getElementById('save')?.addEventListener('click', async () => {
  const savePath = await app.main.showSaveFileDialog({
    defaultPath: defaultFilePath,
    filters: [{ name: 'XML', extensions: ['xml'] }],
  });
  if (!savePath) return;
  defaultFilePath = savePath;
  const text = blocklyApp.getWorkspaceText();
  await app.main.saveTextFile(savePath, text);
});

document.getElementById('load')?.addEventListener('click', async () => {
  const loadPath = await app.main.showOpenFileDialog({
    defaultPath: defaultFilePath,
    filters: [{ name: 'XML', extensions: ['xml'] }],
  });
  if (!loadPath) return;
  const data = await app.main.readTextFile(loadPath);
  if (data === null) return;
  defaultFilePath = loadPath;
  blocklyApp.setWorkspaceText(data);
});
