'use strict';

const assert = require('./../../assert');
const common = require('./../../common');

let battle;

describe('Grassy Terrain', function () {
	afterEach(function () {
		battle.destroy();
	});

	it(`should change the current terrain to Grassy Terrain for five turns`, function () {
		battle = common.createBattle([[
			{species: 'Florges', moves: ['grassyterrain', 'sleeptalk']},
		], [
			{species: 'Wynaut', moves: ['sleeptalk']},
		]]);
		for (let i = 0; i < 4; i++) {
			battle.makeChoices();
			assert(battle.field.isTerrain('grassyterrain'));
		}
		battle.makeChoices('move sleeptalk', 'move sleeptalk');
		assert(battle.field.isTerrain(''));
	});

	it(`should halve the base power of Earthquake, Bulldoze, and Magnitude against grounded targets`, function () {
		battle = common.createBattle([[
			{species: 'Shaymin', moves: ['grassyterrain']},
		], [
			{species: 'Aerodactyl', moves: ['sleeptalk']},
		]]);
		battle.makeChoices();
		const shaymin = battle.p1.active[0];
		const aerodactyl = battle.p2.active[0];
		assert.equal(battle.runEvent('BasePower', aerodactyl, shaymin, Dex.moves.get('earthquake'), 100, true), 50);
		assert.equal(battle.runEvent('BasePower', shaymin, aerodactyl, Dex.moves.get('earthquake'), 100, true), 100);
		assert.equal(battle.runEvent('BasePower', aerodactyl, shaymin, Dex.moves.get('bulldoze'), 60, true), 30);
		assert.equal(battle.runEvent('BasePower', shaymin, aerodactyl, Dex.moves.get('bulldoze'), 60, true), 60);
	});

	it(`should increase the base power of Grass-type attacks used by grounded Pokemon`, function () {
		battle = common.gen(7).createBattle([[
			{species: 'Shaymin', moves: ['grassyterrain']},
		], [
			{species: 'Aerodactyl', moves: ['sleeptalk']},
		]]);
		battle.makeChoices();
		let basePower;
		const move = Dex.moves.get('gigadrain');
		const shaymin = battle.p1.active[0];
		const aerodactyl = battle.p2.active[0];
		basePower = battle.runEvent('BasePower', shaymin, aerodactyl, move, move.basePower, true);
		assert.equal(basePower, battle.modify(move.basePower, 1.5));
		basePower = battle.runEvent('BasePower', aerodactyl, shaymin, move, move.basePower, true);
		assert.equal(basePower, move.basePower);
	});

	it(`should heal grounded Pokemon by 1/16 of their max HP`, function () {
		battle = common.createBattle([[
			{species: 'Shaymin', moves: ['grassyterrain', 'seismictoss']},
		], [
			{species: 'Wynaut', moves: ['magnetrise', 'seismictoss']},
		]]);
		battle.makeChoices('move grassyterrain', 'move magnetrise');
		battle.makeChoices('move seismictoss', 'move seismictoss');
		const shaymin = battle.p1.active[0];
		const wynaut = battle.p2.active[0];
		assert.equal(shaymin.hp, shaymin.maxhp - 100 + Math.floor(shaymin.maxhp / 16));
		assert.equal(wynaut.hp, wynaut.maxhp - 100);
	});

	it(`should not affect Pokemon in a semi-invulnerable state`, function () {
		battle = common.createBattle([[
			{species: 'Shaymin', moves: ['grassyterrain', 'seismictoss']},
		], [
			{species: 'Wynaut', moves: ['skydrop', 'seismictoss']},
		]]);
		battle.makeChoices('move seismictoss', 'move seismictoss');
		battle.makeChoices('move grassyterrain', 'move skydrop');
		const shaymin = battle.p1.active[0];
		const wynaut = battle.p2.active[0];
		assert.equal(shaymin.hp, shaymin.maxhp - 100);
		assert.equal(wynaut.hp, wynaut.maxhp - 100);
	});

	it(`should cause Nature Power to become Energy Ball`, function () {
		battle = common.createBattle([[
			{species: 'Shaymin', moves: ['grassyterrain']},
		], [
			{species: 'Wynaut', moves: ['naturepower']},
		]]);
		battle.makeChoices();
		const resultMove = toID(battle.log[battle.lastMoveLine].split('|')[3]);
		assert.equal(resultMove, 'energyball');
	});

	it(`should heal by Speed order in the same block as Leftovers`, function () {
		battle = common.createBattle([[
			{species: 'rillaboom', ability: 'grassysurge', item: 'leftovers', moves: ['seismictoss']},
		], [
			{species: 'alakazam', item: 'focussash', moves: ['seismictoss']},
		]]);

		battle.makeChoices();
		const log = battle.getDebugLog();
		const zamGrassyIndex = log.indexOf('|-heal|p2a: Alakazam|166/251|[from] Grassy Terrain');
		const rillaGrassyIndex = log.indexOf('|-heal|p1a: Rillaboom|262/341|[from] Grassy Terrain');
		const rillaLeftoversIndex = log.indexOf('|-heal|p1a: Rillaboom|283/341|[from] item: Leftovers');
		assert(zamGrassyIndex < rillaGrassyIndex, 'Alakazam should heal from Grassy Terrain before Rillaboom');
		assert(rillaGrassyIndex < rillaLeftoversIndex, 'Rillaboom should heal from Grassy Terrain before Leftovers');
	});
});
