var build;
$(document).ready(function() {
    $('#nexus').click(function() {
        build.add_unit('a');
    });
    $('#pylon').click(function() {
        build.add_unit('b');
    });
    $('#gateway').click(function() {
        build.add_unit('c');
    });
    $('#probe').click(function() {
        build.add_unit('A');
    });
    $('#zealot').click(function() {
        build.add_unit('B');
    });
    $('#debug').click(function() {
        console.log(build);
    });
});

(function($) {
    
    $.fn.sc2build = function(options) {
        var opts = $.extend({}, $.fn.sc2build.defaults, options);
        build = new sc2build(opts.build[0]);
    }
    
    $.fn.sc2build.defaults = {
        build: 'P'
    }
    
    function sc2build(race) {
        switch(race) {
            case 'P':
                this.race = new protoss();
                this.add_unit('a');
                this.add_unit('A');
                this.add_unit('A');
                this.add_unit('A');
                this.add_unit('A');
                this.add_unit('A');
                this.add_unit('A');
        }
    }
    
    sc2build.prototype.add_unit = function(char, time) {
        var new_unit = this.race.get_unit(char);
        if(typeof(time) == 'undefined') {
            for(var t in this.units) {
                time = t;
            }
            time = (time > 0 ? time : 1);
            while(this.unavailable(new_unit, time)) {
                time++;
            }
        }
        
        if(typeof(this.units[time]) == 'undefined') {
            this.units[time] = [];
        }
        this.units[time].push(new_unit);
        this.render_build();
    }
    
    sc2build.prototype.render_build = function() {
        $('#sc2build').find('tbody').remove();
        $('#sc2build').append('<tbody></tbody>');
        for(var time in this.units) {
            for(var i in this.units[time]) {
                unit = this.units[time][i];
                $('#sc2build').find('tbody').append(
                    $('<tr>'
                        +'<td>'+(time > 0 ? time : 0)+'</td>'
                        +'<td>'+this.get_supply_used((time))+' / '+this.get_supply_total((time))+'</td>'
                        +'<td>'+this.get_minerals((time))+'</td>'
                        +'<td>'+this.get_income((time))+'</td'
                        +'<td>'+this.get_army(time)+'</td>'
                        +'<td>'+this.get_buildings(time)+'</td>'
                        +'<td>'+unit.name+'</td>'
                        +'<td></td>'
                    +'</tr>')
                )
            }
        }
    }
    
    sc2build.prototype.unavailable = function(unit, time) {
        return this.get_minerals(time) < unit.mineral_cost 
            || (this.get_supply_total(time) - this.get_supply_used(time)) < unit.supply_cost;
    }
    
    sc2build.prototype.get_minerals = function(view_time) {
        var minerals = 750;
        for(var time in this.units) {
            if(time > view_time) break;
            for(var i in this.units[time]) {
                unit = this.units[time][i];
                if((unit.time + Number(time)) < view_time) {
                    minerals += Number(unit.mineral_gen) * ((view_time) - (Number(time) + Number(unit.time))) / 60;
                }
                if(time <= view_time) {
                    minerals -= unit.mineral_cost;
                }
            }
        }
        
        return Math.floor(minerals);
    }
    
    sc2build.prototype.get_income = function(view_time) {
        var minerals = 0;
        
        for(var time in this.units) {
            if(time > view_time) break;
            for(var i in this.units[time]) {
                unit = this.units[time][i];
                if((unit.time + Number(time)) < view_time) {
                    minerals += unit.mineral_gen;
                }
            }
        }
        
        return Math.floor(minerals);
    }
    
    sc2build.prototype.get_army = function(view_time) {
        var army = [];
        
        for(var time in this.units) {
            if(time > view_time) break;
            for(var i in this.units[time]) {
                unit = this.units[time][i];
                if((unit.time + Number(time)) < view_time && unit.type == 'army') {
                    if(typeof(army[unit.name]) == 'undefined') {
                        army[unit.name] = 0;
                    }
                    army[unit.name] += 1;
                }
            }
        }
        
        var string = '';
        
        for(var i in army) {
            string += army[i] + ' ' + i + ', ';
        }
        
        return string;
    }
    
    sc2build.prototype.get_buildings = function(view_time) {
        var buildings = [];
        
        for(var time in this.units) {
            if(time > view_time) break;
            for(var i in this.units[time]) {
                unit = this.units[time][i];
                if((unit.time + Number(time)) < view_time && unit.type == 'building') {
                    if(typeof(buildings[unit.name]) == 'undefined') {
                        buildings[unit.name] = 0;
                    }
                    buildings[unit.name] += 1;
                }
            }
        }
        
        var string = '';
        
        for(var i in buildings) {
            string += buildings[i] + ' ' + i + ', ';
        }
        
        return string;
    }
    
    sc2build.prototype.get_supply_total = function(view_time) {
        var supply = 0;
        for(var time in this.units) {
            if(time > view_time) break;
            for(var i in this.units[time]) {
                unit = this.units[time][i];
                if((unit.time + Number(time)) <= view_time) {
                    supply += unit.supply_gen;
                }
            }
        }
        
        return supply;
    }
    
    sc2build.prototype.get_supply_used = function(view_time) {
        var supply = 0;
        for(var time in this.units) {
            if(time > view_time) break;
            for(var i in this.units[time]) {
                unit = this.units[time][i];
                if(time <= view_time) {
                    supply += unit.supply_cost;
                }
            }
        }
        
        return supply;
    }
    
    sc2build.prototype.units = [];
    
    function protoss() {
    
    }
    
    protoss.prototype.units = {
        a: {
            name: 'Nexus',
            mineral_cost: 400,
            supply_cost : 0,
            time: 100,
            supply_gen : 10,
            mineral_gen : 0,
            type: 'building'
        },
        b: {
            name: 'Pylon',
            mineral_cost: 100,
            supply_cost : 0,
            time: 35,
            supply_gen : 8,
            mineral_gen: 0,
            type: 'building'
        },
        c: {
            name: 'Gateway',
            mineral_cost: 150,
            supply_cost : 0,
            time: 65,
            supply_gen : 0,
            mineral_gen: 0,
            type: 'building'
        },
        A: {
            name: 'Probe',
            mineral_cost: 50,
            supply_cost: 1,
            time: 17,
            mineral_gen: 50,
            supply_gen : 0,
            type: 'building'
        },
        B: {
            name: 'Zealot',
            mineral_cost: 100,
            supply_cost: 2,
            time: 33,
            supply_gen : 0,
            mineral_gen: 0,
            type: 'army'
        },
    };
    
    protoss.prototype.get_unit = function(id) {
        return eval('this.units.'+id);
    }
})(jQuery);
