package xmpDynamicType;

import java.util.ArrayList;

public class Warrior extends Character {
	
	private ArrayList<String> arms;
	private int lastEarned=0;	
	static final String[] armory={"knife", "axe", "staff","mace", "shortsword", "longbow",
			"longsword", "crossbow"};
	
	/**
	 * @param name
	 * @param arms
	 */
	public Warrior(String name) {
		super(name);
		arms = new ArrayList<String>();
	}

	public void addWeapon() {
		if (arms.size()<armory.length)
		arms.add(armory[arms.size()]);
		addStrength(10);
	}
	
	@Override
	public void age() {
		super.age();
		if (getAge()>=lastEarned+10) {
			lastEarned=getAge();
			addWeapon();
		}
	}
	
	public String weaponsList() {
		return "Weapons: " + String.join(",", arms);
	}
	
	public void fight(Warrior foe) {
		if (getAge()<10) return;
		if (foe.getAge()<10) return;
		System.out.print("Fight between " + getName() + " and " + foe.getName());
		int sumStrength=this.getStrength() + foe.getStrength();
		if (Character.randGen.nextInt(sumStrength)>this.getStrength()) {
			// foe wins
			System.out.print(" Winner: " + foe.getName());
			if (arms.size()>0) {
				String forfeit = arms.get(0);
				arms.remove(forfeit);
				System.out.print(" who wrested a " + forfeit + " from his foe");
			}
			System.out.println(".");
			addStrength(-10);
			foe.addStrength(10);
			
		} else {
			System.out.print(" Winner: " + getName());
			if (foe.arms.size()>0) {
				String forfeit = foe.arms.get(0);
				foe.arms.remove(forfeit);
				System.out.print(" who wrested a " + forfeit + " from his foe");
			}
			System.out.println(".");
			addStrength(10);
			foe.addStrength(-10);
			
		}
	}
	
	public Warrior mate(Warrior lover) {
		Warrior child=null;
		if (getStrength()>40 && getAge()>15) {
			if (lover.getStrength()>50 && lover.getAge()>20) {
				if (Character.randGen.nextInt(10)>6) {
					child = new Warrior(getName()+"son");
					addStrength(-10);
					lover.addStrength(3);
				}
			}
		}
		if (child != null) {
			System.out.println(getName() + " and " + lover.getName() + 
					" would like to announce the birth of " + child.getName() +
					". Welcome and train bravely!");
		}
		return child;
	}
	
	@Override
	public void eulogy() {
		if (arms.size()==0) {
			System.out.println(getName() + " fought bravely, using only bare hands.");
		} else {
			System.out.print(getName() + " fought every foe with ");
			for(String weapon : arms ) {
				System.out.print(weapon + ", ");
			}
			System.out.println("and the courage and valor of a true warrior.");
		}
		super.eulogy();
	}

	/* (non-Javadoc)
	 * @see lab09.Character#toString()
	 */
	@Override
	public String toString() {
		return "Warrior " + super.toString() + " " + weaponsList();
	}
	
	

}
