'use strict';

var fs = require('fs');
var ipc = require('ipc');
var Mustache = require('mustache');

import { mainView } from './view/main.js';
import * as Schedule from './model/schedule.js';
import { setSchedule, readForm, setForm } from './controller/html.js';

function injectContent() {
    return new Promise(function(resolve, reject) {
        fs.readFile(__dirname + '/app.mst.html', 'utf-8', function(err, data) {
            if (err) {
                reject(err);
            }
            let view = mainView(Schedule.ids);
            let rendered = Mustache.render(data, view);
            document.getElementById('container').innerHTML = rendered;
            resolve();
        });
    });
}

window.onbeforeunload = function(event) {
    ipc.send('save-schedule', readForm());
}

injectContent()
.then(function() {
    ipc.on('schedule', function(schedule) {
        console.log('Recieved schedule from main process', schedule);

        if (schedule === null) {
            schedule = Schedule.defaults();
        } else {
            // Config doesn't save all attributes
            let d = Schedule.defaults();
            for (let a in schedule) { d[a] = schedule[a]; }
            schedule = d;
        }
        setSchedule(Schedule.url(schedule));
        setForm(schedule);
    });

    ipc.send('load-schedule');
})
.then(function() {
    document.getElementById('form-button').addEventListener('click', function() {
        setSchedule(Schedule.url(readForm()));
    });
})
.catch(function(err) {
    console.log(err);
});

@@ -707,96 +707,8 @@
 %0A%7D%0A%0A
-window.onbeforeunload = function(event) %7B%0A    ipc.send('save-schedule', readForm());%0A%7D%0A%0A
 inje
@@ -1264,24 +1264,131 @@
 unction() %7B%0A
+    window.addEventListener('unload', function() %7B%0A        ipc.send('save-schedule', readForm());%0A    %7D);%0A%0A
     document

Use addEventListener instead of window.on