'use strict';

function NavalMap(canvasId, imageMapUrl, imageCompassUrl, config) {
    this.canvas = document.getElementById(canvasId);
    this.imageMap = new Image();
    this.imageCompass = new Image();
    this.config = config;
    this.itemsLoaded = false;
    this.nationsLoaded = false;
    this.shopsLoaded = false;
    this.portsLoaded = false;
    this.imageMapLoaded = false;
    this.imageCompassLoaded = false;
    this.init(imageMapUrl, imageCompassUrl);
}

NavalMap.prototype.init = function init(imageMapUrl, imageCompassUrl) {
    var self = this;
    this.loadEverything(imageMapUrl, imageCompassUrl, function () {
        var stage = new createjs.Stage(self.canvas);
        createjs.Touch.enable(stage);
        stage.enableMouseOver(5);
        stage.tickEnabled = false;
        //createjs.Ticker.framerate = 60;
        createjs.Ticker.timingMode = createjs.Ticker.RAF;
        self.map = new Map(self.canvas, stage, self.imageMap, self.imageCompass, self.config);
    });
};

NavalMap.prototype.loadImageMap = function loadImageMap(url, cb) {
    this.imageMap.src = url;
    var self = this;
    this.imageMap.onload = function () {
        self.imageMapLoaded = true;
        if (self.checkEverethingIsLoaded()) {
            if(cb) {
                cb();
            }
        }
    };
};

NavalMap.prototype.loadImageCompass = function loadImageCompass(url, cb) {
    this.imageCompass.src = url;
    var self = this;
    this.imageCompass.onload = function () {
        self.imageCompassLoaded = true;
        if (self.checkEverethingIsLoaded()) {
            if(cb) {
                cb();
            }
        }
    };
};

NavalMap.prototype.checkEverethingIsLoaded = function () {
    return this.itemsLoaded && this.nationsLoaded && this.shopsLoaded && this.portsLoaded && this.imageMapLoaded && this.imageCompassLoaded;
};

NavalMap.prototype.loadItems = function(cb) {
    var self = this;
    $.getScript("items.php").done(function(){
        self.itemsLoaded = true;
        if (self.checkEverethingIsLoaded()) {
            if(cb) {
                cb();
            }
        }
    });
};

NavalMap.prototype.loadNations = function(cb) {
    var self = this;
    $.getScript("nations.php").done(function(){
        self.nationsLoaded = true;
        if (self.checkEverethingIsLoaded()) {
            if(cb) {
                cb();
            }
        }
    });
};

NavalMap.prototype.loadShops = function(cb) {
    var self = this;
    $.getScript("shops.php").done(function(){
        self.shopsLoaded = true;
        if (self.checkEverethingIsLoaded()) {
            if(cb) {
                cb();
            }
        }
    });
};

NavalMap.prototype.loadPorts = function(cb) {
    var self = this;
    $.getScript("ports.php").done(function(){
        self.portsLoaded = true;
        if (self.checkEverethingIsLoaded()) {
            if(cb) {
                cb();
            }
        }
    });
};


NavalMap.prototype.loadEverything = function loadEverything(urlMap, urlCompass, cb) {
    this.loadImageMap(urlMap, cb);
    this.loadImageCompass(urlCompass, cb);
    this.loadShops(cb);
    this.loadItems(cb);
    this.loadPorts(cb);
    this.loadNations(cb);
};

function Map(canvas, stage, imageMap, imageCompass, config) {
    this.canvas = canvas;
    this.config = config;
    this.stage = stage;
    this.globalContainer = new createjs.Container();
    this.mapContainer = new createjs.Container();
    this.unmodifiedMapContainer = {};
    this.compass = new Compass(imageCompass, config);
    this.update = false;
    this.alreadyZooming = false;
    this.gpsCursor = undefined;
    this.statistics = {};
    this.fpsLabel = new createjs.Text("-- fps", "bold 18px Arial", "black");
    this.init(imageMap);
}

Map.prototype.init = function (imageMap) {
    this.stage.addChild(this.globalContainer);
    this.stage.addChild(this.fpsLabel);
    this.fpsLabel.x = 240;
    this.fpsLabel.y = 10;
    this.globalContainer.addChild(this.mapContainer);
    this.globalContainer.addChild(this.compass);
    this.mapContainer.addChild(new createjs.Bitmap(imageMap));
    this.mapContainer.hasBeenDblClicked = false;
    this.initContainerMap();
    this.resizeCanvas(this);
    this.createAllEvents();
    var self = this;
    Nations.Nations.forEach(function(nation) {
        self.statistics[nation.Name] = 0;
    });
    this.addPorts();
    this.stage.update();
    self.tickEvent();
    setTimeout(function() {
        $("#progress-bar-load").hide();
        $(".top-nav").removeClass('hide');
        $("#port-information").removeClass('hide');
        $("#how-to-use").removeClass('hide');
    },600);
    //this.update = true;
};

