var GamePanel = function(name, link, i){
    this.name = name;
    this.link = link;
    this.i = i;
    this.buttons = [
        {
            name: 'Continue', 
            suffix: '?continue'
        },
        {
            name: 'New Game',
            suffix: ''
        }
    ];

    if (!localStorage['autosave_' + this.link]){
        this.buttons = [this.buttons[1]];
    }
};

GamePanel.prototype.handleTouch = function(pos){
    var x = width * this.i / 3  - 75;
    _.each(this.buttons, (button, j) => {
        var y = j * 50 + 475;
        var buttonRect = [x, y, 150, 25];
        if (containsPos(buttonRect, pos)){
            window.location = this.link + '.html' + button.suffix;
        }
    });
};

GamePanel.prototype.draw = function(){
    var x = width * this.i / 3  - 75;
    var panelRect = [x - 50, 260, 250, 315];
    ctx.fillStyle = '#aaaaaa';
    ctxFillRect(panelRect);
    
    ctx.fillStyle = '#000000';
    ctx.strokeStyle = '#000000';
    ctx.font = getFont(30);
    
    var textWidth = ctx.measureText(this.name).width;
    ctx.fillText(this.name, x + 75 - textWidth / 2, 300);

    var img = getImage('SS_' + this.link);
    ctx.drawImage(img, x - 25, 325, 200, 125);
    
    ctx.font = getFont(20);
    _.each(this.buttons, (button, j) => {
        var y = j * 50 + 475;
        ctxRoundedRect([x , y, 150, 25]);
        textWidth = ctx.measureText(button.name).width;
        ctx.fillText(button.name, x + 75 - textWidth / 2, y + 20);
    });

    ctxRoundedRect(panelRect);
};

var GameSelectMenu = function(){
    
};

GameSelectMenu.prototype.initialize = function(){
    this.ynotLogo = getImage('ynotLogo');
    this.logoImage = getImage('logo1');
    this.panels = [
        new GamePanel('The Quiet Sleep', 'sleeper', 1),
        new GamePanel('Songcraft', 'songs', 2)
    ];
    this.background = new MapBackground('#b7b7b7');
    document.addEventListener('keydown', (e) => {
        this.handleKeyPress(e);
    });
    bindHandler.bindFunction(this.getTouchFunction());
};

GameSelectMenu.prototype.getTouchFunction = function(){
    return (e) => {
        e.preventDefault();
        var pos = getPos(e);
        _.each(this.panels, panel => panel.handleTouch(pos));
        if (containsPos([width - 150, height - 50, 120, 20], pos)){
            nw.App.quit();
        }
        return false;
    };
};

GameSelectMenu.prototype.handleKeyPress = function(e){
    var keyCode = e.keyCode;
    if (keyCode == 27){
        nw.App.quit();
    }
};

GameSelectMenu.prototype.draw = function(){
    ctx.fillStyle = '#aaaaaa';
    ctx.fillRect(0, 0, width, height);

    this.background.draw();
    
    ctx.drawImage(this.ynotLogo, 10, height - 50, 150, 25);
    ctx.drawImage(this.logoImage, width / 2 - 300, 50);

    ctx.font = getFont(17);
    ctx.fillStyle = '#000000';
    ctx.strokeStyle = '#000000';
    ctx.fillText('by Nikhil Murthy', width - 150, height - 100);
    ctxRoundedRect([width - 150, height - 80, 120, 20]);
    ctx.fillText('Full Credits', width - 125, height - 64);
    ctxRoundedRect([width - 150, height - 50, 120, 20]);
    ctx.fillText('Quit', width - 100, height - 34);
    
    ctx.fillStyle = '#000000';
    _.each(this.panels, (panel) => panel.draw());
};

GameSelectMenu.prototype.update = function(){
    return true;
};

var getGame = function(gameProperties){
    return new GameSelectMenu(gameProperties);
};

var GameProperties = function(){
    this.next = null;
};
