#include "x2_inc_switches"

// this function ripped from contest of champions
void ApplyFlagEffect( object oPlayer )
{
    int nTeam, nVisEffect;
    effect eFlagEffect;

    nTeam = GetLocalInt(oPlayer, "m_nTeam");


        nVisEffect = VFX_DUR_FLAG_GOLD;
    if(nTeam == 1)
        nVisEffect = VFX_DUR_FLAG_BLUE;
    if(nTeam == 2)
        nVisEffect = VFX_DUR_FLAG_RED;


//    }
//    else if ( nTeam == TEAM_RED )
//    {
//        nVisEffect = VFX_DUR_FLAG_RED;
//    }
//    else if ( nTeam == TEAM_GOLD )
//    {
//        nVisEffect = VFX_DUR_FLAG_PURPLE;
//    }
//    else if ( nTeam == TEAM_PURPLE )
//    {
//        nVisEffect = VFX_DUR_FLAG_GOLD;
//    }

    eFlagEffect = EffectVisualEffect(nVisEffect);
    eFlagEffect = SupernaturalEffect(eFlagEffect);

    ApplyEffectToObject(DURATION_TYPE_PERMANENT,eFlagEffect,oPlayer);
    PlaySound("as_mg_telepout1");
}

void RemoveAllEffects( object oPlayer )
{
    effect eEffect;
    eEffect = GetFirstEffect(oPlayer);

    while ( GetIsEffectValid(eEffect) == TRUE )
    {
        if ( GetEffectDurationType(eEffect) == DURATION_TYPE_TEMPORARY ||
             GetEffectDurationType(eEffect) == DURATION_TYPE_PERMANENT )
        {
            RemoveEffect(oPlayer,eEffect);
        }

        eEffect = GetNextEffect(oPlayer);
    }
}

void main()
{

	object oItem = GetModuleItemAcquired();
	object oPC = GetItemPossessor(oItem);
	int nYes;
	string sItemTag = GetTag(oItem);
    // * Generic Item Script Execution Code
    // * If MODULE_SWITCH_EXECUTE_TAGBASED_SCRIPTS is set to TRUE on the module,
    // * it will execute a script that has the same name as the item's tag
    // * inside this script you can manage scripts for all events by checking against
    // * GetUserDefinedItemEventNumber(). See x2_it_example.nss
    if (GetModuleSwitchValue(MODULE_SWITCH_ENABLE_TAGBASED_SCRIPTS) == TRUE)
    {
		SetUserDefinedItemEventNumber(X2_ITEM_EVENT_ACQUIRE);
        int nRet =   ExecuteScriptAndReturnInt(GetUserDefinedItemEventScriptName(oItem),OBJECT_SELF);
        if (nRet == X2_EXECUTE_SCRIPT_END)
        {
           return;
        }
	}

  // check to see if the acquired item is a "mark"
  if(sItemTag == "FalseMark")
    nYes = 1;
  if(sItemTag == "MarkofOron")
    nYes = 1;
  if(sItemTag == "MarkofGamlee")
    nYes = 1;
  if(sItemTag == "MarkofZool")
    nYes = 1;
  if(sItemTag == "MarkofMaro")
    nYes = 1;


  // check for the presence of magic bags

  int nBagYes;
  object oInvItem = GetFirstItemInInventory(oPC);
  while (GetIsObjectValid(oInvItem) != 0)
  {

    if(GetName(oInvItem) == "Magic Bag") {
      nBagYes = 1;
    }

    oInvItem = GetNextItemInInventory(oPC);

  }

  // if acquiring a mark and have magic bag in inv, destroy mark
  if(nBagYes == 1 && nYes == 1) {
    DestroyObject(oItem);
    return;
  }

  // if acquiring magic bag, check for presence and destroy marks

  if(GetName(oItem) == "Magic Bag") {

    object oInv2 = GetFirstItemInInventory(oPC);
    while (GetIsObjectValid(oInv2) != 0) {
      if(GetTag(oInv2) == "FalseMark") {
        DestroyObject(oInv2);
        RemoveAllEffects(oPC);
      }
      if(GetTag(oInv2) == "MarkofGamlee") {
        DestroyObject(oInv2);
        RemoveAllEffects(oPC);
      }
      if(GetTag(oInv2) == "MarkofOron") {
        DestroyObject(oInv2);
        RemoveAllEffects(oPC);
      }
      if(GetTag(oInv2) == "MarkofMaro") {
        DestroyObject(oInv2);
        RemoveAllEffects(oPC);
      }
      if(GetTag(oInv2) == "MarkofZool") {
        DestroyObject(oInv2);
        RemoveAllEffects(oPC);
      }
      oInv2 = GetNextItemInInventory(oPC);
    }
  }


  // if mark, apply the flag effect to the player
  if(nYes == 1) {

    if(oPC == OBJECT_INVALID)
      return;

    AssignCommand(oPC, DelayCommand(3.0, ApplyFlagEffect(oPC)));

  }


}