Map.prototype.initContainerMap = function () {
    this.setScale(this.config.map.scale);
    this.centerTo(this.config.map.x, this.config.map.y);
    var self = this;
    this.mapContainer.addLine = function (x, y) {
        var shape = new createjs.Shape();
        self.mapContainer.lineIndex = self.mapContainer.children.length;
        self.mapContainer.addChild(shape);
        shape.graphics.setStrokeStyle(3, "round").beginStroke('#3d3d3d').moveTo((self.compass.x - self.mapContainer.x) / self.mapContainer.scale, (self.compass.y - self.mapContainer.y) / self.mapContainer.scale).lineTo(x, y);
    };

    this.mapContainer.removeLine = function () {
        if (self.mapContainer.lineIndex) {
            self.mapContainer.removeChildAt(self.mapContainer.lineIndex);
        }
    };
    //this.globalContainer.cursor = "default";
};

Map.prototype.populateStatistics = function () {
    var stats = $("#ports-number");
    $.each(this.statistics, function(name, number) {
        stats.append('<strong>' + name + ' : </strong>' + number + '<br>');
    })
};

Map.prototype.setScale = function (scale) {
    this.mapContainer.scale = this.mapContainer.scaleX = this.mapContainer.scaleY = scale;
};

Map.prototype.zoom = function (increment) {
    this.setScale(this.mapContainer.scale + increment);
};

Map.prototype.addPorts = function () {
    var self = this;
    setTimeout(function() {
        Ports.forEach(function (port, idx) {
            var circle = new createjs.Shape();
            circle.graphics.beginFill(self.config.color[port.Nation]).drawCircle(0, 0, 5);
            circle.x = (port.sourcePosition.x + self.config.portsOffset.x) * self.config.portsOffset.ratio;
            circle.y = (port.sourcePosition.y + self.config.portsOffset.y) * self.config.portsOffset.ratio;
            circle.cursor = "pointer";
            circle.idx = idx;
            self.statistics[getNationFromIdx(port.Nation).Name] += 1;
            circle.on("click", function () {
                var currPort = Ports[this.idx];
                $('#port-title').text(currPort.Name);
                $('#nation').text(getNationFromIdx(currPort.Nation).Name);
                var timer = currPort.ConquestFlagTimeSlot + 'h - ' + (currPort.ConquestFlagTimeSlot + 2) + "h";
                $('#timer').text(currPort.ConquestFlagTimeSlot == -1?'No Timer':timer);
                $('#capital').text(currPort.Capital?'yes':'no');
                $('#regional').text(currPort.Regional?'yes':'no');
                $('#shallow').text(currPort.Depth == 1?'yes':'no');
                $('#capturer').text(currPort.Capturer);
                var produces = Shops[this.idx].ResourcesProduced;
                var consumes = Shops[this.idx].ResourcesConsumed;
                $('#produces-list').html('');
                $('#consumes-list').html('');
                produces.forEach(function (produce) {
                    var item = getItemTemplateFromId(produce.Key);
                    $('#produces-list').append('<li class="list-group-item">'+item.Name+' : '+ produce.Value+'</li>');
                });
                consumes.forEach(function (consume) {
                    var item = getItemTemplateFromId(consume.Key);
                    $('#consumes-list').append('<li class="list-group-item">'+item.Name+' : '+ consume.Value+'</li>');
                });
            });
            circle.cache(-5, -5, 10, 10);
            self.mapContainer.addChild(circle);
        });
        self.update = true;
        self.stage.tick();
        self.populateStatistics();
    },200);
};

Map.prototype.keepMapUnderPos = function (x, y) {
    var mapPos = this.getMapPosFromWindowPos(x, y);
    this.globalContainer.x = x - this.mapContainer.scale * mapPos.x;
    this.globalContainer.y = y - this.mapContainer.scale * mapPos.y;
};

Map.prototype.keepCompassUnderCurrentPos = function () {
    var mapPos = this.getMapPosFromWindowPos(this.compass.x + this.unmodifiedMapContainer.x, this.compass.y + this.unmodifiedMapContainer.y);
    this.compass.x = mapPos.x * this.mapContainer.scale;
    this.compass.y = mapPos.y * this.mapContainer.scale;
};

Map.prototype.centerTo = function (x, y) {
    this.globalContainer.x = this.canvas.width / 2 - this.mapContainer.scale * x;
    this.globalContainer.y =