/*
 * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/>
 * Copyright (C) 2006-2009 ScriptDev2 <https://scriptdev2.svn.sourceforge.net/>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "the_eye.h"

enum Spells
{
    SPELL_COUNTERCHARGE    = 35035,
    SPELL_KNOCKAWAY        = 22893,
};

struct npc_crystalcore_devastator : public ScriptedAI
{
    explicit npc_crystalcore_devastator(Creature* creature) : ScriptedAI(creature)
    {
        Initialize();
    }

    void Initialize()
    {
        Countercharge_Timer = 9000;
        Knockaway_Timer = 25000;
    }

    uint32 Knockaway_Timer;
    uint32 Countercharge_Timer;

    void Reset() override
    {
        Initialize();
    }

    void EnterCombat(Unit* /*who*/) override
    {
    }

    void UpdateAI(uint32 diff) override
    {
        if (!UpdateVictim())
            return;

        //Check if we have a current target
        //Knockaway_Timer
        if (Knockaway_Timer <= diff)
        {
            DoCastVictim(SPELL_KNOCKAWAY, true);

            // current aggro target is knocked away pick new target
            Unit* target = SelectTarget(SELECT_TARGET_TOPAGGRO, 0);

            if (!target || target == me->getVictim())
                target = SelectTarget(SELECT_TARGET_TOPAGGRO, 1);

            if (target)
                me->TauntApply(target);

            Knockaway_Timer = 23000;
        }
        else
            Knockaway_Timer -= diff;

        //Countercharge_Timer
        if (Countercharge_Timer <= diff)
        {
            DoCast(me, SPELL_COUNTERCHARGE);
            Countercharge_Timer = 45000;
        }
        else
            Countercharge_Timer -= diff;

        DoMeleeAttackIfReady();
    }
};
void AddSC_the_eye()
{
    RegisterCreatureAI(npc_crystalcore_devastator);
}

