using System;
using System.Collections.Generic;
using System.Linq;
using Library;
using Library.SystemModels;
using Server.DBModels;
using Server.Envir;
using S = Library.Network.ServerPackets;

namespace Server.Models.Monsters
{
    public class ArchLichTaedu : MonsterObject
    {
        public int MaxStage = 7;
        public int Stage;

        public int MinSpawn = 20;
        public int RandomSpawn = 5;


        public ArchLichTaedu()
        {
            AvoidFireWall = false;
            MaxMinions = 50;
        }


        protected override void OnSpawned()
        {
            base.OnSpawned();

            Stage = MaxStage;
        }
        
        public override void Process()
        {
            base.Process();

            if (Dead) return;


            if (CurrentHP * MaxStage / Stats[Stat.Health] >= Stage || Stage <= 0) return;

            Stage--;

            ActionTime += TimeSpan.FromSeconds(1);

            Broadcast(new S.ObjectShow { ObjectID = ObjectID, Direction = Direction, Location = CurrentLocation });

            SpawnMinions(MinSpawn, RandomSpawn, Target);
        }
        

        public override void ProcessTarget()
        {
            if (Target == null) return;

            if (!InAttackRange())
            {
                if (CanAttack)
                {
                    if (SEnvir.Random.Next(2) == 0)
                        RangeAttack();
                }


                if (CurrentLocation == Target.CurrentLocation)
                {
                    MirDirection direction = (MirDirection)SEnvir.Random.Next(8);
                    int rotation = SEnvir.Random.Next(2) == 0 ? 1 : -1;

                    for (int d = 0; d < 8; d++)
                    {
                        if (Walk(direction)) break;

                        direction = Functions.ShiftDirection(direction, rotation);
                    }
                }
                else
                    MoveTo(Target.CurrentLocation);
            }

            if (!CanAttack) return;

            if (SEnvir.Random.Next(5) > 0)
            {
                if (InAttackRange())
                    Attack();
            }
            else RangeAttack();
        }

        public void RangeAttack()
        {
            Direction = Functions.DirectionFromPoint(CurrentLocation, Target.CurrentLocation);

            UpdateAttackTime();

            Broadcast(new S.ObjectRangeAttack { ObjectID = ObjectID, Direction = Direction, Location = CurrentLocation, Targets = new List<uint> { Target.ObjectID } });

            ActionList.Add(new DelayedAction(
                               SEnvir.Now.AddMilliseconds(400),
                               ActionType.DelayAttack,
                               Target,
                               GetDC(),
                               AttackElement));
        }
    }
}
