package com.planet_ink.coffee_mud.Abilities.Common;
import com.planet_ink.coffee_mud.core.interfaces.*;
import com.planet_ink.coffee_mud.core.*;
import com.planet_ink.coffee_mud.Abilities.interfaces.*;
import com.planet_ink.coffee_mud.Areas.interfaces.*;
import com.planet_ink.coffee_mud.Behaviors.interfaces.*;
import com.planet_ink.coffee_mud.CharClasses.interfaces.*;
import com.planet_ink.coffee_mud.Commands.interfaces.*;
import com.planet_ink.coffee_mud.Common.interfaces.*;
import com.planet_ink.coffee_mud.Exits.interfaces.*;
import com.planet_ink.coffee_mud.Items.interfaces.*;
import com.planet_ink.coffee_mud.Locales.interfaces.*;
import com.planet_ink.coffee_mud.MOBS.interfaces.*;
import com.planet_ink.coffee_mud.Races.interfaces.*;

import java.util.*;

/* 
   Part of the Mabus Battle System
   Created for CoffeeMud version 5.2.3
   CoffeeMud 5.2.3 is Copyright 2000-2006 Bo Zimmerman
   CoffeeMud 5.2.3 is Licensed under the Apache License, Version 2.0
   This code Copyright 2009   Throes of Creation (ToC)
   Permission to Use and Modify Granted
   This above notice must remain in the source.

   This file goes in /com/planet_ink/coffee_mud/Abilities/Common
   
   Recommended to add to Artisan class at  higher-level:

   CMLib.ableMapper().addCharAbilityMapping(ID(),35,"EnhanceWeapon",false,CMParms.parseSemicolons("Weaponsmithing",true));

   Help file addition recommendation for /resources/help/common_help.ini:

ENHANCE_WEAPON=ENHANCEWEAPON
ENHANCE_WEAPON=<ABILITY>\
Usage    : ENHANCEWEAPON [WEAPON NAME] [PARAMETER]\n\r\
Usage    : EWEAP [WEAPON NAME] [PARAMETER]\n\r\
Example  : eweap dagger damage\n\r\
Example  : eweap dagger attack\n\r\
Example  : eweap dagger examine\n\r\
This skill allows the crafter to examine if a weapon was made by them, and   \
to enhance the damage or attack of the weapon. A weapon may only be enhanced \
one time, and failed attempts can lower the attack or damage (and possibly \
even destroy the weapon).

*/

public class EnhanceWeapon extends CommonSkill
{
	public String ID() { return "EnhanceWeapon"; }
	public String name(){ return "Enhance Weapon";}
	private static final String[] triggerStrings = {"ENHANCEWEAPON","EWEAP"};
	public String[] triggerStrings(){return triggerStrings;}

	protected Item found=null;
	protected Weapon eWeapon=null;
	protected String writing="";
	protected String DoWhat="";
	protected String WhoMade="";
	protected int eChange=0;
	protected boolean damOrAtt=false;
	protected boolean esuccess=false;

	public EnhanceWeapon()
	{
		super();
		displayText="You are enchancing...";
		verb="enhancing";
	}

	public void unInvoke()
	{
		if(canBeUninvoked())
		{
			if((affected!=null)&&(affected instanceof MOB)&&(!aborted)&&(!helping))
			{
				MOB mob=(MOB)affected;
				if(!(esuccess))     			//failure
				{
					int minus=(CMLib.dice().roll(1,10,0))*(-1);
					commonEmote(mob,"<S-NAME> mess(es) up the enhancing.");
					if(damOrAtt) 			//damage
					{
						minus=found.baseEnvStats().attackAdjustment()+minus;
                                                found.baseEnvStats().setDamage(minus);
                                                found.recoverEnvStats();
					}
					else         			//attack
					{
						minus=found.baseEnvStats().attackAdjustment()+minus;
   	 				 	found.baseEnvStats().setAttackAdjustment(minus);
   	 				 	found.recoverEnvStats();

					}
					found.setUsesRemaining((found.usesRemaining()-CMLib.dice().roll(1,99,0)));
					found.recoverEnvStats();
					if(found.usesRemaining()<1)    //destroy
					{
						commonEmote(mob,"<S-NAME> wince(s) as "+found.name()+" shatter(s).");
						found.destroy();
					}
					mob.location().recoverEnvStats();
				}
				else                 			//success
				{
				    	if(damOrAtt) 			//damage
					{
						found.baseEnvStats().setDamage(eChange);
					}
					else         			//attack
					{
                                         	found.baseEnvStats().setAttackAdjustment(eChange);
					}
					found.setSecretIdentity(WhoMade+DoWhat);
					found.recoverEnvStats();
					found.text();
					found.recoverEnvStats();
				}
			}
		}
		super.unInvoke();
	}


