"use strict";

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

module.exports = {

	init() {
		this.playerLib = blackout.playerLib;
	},

	/**
  * Creates and store a new starter Squad
  * @param  {object} params A list of parameters
  * @return {array}        The newborn players
  * 
  * Params
  * ------
 	{
 		// Required
 		clubId: '',
 		managerId: '',
 		
 		// Optional
 		numPlayers: 25,  // {int} The number of playes we want to generate in the squad
 	}
  */
	createAndStoreStarterSquad(params, callback) {
		var _this = this;

		return _asyncToGenerator(function* () {

			try {

				if (!params.managerId) {
					throw new Error('Manager Id must be provided in params to createAndStoreStarterSquad()');
				} else if (!params.clubId) {
					throw new Error('Club Id must be provided in params to createAndStoreStarterSquad()');
				}

				let squadParams = {
					numPlayers: 25,
					ageAverage: 20.5,
					ageRange: 5,
					managerId: params.managerId,
					clubId: params.clubId
				};

				// Generate squad
				let squad = yield _this.createSquadAsync(squadParams);

				// Store squad
				let putParams = {
					table: 'Players',
					items: squad
				};
				yield blackout.db.putItemsAsync(putParams);

				callback(null, squad);
			} catch (err) {
				blackout.errorInternal(err);
			}
		})();
	},

	/**
  * Creates a new Squad
  * @param  {object} params A list of parameters
  * @return {array}        The newborn players
  * 
  * Params
  * ------
 	{
 		// Required
 		...
 		
 		// Optional
 		numPlayers: 25,
 		ageAverage: 20.5,
 		ageRange: 5,
 		clubId: '',
 	}
  */
	createSquad(params, callback) {
		var _this2 = this;

		return _asyncToGenerator(function* () {

			// Defaults
			params = Object.assign({
				numPlayers: 25,
				ageAverage: 20.5,
				ageRange: 5
			}, params);

			try {

				// Params for all players
				let playerParams = {
					isSquad: true
				};

				let positions = {
					'p1': 'prop',
					'p2': 'hooker',
					'p3': 'prop',
					'p4': 'lock',
					'p5': 'lock',
					'p6': 'flanker',
					'p7': 'flanker',
					'p8': 'no8',
					'p9': 'scrumhalf',
					'p10': 'flyhalf',
					'p11': 'wing',
					'p12': 'center',
					'p13': 'center',
					'p14': 'wing',
					'p15': 'fullback',
					'b1': 'prop',
					'b2': 'hooker',
					'b3': 'lock',
					'b4': ['flanker', 'no8'],
					'b5': 'scrumhalf',
					'b6': 'center',
					'b7': ['wing', 'fullback'],
					'e1': ['prop', 'hooker', 'lock', 'flanker', 'no8'],
					'e2': 'flyhalf',
					'e3': ['wing', 'fullback', 'center']
				};
				let positionKeys = Object.keys(positions);

				// Age range
				let averageAge = 20.5;
				let ageRange = 5;
				let ages = blackout.stats.generateDataWithForcedAverage(averageAge, ageRange, params.numPlayers);

				let awaits = [],
				    players = [];

				// Generate a bunch of players
				for (var i = 0; i < params.numPlayers; i++) {

					// Figure out position
					let position;
					if (i < 15) {
						position = i + 1; // To ensure at least one of each jersey number (preferred position)
					} else {
						let positionKey = i < positionKeys.length ? positionKeys[i] : null;
						if (positionKey) {
							position = positions[positionKey];
						} else {
							// Get random position
							position = _this2.playerLib.positions.getRandom();
						}
					}

					// If position is array, get random
					if (blackout.isArray(position)) {
						position = position.getRandom();
					}

					// Params per player
					playerParams.position = position;
					playerParams.age = ages[i];

					// Create player, but don't wait yet
					awaits.push(_this2.createPlayerAsync(playerParams).then(function (player) {
						if (params.clubId) {
							player.Club = params.clubId;
						}
						if (params.managerId) {
							player.Manager = params.managerId;
						}
						players.push(player);
					}));
				}

				yield Promise.all(awaits);
				callback(null, players);
			} catch (err) {
				blackout.errorInternal(err);
			}
		})();
	},

	/**
  * Creates a new player
  * @param  {object} params A list of parameters
  * @return {object}        The newborn player
  * 
  * Params
  * ------
 	{
 		// Required
 		position: // {string|int}  The position of the player
 		
 		// Optional
 		age: 18,                 // {int}     The age of the player, which will add some assumed pre training beyond 18 years old.
 		cardLevel: 1,            // {int}     The playing card level
 		clubPlayerLevel: 15,     // {int}     The club's current player level
 		playerType: 'standard',  // {string}  The type of player: standard|star|superstar
 		isSquad: false,          // {bool}    Set to true if generating a squad
 	}
  */
	createPlayer(params, callback) {
		var _this3 = this;

		return _asyncToGenerator(function* () {

			// Defaults
			params = Object.assign({
				age: 18,
				cardLevel: 1,
				clubPlayerLevel: 15,
				playerType: 'standard',
				isSquad: false
			}, params);

			try {

				let awaits = [];

				let player = { Attributes: {}, Skills: {}, Meta: {}, Avatar: {} };

				// -------------------------------------- Nationality an Ethnicity

				player.Nationality = 'NZ';

				player.Ethnicity = _this3.createPlayerEthnicity(player.Nationality);
				player.Race = _this3.createPlayerRace(player.Ethnicity);

				// -------------------------------------- Name

				awaits.push(_this3.createPlayerFirstNameAsync(player));
				awaits.push(_this3.createPlayerLastNameAsync(player));

				// -------------------------------------- Energy

				player.Energy = Math.min(100, blackout.rand(70, 110));

				// -------------------------------------- Form

				player.Form = blackout.rand(3, 5);

				// -------------------------------------- Dominant side

				player.DominantSide = _this3.getRandomDominantSide();

				// -------------------------------------- Age & Age of decline & Birthday

				awaits.push(_this3.createPlayerAgeAsync(player, params));

				// -------------------------------------- Attributes & Skills

				_this3.createPlayerSkills(player, params);

				// -------------------------------------- Ratings

				_this3.playerLib.refreshPlayerRatings(player);

				// -------------------------------------- Level

				player.Level = _this3.playerLib.getPlayerLevel(player);

				// -------------------------------------- Contract

				_this3.createPlayerContract(player);

				// -------------------------------------- Position

				player.Positions = [isNaN(params.position) ? _this3.playerLib.positionWordToNumber(params.position) : params.position];

				// -------------------------------------- Avatar

				_this3.createPlayerAvatar(player);

				// -------------------------------------- All done!

				player.Date = new Date().toISOString();

				yield Promise.all(awaits);
				callback(null, player);
			} catch (err) {
				blackout.errorInternal(err);
			}
		})();
	},

	createPlayerContract(player) {

		let contract = {};

		// Get contract length
		contract.Seasons = blackout.rand(1, 3);

		// Get expiry
		contract.Expires = blackout.moment().add(contract.Seasons * 4, 'w').format();

		// Get salary
		contract.Salary = blackout.playerLib.getSalary(player);

		player.Contract = contract;
	},

	createPlayerAge(player, params, callback) {
		return _asyncToGenerator(function* () {
			const stats = blackout.stats;

			try {

				// -------------------------------------- Age

				player.Age = params.age;

				// -------------------------------------- Age of decline

				let daysFromAverage = stats.generateRoundedNormal(0, 1.5 * 28, -2.5 * 28, 2.5 * 28);
				let ageOfDecline = Math.floor((daysFromAverage + 14) / 28); // 32.5 is the average
				player.AgeOfDecline = {
					Age: 32 + ageOfDecline,
					Day: daysFromAverage + 14 - ageOfDecline * 28 };

				// -------------------------------------- Date joined club

				let { season, day } = yield blackout.getSeasonAndDayAsync(null);

				player.JoinedClub = {
					Season: season,
					Day: day
				};

				// -------------------------------------- Birthday

				if (params.isSquad) {
					// Birthday should be any day in the last 27 days
					day += blackout.rand(-27, -1);
				} else {
					// Birthday should be in the last 7 days
					day += blackout.rand(-7, -1);
				}
				if (day < 1) {
					season--;
					day += 28;
				}

				player.BirthDay = day;

				// -------------------------------------- Birthdate (For sorting)

				// When checking this number, don't forget about the season where season=0
				player.BirthDate = (season - 1 - player.Age) * 28 + day;

				// -------------------------------------- Done with age stuff

				callback(null, player);
			} catch (err) {
				blackout.errorInternal(err);
			}
		})();
	},

	createPlayerSkills(player, params) {

		let info = this.createPlayerPositionInfo(params);
		//console.log('info',info);
		const stats = blackout.stats;

		// -------------------------------------- Weight & Height

		player.Attributes.Weight = stats.generateRoundedNormal(info.weight, info.weightDeviation, info.weightMin, info.weightMax);
		player.Attributes.Height = stats.generateRoundedNormal(info.height, info.heightDeviation, info.heightMin, info.heightMax);

		// -------------------------------------- Attributes & Skills

		this.createPlayerSkillLevels(info.skillsCat1, info.levels.skillsCat1, player);
		this.createPlayerSkillLevels(info.skillsCat2, info.levels.skillsCat2, player);
		this.createPlayerSkillLevels(info.skillsCat3, info.levels.skillsCat3, player);
		this.createPlayerSkillLevels(info.skillsCat4, info.levels.skillsCat4, player);

		// -------------------------------------- Aggression

		player.Attributes.Aggression = stats.generateRoundedNormal(3, 1.5, 1, 5);

		// -------------------------------------- Leadership

		this.createPlayerSkillLevels(['Leadership'], info.levels.skillsCat3, player);
	},

	createPlayerSkillLevels(skills, settings, player) {

		// Get levels with forced average
		let levels = blackout.stats.generateDataWithForcedAverage(settings.base, settings.mod * 2, skills.length);

		for (let [i, skill] of skills.entries()) {

			let group = this.playerLib.skills.includes(skill) ? player.Skills : player.Attributes;
			let level = levels[i];

			// Add skill to player
			group[skill] = this.playerLib.levelToPoints(level);

			// Determine max skill level
			//let ageIncrease = settings.base - settings.baseOG;
			let maxLevel = Math.min(settings.max, Math.max(0, settings.baseOG + settings.distToMax + 1));

			// Add meta
			player.Meta[skill] = {
				Start: this.playerLib.levelToPoints(level),
				Max: this.playerLib.levelToPoints(maxLevel) - 1,
				Cat: settings.cat
			};
		}
	},

	createPlayerPositionInfo(params) {

		// Get position as a word
		let position = params.position;

		if (!isNaN(position)) {
			position = this.playerLib.positionNumberToWord(position);
		}

		// Defaults
		let info = {};
		let positionalSkills = ['Scrum', 'Maul', 'LineoutJumping', 'LineoutThrowing', 'Kicking', 'Strength', 'Awareness'];
		let generalSkills = ['Power', 'Agility', 'Speed', 'Acceleration', 'Coordination', 'Endurance', 'Composure', 'Passing', 'Catching', 'Tackling', 'LineBreaking', 'Rucking', 'CounterRucking'];

		// Positional Skills
		let skillsForPosition = this.playerLib.getPositionalSkills(position);
		info.skillsCat1 = skillsForPosition.skillsCat1;
		info.skillsCat2 = skillsForPosition.skillsCat2;
		info.skillsCat3 = skillsForPosition.skillsCat3;

		// Positional modifications
		switch (position) {

			/**
    * Averages + std deviations derived from Super Rugby Players 2016
    * Mins + Maxes are also derived from these stats, but extended slightly. Actuals are provided in comments.
    */

			case 'prop':

				// Weight
				info.weight = 117;
				info.weightDeviation = 6.3;
				info.weightMin = 100; // 101 OG
				info.weightMax = 140; // 135 OG

				// Height
				info.height = 184;
				info.heightDeviation = 4.5;
				info.heightMin = 170; // 173 OG
				info.heightMax = 198; // 195 OG

				break;

			case 'hooker':

				// Weight
				info.weight = 107;
				info.weightDeviation = 6.0;
				info.weightMin = 80; // 82 OG
				info.weightMax = 120; // 118 OG

				// Height
				info.height = 182;
				info.heightDeviation = 3.9;
				info.heightMin = 170; // 173 OG
				info.heightMax = 190; // 189 OG

				break;

			case 'lock':

				// Weight
				info.weight = 114;
				info.weightDeviation = 6.4;
				info.weightMin = 95; // 95 OG
				info.weightMax = 140; // 140 OG

				// Height
				info.height = 198.5;
				info.heightDeviation = 3.3;
				info.heightMin = 190; // 192 OG
				info.heightMax = 210; // 208 OG

				break;

			case 'flanker':

				// Weight
				info.weight = 103;
				info.weightDeviation = 6.0;
				info.weightMin = 90; // 90 OG
				info.weightMax = 120; // 116 OG

				// Height
				info.height = 188;
				info.heightDeviation = 5.0;
				info.heightMin = 175; // 176 OG
				info.heightMax = 200; // 201 OG

				break;

			case 'no8':

				// Weight
				info.weight = 106;
				info.weightDeviation = 4.1;
				info.weightMin = 100; // 100 OG
				info.weightMax = 115; // 115 OG

				// Height
				info.height = 189;
				info.heightDeviation = 3.9;
				info.heightMin = 180; // 182 OG
				info.heightMax = 195; // 194 OG

				break;

			case 'scrumhalf':

				// Weight
				info.weight = 83.5;
				info.weightDeviation = 6.0;
				info.weightMin = 70; // 70 OG
				info.weightMax = 95; // 95 OG

				// Height
				info.height = 176.5;
				info.heightDeviation = 5.2;
				info.heightMin = 165; // 166 OG
				info.heightMax = 190; // 187 OG

				break;

			case 'flyhalf':

				// Weight
				info.weight = 83.5;
				info.weightDeviation = 6.0;
				info.weightMin = 70; // 70 OG
				info.weightMax = 95; // 95 OG

				// Height
				info.height = 180.6;
				info.heightDeviation = 5.2;
				info.heightMin = 165; // 168 OG
				info.heightMax = 195; // 192 OG

				break;

			case 'wing':

				// Weight
				info.weight = 94.5;
				info.weightDeviation = 8.5;
				info.weightMin = 75; // 76 OG
				info.weightMax = 125; // 125 OG

				// Height
				info.height = 184.4;
				info.heightDeviation = 6.1;
				info.heightMin = 170; // 170 OG
				info.heightMax = 199; // 197 OG

				break;

			case 'center':

				// Weight
				info.weight = 96.4;
				info.weightDeviation = 5.8;
				info.weightMin = 80; // 83 OG
				info.weightMax = 110; // 110 OG

				// Height
				info.height = 185.1;
				info.heightDeviation = 4.5;
				info.heightMin = 170; // 173 OG
				info.heightMax = 199; // 195 OG

				break;

			case 'fullback':

				// Weight
				info.weight = 91;
				info.weightDeviation = 7.8;
				info.weightMin = 70; // 73 OG
				info.weightMax = 110; // 104 OG

				// Height
				info.height = 183.4;
				info.heightDeviation = 5.3;
				info.heightMin = 170; // 173 OG
				info.heightMax = 200; // 194 OG

				break;

			default:
				blackout.errorInternal(new Error('An unknown rugby position was provided to createPlayer()'));

		}

		// Set skill levels, starts and maximums
		info.levels = this.createPlayerSkillLevelsInfo(params);

		// Add unused general skills
		info.skillsCat4 = [];
		generalSkills.shuffle();

		let i = 0;
		do {
			if (!info.skillsCat1.includes(generalSkills[i]) && !info.skillsCat2.includes(generalSkills[i]) && !info.skillsCat3.includes(generalSkills[i])) {
				info.skillsCat4.push(generalSkills[i]);
			}
			i++;
		} while (i < generalSkills.length);

		// Add any excess positional skills
		i = 0;
		do {
			if (!info.skillsCat1.includes(positionalSkills[i]) && !info.skillsCat2.includes(positionalSkills[i]) && !info.skillsCat3.includes(positionalSkills[i])) {
				info.skillsCat4.push(positionalSkills[i]);
			}
			i++;
		} while (i < positionalSkills.length);

		return info;
	},

	createPlayerSkillLevelsInfo(params) {

		let info = {
			skillsCat1: {},
			skillsCat2: {},
			skillsCat3: {},
			skillsCat4: {}
		};

		switch (params.cardLevel) {

			case 5:

				// Base levels
				info.skillsCat1.base = 37;
				info.skillsCat2.base = 33;
				info.skillsCat3.base = 31;
				info.skillsCat4.base = 15;

				// Possible offset from base
				info.skillsCat1.mod = 3;
				info.skillsCat2.mod = 4;
				info.skillsCat3.mod = 5;
				info.skillsCat4.mod = 4;

				break;

			case 4:

				// Base levels
				info.skillsCat1.base = 35;
				info.skillsCat2.base = 30;
				info.skillsCat3.base = 27;
				info.skillsCat4.base = 13;

				// Possible offset from base
				info.skillsCat1.mod = 3;
				info.skillsCat2.mod = 3;
				info.skillsCat3.mod = 6;
				info.skillsCat4.mod = 3;

				break;

			case 3:

				// Base levels
				info.skillsCat1.base = 32;
				info.skillsCat2.base = 27;
				info.skillsCat3.base = 24;
				info.skillsCat4.base = 11;

				// Possible offset from base
				info.skillsCat1.mod = 3;
				info.skillsCat2.mod = 3;
				info.skillsCat3.mod = 5;
				info.skillsCat4.mod = 3;

				break;

			case 2:

				// Base levels
				info.skillsCat1.base = 27;
				info.skillsCat2.base = 22;
				info.skillsCat3.base = 19;
				info.skillsCat4.base = 9;

				// Possible offset from base
				info.skillsCat1.mod = 3;
				info.skillsCat2.mod = 3;
				info.skillsCat3.mod = 4;
				info.skillsCat4.mod = 2;

				break;

			case 1:
			default:

				// Base levels
				info.skillsCat1.base = 20;
				info.skillsCat2.base = 15;
				info.skillsCat3.base = 13;
				info.skillsCat4.base = 7;

				// Possible offset from base
				info.skillsCat1.mod = 3;
				info.skillsCat2.mod = 3;
				info.skillsCat3.mod = 3;
				info.skillsCat4.mod = 1;

		}

		// Add distance to maximums
		info.skillsCat1.distToMax = 40;
		info.skillsCat2.distToMax = 35;
		info.skillsCat3.distToMax = 30;
		info.skillsCat4.distToMax = 30;

		// Determine absolute skill max
		let max;
		if (params.playerType === 'superstar') {
			max = this.playerLib.skillMaxLevelSuperStars;
		} else if (params.playerType === 'star') {
			max = this.playerLib.skillMaxLevelStars;
		} else {
			max = this.playerLib.skillMaxLevelStandard;
		}
		info.skillsCat1.max = max;
		info.skillsCat2.max = max;
		info.skillsCat3.max = max;
		info.skillsCat4.max = max;

		// Add category
		info.skillsCat1.cat = 1;
		info.skillsCat2.cat = 2;
		info.skillsCat3.cat = 3;
		info.skillsCat4.cat = 4;

		// ------------------------------------------------------------ //

		// All else being equal, these settings would create a player with a higher level than we expect
		// due to how levels are worked out based on the highest skills. Right now this is about level 17.
		// We must add a mod below to try and remedy this.
		let mod = 10 / 17;
		// ^ Should be rechecked if we change the points progression (exponential growth rate)
		// You can check this by generating a cardLevel 1 18yo without the following age mods
		// and seeing how far away from level 10 they are

		// Base levels
		info.skillsCat1.base = Math.round(info.skillsCat1.base * mod);
		info.skillsCat2.base = Math.round(info.skillsCat2.base * mod);
		info.skillsCat3.base = Math.round(info.skillsCat3.base * mod);
		info.skillsCat4.base = Math.round(info.skillsCat4.base * mod);

		// Possible offset from base
		info.skillsCat1.mod = Math.round(info.skillsCat1.mod * mod);
		info.skillsCat2.mod = Math.round(info.skillsCat2.mod * mod);
		info.skillsCat3.mod = Math.round(info.skillsCat3.mod * mod);
		info.skillsCat4.mod = Math.round(info.skillsCat4.mod * mod);

		// Store base before age mods
		info.skillsCat1.baseOG = info.skillsCat1.base;
		info.skillsCat2.baseOG = info.skillsCat2.base;
		info.skillsCat3.baseOG = info.skillsCat3.base;
		info.skillsCat4.baseOG = info.skillsCat4.base;

		// Add age mods
		// Apply the player's age, if they have some aging added
		// This should be based on how fast a club would train a player based on the club's age
		let percentageThroughTrainableLife = (params.age - 18) / 14;
		let levelsThroughClubTraining = Math.round((params.clubPlayerLevel - 10) * percentageThroughTrainableLife * mod);

		// Apply age
		let proportionOfGrowth = levelsThroughClubTraining / info.skillsCat1.base;
		info.skillsCat1.base += levelsThroughClubTraining;
		info.skillsCat1.mod += Math.round(info.skillsCat1.mod * proportionOfGrowth);

		proportionOfGrowth = levelsThroughClubTraining / info.skillsCat2.base;
		info.skillsCat2.base += levelsThroughClubTraining;
		info.skillsCat2.mod += Math.round(info.skillsCat2.mod * proportionOfGrowth);

		proportionOfGrowth = levelsThroughClubTraining / info.skillsCat3.base;
		info.skillsCat3.base += levelsThroughClubTraining;
		info.skillsCat3.mod += Math.round(info.skillsCat3.mod * proportionOfGrowth);

		proportionOfGrowth = levelsThroughClubTraining / info.skillsCat4.base;
		info.skillsCat4.base += levelsThroughClubTraining;
		info.skillsCat4.mod += Math.round(info.skillsCat4.mod * proportionOfGrowth);

		return info;
	},

	createPlayerAvatar(player) {

		// -------------------------------------- Hair colour

		let colours = ['black', 'darkBrown', 'brown', 'lightBrown', 'red', 'blonde', 'bald', 'headgear', 'purple', 'blue', 'green'];
		player.Avatar.HairColour = colours.getRandom();

		// -------------------------------------- Skin colour

		let skins = ['white', 'olive', 'brown', 'black'];
		player.Avatar.Skin = skins[blackout.rand(0, skins.length - 1)];

		// -------------------------------------- Body Keys

		let bodyKeys = ['weight_arm_max', 'weight_arm_min', 'weight_leg_max', 'weight_leg_min', 'weight_abdomen_max', 'weight_abdomen_min', 'weight_head_max', 'weight_head_min', 'muscle_arm_max', 'muscle_arm_min', 'muscle_leg_max', 'muscle_leg_min', 'muscle_abdomen_max', 'muscle_abdomen_min', 'heightproportioncorrector_max', 'heightproportioncorrector_min'];

		player.Avatar.BodyKeys = {};
		for (let key of bodyKeys) {
			player.Avatar.BodyKeys[key] = blackout.rand0_1();
		}

		// -------------------------------------- Face Keys

		let faceKeys = ['ear_size_max', 'ear_size_min', 'ear_angle_max', 'ear_angle_min', 'lip_size_max', 'lip_size_min', 'lip_width_max', 'lip_width_min', 'cheek_fat_max', 'cheek_fat_min', 'neck_fat_max', 'neck_fat_min', 'nose_size_max', 'nose_size_min', 'nose_length_max', 'nose_length_min', 'chin_length_max', 'chin_length_min', 'eye_height_max', 'eye_height_min', 'head_width_max', 'head_width_min', 'face_height_max', 'face_height_min'];

		player.Avatar.FaceKeys = {};
		for (let key of faceKeys) {
			player.Avatar.FaceKeys[key] = blackout.rand0_1();
		}
	},

	createPlayerEthnicity(nationalityISO) {

		let x = blackout.rand(1, 100);
		let playerISO;

		switch (nationalityISO) {
			case 'NZ':
				switch (true) {
					case x <= 75:
						playerISO = 'EN';
						break;
					case x === 76:
						playerISO = 'ZA';
						break;
					case x > 76 && x <= 96:
						playerISO = 'PI';
						break;
					case x === 97:
						playerISO = 'SL';
						break;
					case x === 98:
						playerISO = 'WA';
						break;
					case x === 99:
						playerISO = 'IE';
						break;
					case x === 100:
						playerISO = 'JP';
						break;
				}
				break;
		}

		return playerISO;
	},

	createPlayerFirstName(player, callback) {
		return _asyncToGenerator(function* () {

			let nameItem = yield blackout.db.getRandomItemAsync({
				table: 'Names',
				group: { field: 'Country', value: player.Ethnicity, index: 'Country' },
				filter: { NameType: 1 },
				cacheAllItems: true
			});
			player.FirstName = nameItem.Name;

			callback();
		})();
	},

	createPlayerLastName(player, callback) {
		return _asyncToGenerator(function* () {

			let nameItem = yield blackout.db.getRandomItemAsync({
				table: 'Names',
				group: { field: 'Country', value: player.Ethnicity, index: 'Country' },
				filter: { NameType: 2 },
				cacheAllItems: true
			});
			player.LastName = nameItem.Name;

			callback();
		})();
	},

	getRandomDominantSide() {

		let x = blackout.rand(1, 100);

		if (x <= 90) return 'Right';else return 'Left';
	},

	createPlayerRace(ethnicity) {

		let race;

		switch (ethnicity) {

			case 'JP':
			case 'PI':
			case 'KR':
			case 'HK':
				race = 'Asian';
				break;

			case 'EN':
			case 'FR':
			case 'ZA':
			case 'ES':
			case 'WA':
			case 'IE':
			case 'AR':
			case 'DE':
			case 'NL':
			case 'BR':
			case 'CL':
			case 'BE':
			case 'PT':
			case 'PL':
			case 'HR':
			case 'IT':
			case 'RO':
			case 'CZ':
			case 'RU':
				race = 'Caucasian';
				break;

			case 'SL':
			case 'SA':
			case 'TR':
			case 'CI':
			case 'NA':
			case 'ZW':
			case 'KE':
				race = 'African';
				break;

			default:
				race = 'Pacific';
				break;

		}

		return race;
	}

};
//# sourceMappingURL=genesis.js.map