if (STEAM){
    var greenworks = require('./greenworks');
    try{
        greenworks.init();
    }
    catch (e) {
        STEAM = false;
    }
}

var renderCenteredText = function(txt, height){
    var startX = 90;
    var backWidth = 820;
    var centerX = startX + backWidth / 2;
    var textWidth = ctx.measureText(txt).width;
    ctx.fillText(txt, centerX - (textWidth / 2), height);
};

var SelectMenu = function(){
    this.stillRunning = true;
    
    this.startedStories = [];

    var startedStoriesKey = 'startedStories';
    var saveInfo = false;

    if (STEAM){
        if (greenworks.isCloudEnabledForUser() && greenworks.isCloudEnabled()){
            greenworks.readTextFromFile(startedStoriesKey,
                                        function(fileContent){
                                            saveInfo = fileContent;
                                        },
                                        function(){
                                        });
        }
    }

    if (localStorage[startedStoriesKey]){
        saveInfo = localStorage[startedStoriesKey];
    }

    if (saveInfo){
        this.startedStories = JSON.parse(saveInfo);
    }

    bindHandler.bindFunction(this.getTouchFunction());
};

SelectMenu.prototype.draw = function(){
    this.buttonRects = [];
    ctx.drawImage(getImage('selectBack'), 0, 0);
    ctx.drawImage(getImage('uiBack'), 90, 30, 850, 630);
    ctx.fillStyle = '#000000';
    ctx.font = getFont(60);
    ctx.strokeStyle = '#000000';
    renderCenteredText('Choose Your Story', 100);
    
    var stories = [
        {
            title: 'The Quiet Sleep',
            url: 'sleeper.html',
            desc: ['You\'ve just moved to a new town and', 'things aren\'t as they seem.', '', 'Not for the town and not for you.']
        }, 
        {
            title: 'Songcraft',
            url: 'songs.html',
            desc: ['It\'s one thing to quit your job to follow', 'your passion.', '', 'It\'s another to actually make something.']
        },
        {
            title: 'Evocations',
            url: 'flip.html',
            desc: ['Your new relationship has you excited,', 'but you seem to be the only one who', 'feels anything here.']
        }
    ];
    var urls = ['sleeper.html', 'songs.html'];
    

    _.each(stories, (story, i) => {
        ctx.font = getFont(25);
        var storyRect = [120 + 275 * i, 200, 250, 350];
        var textWidth = ctx.measureText(story.title).width;
        ctx.fillText(story.title, storyRect[0] + (storyRect[2] - textWidth) / 2, storyRect[1] + 50);
        ctx.fillText('' + (i + 1) + '.', storyRect[0] + 5, storyRect[1] + 50);
        ctx.font = '13px OpenSansItalic';
        _.each(story.desc, (line, i) => {
            ctx.fillText(line, storyRect[0] + 10, storyRect[1] + 100 + 20 * i);
        });
        ctx.font = getFont(18);
        var buttonWidth = 150;
        var x = storyRect[0] + (storyRect[2] - buttonWidth) / 2;
        var y = storyRect[1] + 250;
        if (_.contains(this.startedStories, story.title)){
            var continueRect = [x, y, buttonWidth, 30];
            this.buttonRects.push([continueRect, story.url + '?continue', story.title]);
            ctxRoundedRect(continueRect);
            ctx.fillText('Load', continueRect[0] + 60, continueRect[1] + continueRect[3] - 10);
            y += 50;
        }
        var newRect = [x, y, buttonWidth, 30];
        ctxRoundedRect(newRect);
        ctx.fillText('New', newRect[0] + 60, newRect[1] + newRect[3] - 10);
        this.buttonRects.push([newRect, story.url, story.title]);
        ctxRoundedRect(storyRect);
    });
    
    var creditRect = [770, 630, 100, 25];
    ctxRoundedRect(creditRect);
    ctx.fillText('Credits', creditRect[0] + 10, creditRect[1] + creditRect[3] - 5);
    this.buttonRects.push([creditRect, 'ATTRIBUTION.html', 'credits']);
};

SelectMenu.prototype.update = function(){
    return this.stillRunning;
};

SelectMenu.prototype.getTouchFunction = function(){
    return (e) =>{
        e.preventDefault();
        var pos = getPos(e);
        _.each(this.buttonRects, (buttonRect) => {
            if (containsPos(buttonRect[0], pos)){
                if (!_.contains(this.startedStories, buttonRect[2])){
                    this.startedStories.push(buttonRect[2]);
                    localStorage.startedStories = JSON.stringify(this.startedStories);
                }
                
                window.location = buttonRect[1];
            }
        });
    };
};

SelectMenu.prototype.initialize = trueFunction;

var GameProperties = function(){};

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