	public boolean invoke(MOB mob, Vector commands, Environmental givenTarget, boolean auto, int asLevel)
	{
		if(commands.size()<2)
		{
			commonTell(mob,"You must specify what weapon you want to enhance, and how you want to enhance it.");
			return false;
		}

		if(getRequiredFire(mob,0)==null)
		{
			commonTell(mob,"You need a fire in the room to be able to enhance a weapon.");
                        return false;
  		}

		Item target=mob.fetchCarried(null,(String)commands.firstElement());

		if((target==null)||(!CMLib.flags().canBeSeenBy(target,mob)))
		{
			commonTell(mob,"You don't seem to have a '"+((String)commands.firstElement())+"'.");
			return false;
		}
		commands.remove(commands.firstElement());

		if(!(target instanceof Weapon))
		{
			commonTell(mob,"You can only enhance weapons with this skill, but "+target.name()+" is not a weapon.");
			return false;
		}

		if(!target.isGeneric())
		{
			commonTell(mob,"You can't enhance this weapon.");
			return false;
		}

		WhoMade=target.rawSecretIdentity();
		boolean beenDone=false;		//enhanced previously?

                if(WhoMade.contains("Enhanced"))
 		{
			beenDone=true;
		}

                found=target;
                eWeapon=(Weapon)found;
                writing=CMParms.combine(commands,0).toLowerCase();

                int eWprof=this.proficiency();
                int eWmod=-30;      		 //apply penalty if weapon not made by Artisan

		if(WhoMade.contains(mob.name())) //if weapon made by Artisan remove penalty
                {
			eWmod=0;
   		}

		if(writing.equals("examine"))
		{
			if(WhoMade.contains(mob.name()))
                        {
			 	commonTell(mob,"You recognize that "+target.name()+" was made by you.");
			 	if(beenDone)
				{
				 	commonTell(mob,"This weapon has already been enhanced.");
				}
				return false;
			}
			else
			{
			    	commonTell(mob,"You have no idea who made "+target.name()+".");
			    	if(beenDone)
			    	{
				 	commonTell(mob,"This weapon has already been enhanced.");
				}
				return false;
			}
		}
		else
                if(writing.equals("damage"))
		{
			if(beenDone)
  			{
				commonTell(mob,"This weapon has already been enhanced.");
				return false;
			}
                        int oldDam=eWeapon.baseEnvStats().damage();
                        if(oldDam<1) oldDam=1;
			eChange=((oldDam*eWprof)/100)+1;
			eChange=CMLib.dice().roll(1,eChange,0);
			eChange=eChange+oldDam;      	//could combine above and here, but prefer separate for now
			damOrAtt=true;    		//damage

		}
		else
		if(writing.equals("attack"))
		{
			if(beenDone)
  			{
			 	commonTell(mob,"This weapon has already been enhanced.");
			 	return false;
			}
                        int oldAtt=eWeapon.baseEnvStats().attackAdjustment();
                        eChange=(eWprof*50)/100+1;
                        eChange=CMLib.dice().roll(1,eChange,0);
                        eChange=eChange+oldAtt;
                        damOrAtt=false;   		//attack
		}
		else
                {
			commonTell(mob,"You can only use EXAMINE, DAMAGE or ATTACK with Enhance Weapon.");
			return false;
		}

                DoWhat=" Enhanced "+writing+" by "+mob.name()+".";

		if(!super.invoke(mob,commands,givenTarget,auto,asLevel))
			return false;
		verb="enhancing the "+writing+" of "+target.name();
		displayText="You are "+verb;

		esuccess=proficiencyCheck(mob,eWmod,auto);

		int duration=70-mob.envStats().level()+target.envStats().level();

		if((target.material()&RawMaterial.MATERIAL_MASK)==RawMaterial.MATERIAL_MITHRIL)
			duration*=2;

		if(duration<6) duration=6;

		CMMsg msg=CMClass.getMsg(mob,null,this,CMMsg.MSG_NOISYMOVEMENT,"<S-NAME> start(s) enhancing the "+writing+" of "+target.name()+".");
		if(mob.location().okMessage(mob,msg))
		{
			mob.location().send(mob,msg);
			beneficialAffect(mob,mob,asLevel,duration);
		}
		return true;
	}
}