using CommonAI.RTS;
using CommonAI.Zone;
using CommonAI.Zone.Formula;
using CommonAI.Zone.Helper;
using CommonAI.Zone.Instance;
using CommonLang;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XmdsCommonServer.Plugin.Units
{
    public class XmdsInstanceNPC : InstanceGuard
    {
        private bool mIsBattleable;
        public override bool IsNature { get { return !this.mIsBattleable; } }
        public override bool IsActive { get { return this.mIsBattleable; } }
        public override bool IsAttackable { get { return this.mIsBattleable; } }

        private float mGuardRangeLimit;
        private float mGuardRange;

        public XmdsInstanceNPC(InstanceZone zone, UnitInfo info, string name, int force, int level)
            : base(zone, info, name, force, level)
        {
            //该类型,单位无法被攻击.
            this.mIsBattleable = info.UType != UnitInfo.UnitType.TYPE_NEUTRALITY && info.UType != UnitInfo.UnitType.TYPE_NPC;

            //阵营为0无法被攻击.
            if (force == 0)
            {
                this.mIsBattleable = false;
            }

            mGuardRangeLimit = info.GuardRangeLimit;
            mGuardRange = info.GuardRange;
        }


        protected override void updateBackToOrgin()
        {
            if (mBackToPosition != null)
            {
                if (mBackToPosition.IsDone)
                {
                    mBackToPosition = null;
                }
            }
            if (mOrginPosition != null && mGuardRangeLimit > mGuardRange)
            {
				//if (mCheckInGuardLimit.Update(Parent.UpdateIntervalMS))
				if (mNextCheckGuardLimit.IsTrigger())
				{
					if (!CMath.includeRoundPoint(X, Y, mGuardRangeLimit, mOrginPosition.X, mOrginPosition.Y))
                    {
                        backToOrgin();
                    }
                    else if (mTracingTarget != null)
                    {
                        if (!mTracingTarget.IsActive || !CMath.intersectRound(
                            X, Y, mGuardRangeLimit,
                            mTracingTarget.TargetUnit.X,
                            mTracingTarget.TargetUnit.Y,
                            mTracingTarget.TargetUnit.BodyHitSize))
                        {
                            backToOrgin();
                            return;
                        }
                    }
                }
            }
        }
        public void SetGuardRange(float r)
        {
            if (mViewTrigger is ViewTriggerRoundBody)
            {
                mGuardRange = r;
                (mViewTrigger as ViewTriggerRoundBody).SetLookRange(r);
            }
        }


        protected override void onNewStateBeginChange(State old_state, ref State new_state)
        {
            if (new_state is StateBackToPosition)
            {
                //回血.
                this.AddHP(this.MaxHP, null);
                this.AddMP(this.MaxMP, null);
				this.Virtual.StartRecoverMP(true);

				//去BUFF.
				this.clearBuffs();

            }

            base.onNewStateBeginChange(old_state, ref new_state);
        }

        public override void InitSkills(LaunchSkill baseSkill, params LaunchSkill[] skills)
        {
            if (this.Virtual == null ||
               (this.Virtual as XmdsVirtual).IsFinishSkillInit() == false)
            {
                return;
            }

            base.InitSkills(baseSkill, skills);
        }

        public override bool tryLaunchRandomSkillAndCancelCurrentSkill(InstanceUnit target, bool autoFocusNearTarget = false)
        {
            //自动战斗不允许中断当前技能施放.
            return false;
        }
    }
}