'use strict';

const assert = require('./../../assert');
const common = require('./../../common');
const Sim = require('./../../../dist/sim');

let battle;

describe('Roost', () => {
	afterEach(() => {
		battle.destroy();
	});

	it('should fail if the user is at max HP', () => {
		battle = common.createBattle([[
			{ species: "Clefable", item: 'leftovers', ability: 'unaware', moves: ['calmmind'] },
		], [
			{ species: "Dragonite", item: 'laggingtail', ability: 'multiscale', moves: ['roost'] },
		]]);
		battle.makeChoices('move calmmind', 'move roost');
		assert(battle.log[battle.lastMoveLine + 1].startsWith('|-fail|'));
	});

	it('should heal the user', () => {
		battle = common.createBattle([[
			{ species: "Clefable", ability: 'unaware', moves: ['calmmind', 'hiddenpowergrass'] },
		], [
			{ species: "Dragonite", ability: 'multiscale', moves: ['roost', 'dragondance'] },
		]]);
		battle.makeChoices('move hiddenpowergrass', 'move dragondance');
		battle.makeChoices('move calmmind', 'move roost');
		assert.equal(battle.p2.active[0].hp, battle.p2.active[0].maxhp);
	});

	it('should suppress user\'s current Flying type if successful', () => {
		battle = common.createBattle([[
			{ species: "Aggron", item: 'leftovers', ability: 'sturdy', moves: ['mudslap', 'hiddenpowergrass'] },
		], [
			{ species: "Aerodactyl", item: 'focussash', ability: 'wonderguard', moves: ['roost', 'doubleedge'] },
		]]);

		battle.makeChoices('move mudslap', 'move roost');
		assert.equal(battle.p2.active[0].hp, battle.p2.active[0].maxhp); // Immune to Mud Slap

		// Ensure that Aerodactyl has some damage
		battle.makeChoices('move mudslap', 'move doubleedge');

		battle.makeChoices('move mudslap', 'move roost');
		assert.notEqual(battle.p2.active[0].hp, battle.p2.active[0].maxhp); // Hit super-effectively by Mud Slap

		// Ensure that Aerodactyl has some damage
		battle.makeChoices('move mudslap', 'move doubleedge');

		battle.makeChoices('move hiddenpowergrass', 'move roost');
		assert.notEqual(battle.p2.active[0].hp, battle.p2.active[0].maxhp); // Hit super-effectively by HP Grass
	});

	it('should suppress Flying type yet to be acquired this turn', () => {
		battle = common.createBattle({ gameType: 'doubles' }, [[
			{ species: "Pidgeot", item: 'laggingtail', ability: 'victorystar', moves: ['aircutter'] },
			{ species: "Gligar", item: 'laggingtail', ability: 'immunity', moves: ['earthquake'] },
		], [
			{ species: "Kecleon", ability: 'colorchange', moves: ['roost'] },
			{ species: "Venusaur", ability: 'chlorophyll', moves: ['earthquake'] },
		]]);

		let hitCount = 0;
		battle.p2.active[0].damage = function (...args) {
			hitCount++;
			return Sim.Pokemon.prototype.damage.apply(this, args);
		};

		battle.makeChoices('move aircutter, move earthquake', 'move roost, move earthquake');
		assert.equal(hitCount, 3);
	});

	it('should treat a pure Flying pokémon as Normal type', () => {
		battle = common.createBattle([[
			{ species: "Tornadus", item: 'focussash', ability: 'prankster', moves: ['roost'] },
		], [
			{ species: "Gastly", item: 'laggingtail', ability: 'levitate', moves: ['astonish', 'trickortreat'] },
		]]);
		battle.makeChoices('move roost', 'move astonish');
		battle.makeChoices('move roost', 'move astonish');
		assert.equal(battle.p1.active[0].hp, battle.p1.active[0].maxhp); // Immune to Astonish

		// Ensure that it also replaces the Flying type with Normal even when there is an added type
		battle.makeChoices('move roost', 'move trickortreat');
		battle.makeChoices('move roost', 'move astonish');
		battle.makeChoices('move roost', 'move astonish');
		assert.equal(battle.p1.active[0].hp, battle.p1.active[0].maxhp); // Immune to Astonish
	});

	it('should not remove Flying type during Terastallization', () => {
		battle = common.gen(9).createBattle([[
			{ species: "Dudunsparce", ability: "runaway", moves: ['sleeptalk', 'roost'], teraType: "Flying" },
		], [
			{ species: "Chansey", ability: "naturalcure", moves: ['earthquake'] },
		]]);
		battle.makeChoices();
		battle.makeChoices('move roost terastallize', 'auto');
		assert.fullHP(battle.p1.active[0]);
	});
});

describe('Roost - DPP', () => {
	afterEach(() => {
		battle.destroy();
	});

	it('should treat a pure Flying pokémon as `???` type', () => {
		battle = common.gen(4).createBattle([[
			{ species: "Arceus-Flying", item: 'skyplate', ability: 'multitype', moves: ['roost'] },
		], [
			{ species: "Gastly", item: 'laggingtail', ability: 'levitate', moves: ['astonish', 'earthpower'] },
		]]);

		battle.makeChoices('move roost', 'move astonish');
		battle.makeChoices('move roost', 'move astonish');
		assert.notEqual(battle.p1.active[0].hp, battle.p1.active[0].maxhp); // Affected by Astonish

		battle.makeChoices('move roost', 'move earthpower');
		assert.notEqual(battle.p1.active[0].hp, battle.p1.active[0].maxhp); // Affected by Earth Power
	});
});
