var ObjectWithListeners = require('./ObjectWithListeners');

/**
 * Class Configuration is responsible for load a configuration from a text file
 * conected to current project (in one project it is possible to show many
 * maps).
 */
function Configuration() {
  // call super constructor
  ObjectWithListeners.call(this);

  // set default values
  this.TILE_SIZE = 256;
  this.PICTURE_SIZE = 256;
  this.MAX_ZOOM = 4;
  this.MIN_ZOOM = 2;
  this.IMG_DIR = "images/";
  this.MAP_NAME = "unknown map";
  this.MAPS = [];
  this.SUBMODELS = [];

  this.speciesAddr = "species";
  this.miriamAddr = "miriam";
  this.feedbackAddr = "feedback";

  this.registerListenerType("onreload");
};

// this class inherits from ObjectWithListeners class where generic methods for
// listeners are set
Configuration.prototype = Object.create(ObjectWithListeners.prototype);
Configuration.prototype.constructor = Configuration;

/**
 * Load configuration from file.
 */
Configuration.prototype.loadFromFile = function(fileName, callBackFunction) {
  var self = this;

  var txtFile = new XMLHttpRequest();
  txtFile.open("GET", fileName, true);
  txtFile.onreadystatechange = function() {
    // Makes sure the document is ready to parse.
    if (txtFile.readyState === 4) {
      // Makes sure it's found the file.
      if (txtFile.status === 200) {
        allText = txtFile.responseText;
        // Will separate each line into an array
        textLines = txtFile.responseText.split("\n");

        // name of the map is the filename wthout path and extension
        self.MAP_NAME = textLines[0].replace(/^.*[\\\/]/, '').split(".")[0];
        self.TILE_SIZE = parseInt(textLines[3]);
        self.PICTURE_SIZE = parseInt(textLines[4]);
        self.MIN_ZOOM = 2;
        self.MAX_ZOOM = self.MIN_ZOOM + parseInt(textLines[2]);
        // read information about all maps connected with current project
        mapCounter = parseInt(textLines[5]);
        for (var i = 0; i < mapCounter; i++) {
          mapDesc = {
            id : textLines[6 + i * 2],
            title : textLines[6 + i * 2 + 1]
          };
          self.MAPS.push(mapDesc);
        }
        callBackFunction();
        self.callListeners("onreload");
      }
    }
  };
  txtFile.send(null);
};

/**
 * Updates configuration data from object passed from server side.
 * 
 * @param modelView
 *            object retrieved from server side
 */
Configuration.prototype.loadFromModelView = function(modelView) {
  this.ID_MODEL = modelView.idObject;
  this.TILE_SIZE = modelView.tileSize;
  this.PICTURE_SIZE = modelView.pictureSize;
  this.MAX_ZOOM = modelView.maxZoom;
  this.MIN_ZOOM = modelView.minZoom;
  this.MAP_NAME = modelView.version;
  this.CENTER_LAT = modelView.centerLatLng.lat;
  this.CENTER_LNG = modelView.centerLatLng.lng;

  this.MAPS = modelView.layouts;
  this.MAPS = this.MAPS.concat(modelView.customLayouts);

  this.SUBMODELS = [];
  if (modelView.submodels != null) {
    for (var i = 0; i < modelView.submodels.length; i++) {
      var conf = new Configuration();
      conf.loadFromModelView(modelView.submodels[i]);
      this.SUBMODELS.push(conf);
    }
  }

  this.TOP_OVERVIEW_IMAGE = modelView.topOverviewImage;
  this.OVERVIEW_IMAGES = modelView.overviewImageViews;

  this.topLeftLatLng = modelView.topLeftLatLng;
  this.bottomRightLatLng = modelView.bottomRightLatLng;
  this.fitMapBounds = modelView.fitMapBounds;

  this.callListeners("onreload");
};

Configuration.prototype.getId = function(modelView) {
  return this.ID_MODEL;
};

Configuration.prototype.addLayout = function(layout) {
	this.MAPS.push(layout);
}


module.exports = Configuration;