"use strict";
this.name = "sothis-station-market";

//put the original defs here. no def is handled later.
this.$originalDefs = {
	"food" : [0, 0, 25, -1, -1, 250, 5, 5, 0],
	"textiles" : [0, 0, 18, -1, 0, 250, 5, 10, 0],
	"radioactives" : [0, 0, 53, -2, -3, 0, 7, 15, 0],
	"slaves" : [0, 0, 20, -2, -1, 5, 0, 3, 0],
	"liquor_wines" : [0, 0, 130, -1, -1, 250, 15, 5, 0],
	"luxuries" : [0, 0, 190, 8, -1, 250, 9, 21, 0],
	"narcotics" : [0, 0, 36, -5, -9, 250, 5, 5, 0],
	"computers" : [0, 0, 150, 14, -1, 250, 3, 21, 0],
	"machinery" : [0, 0, 120, 6, 1, 250, 31, 10, 0],
	"alloys" : [0, 0, 88, 3, 1, 250, 21, 10, 0],
	"firearms" : [0, 0, 100, 13, 0, 0, 63, 0, 0],
	"furs" : [0, 0, 170, -9, -1, 250, 63, 21, 0],
	"minerals" : [0, 0, 16, -1, -2, 15, 3, 31, 0],
	"gold" : [0, 0, 100, 0, 0, 0, 4, 0, 1],
	"platinum" : [0, 0, 181, 0, 0, 0, 21, 0, 1],
	"gem_stones" : [0, 0, 50, 0, 0, 0, 10, 0, 2],
	"alien_items" : [0, 0, 125, 1, 0, 0, 31, 0, 0]
};

this.updateLocalCommodityDefinition = function (goodDefinition, station, sysID) {
	log(this.name, dumpObject(goodDefinition));
	var commodity = goodDefinition.key;
	var oldDefs = this.$originalDefs[commodity];

	if (!station || !station.hasRole("sothis")) return goodDefinition;

	//define the market size for the station
	goodDefinition.capacity = 63;
	var soth = worldScripts["Spawn-sothis"];
	if (!soth) return goodDefinition;
	var conf = soth._config;
	if (!conf) return goodDefinition;

	//old style definition found for the good. calculate it the old way
	if (conf.marketType == 1 && oldDefs) {
		var market_base_price = oldDefs[2];
		var market_eco_adjust_price = oldDefs[3];
		var market_eco_adjust_quantity = oldDefs[4];
		var market_base_quantity = oldDefs[5];
		var market_mask_price = oldDefs[6];
		var market_mask_quantity = oldDefs[7];
		var market_rnd = Math.floor(Math.random() * 256);

		var economy = system.economy;

		var price = (market_base_price + (market_rnd & market_mask_price) + (economy * market_eco_adjust_price)) & 255;
		price *= 0.4;

		var quantity = (market_base_quantity + (market_rnd & market_mask_quantity) - (economy * market_eco_adjust_quantity)) & 255;
		if (quantity > 127) quantity = 0;

		quantity &= 63;

		goodDefinition.quantity = quantity;
		goodDefinition.price = price * 10;
	}
	//type 2: just add some variance to the system market (+/-5%)
	else if (conf.marketType == 2) {
		goodDefinition.price = goodDefinition.price * (1.05 - Math.random() * 0.1);
		goodDefinition.quantity = Math.floor(goodDefinition.quantity * (1.05 - Math.random() * 0.1));
	}
	// type 3: oolite-business trade goods only
	else if (conf.marketType == 3) {
		if (goodDefinition.classes.indexOf("oolite-business") >= 0) {
			// from the shipdata market_definition
			//{ type = "class"; name = "oolite-business"; price_multiplier = 1.2; price_randomiser = 0.3; quantity_multiplier = 0.7; quantity_randomiser = 0.4; }, 			
			goodDefinition.price *= (1.2 + (0.3 * (Math.random() * 2 - 1)));
			goodDefinition.quantity *= (0.7 + (0.4 * (Math.random() * 2 - 1)));
		} else {
			goodDefinition.price = 0;
			goodDefinition.quantity = 0;
			goodDefinition.capacity = 0;
		}
	}
	//type 0: no market 
	else if (conf.marketType == 0) {
		goodDefinition.price = 0;
		goodDefinition.quantity = 0;
		goodDefinition.capacity = 0;
	}

	//scale down if quantity too high
	if (goodDefinition.quantity > goodDefinition.capacity)
		goodDefinition.quantity = Math.floor(goodDefinition.quantity / 127 * goodDefinition.capacity);

	return goodDefinition;
};