using System;
using System.Collections.Generic;
using System.Text;
using CommonLang;
using CommonAI.Zone.Helper;

namespace CommonAI.Zone.Instance
{
    /// <summary>
    /// 代理用类
    /// </summary>
    partial class InstanceZone
    {
        private InstanceUnit mLastAddedUnit;
        private InstanceUnit mLastActivatedUnit;
        private InstanceUnit mLastRebirthUnit;
        private InstanceUnit mLastHittedUnit;
        private InstanceUnit mLastAttackUnit;
        private InstanceUnit mLastKilledUnit;

        private InstanceItem mLastCreatedInstanceItem;
        private InstanceItem mLastUnitGotInstanceItem;

        private ItemTemplate mLastUnitGotItem;
        private ItemTemplate mLastUnitLostItem;
        private ItemTemplate mLastUnitUseItem;
        private BuffTemplate mLastUnitGotBuff;

        private InstanceUnit mLastPickingItemUnit;
        private InstanceItem mLastPickingItem;
        private InstanceUnit mLastPickableUnit;

        private InstanceUnit mLastLaunchSkillUnit;
        private SkillTemplate mLastLaunchSkill;
        private SpellTemplate mLastLaunchSpell;

        private ZoneServer.SendMessageR2B mLastRecvMessageR2B;
        private ZoneServer.SendMessageB2R mLastSentMessageB2R;

        //当前场景类型
        protected CommonAI.Data.SceneType mSceneType;

        public InstanceUnit LastAddedUnit
        {
            get { return mLastAddedUnit; }
            private set { if (value != null) mLastAddedUnit = value; }
        }
        public InstanceUnit LastActivatedUnit
        {
            get { return mLastActivatedUnit; }
            private set { if (value != null) mLastActivatedUnit = value; }
        }
        public InstanceUnit LastRebirthUnit
        {
            get { return mLastRebirthUnit; }
            private set { if (value != null) mLastRebirthUnit = value; }
        }
        public InstanceUnit LastHittedUnit
        {
            get { return mLastHittedUnit; }
            private set { if (value != null) mLastHittedUnit = value; }
        }
        public InstanceUnit LastAttackUnit
        {
            get { return mLastAttackUnit; }
            private set { if (value != null) mLastAttackUnit = value; }
        }
        public InstanceUnit LastKilledUnit
        {
            get { return mLastKilledUnit; }
            private set { if (value != null) mLastKilledUnit = value; }
        }
        public InstanceItem LastCreatedInstanceItem
        {
            get { return mLastCreatedInstanceItem; }
            private set { if (value != null) mLastCreatedInstanceItem = value; }
        }
        public InstanceItem LastUnitGotInstanceItem
        {
            get { return mLastUnitGotInstanceItem; }
            private set { if (value != null) mLastUnitGotInstanceItem = value; }
        }

        public ItemTemplate LastUnitGotInventoryItem
        {
            get { return mLastUnitGotItem; }
            private set { if (value != null) mLastUnitGotItem = value; }
        }
        public ItemTemplate LastUnitLostInventoryItem
        {
            get { return mLastUnitLostItem; }
            private set { if (value != null) mLastUnitLostItem = value; }
        }
        public ItemTemplate LastUnitUseItem
        {
            get { return mLastUnitUseItem; }
            private set { if (value != null) mLastUnitUseItem = value; }
        }
        public BuffTemplate LastUnitGotBuff
        {
            get { return mLastUnitGotBuff; }
            private set { if (value != null) mLastUnitGotBuff = value; }
        }
        public InstanceUnit LastPickingItemUnit
        {
            get { return mLastPickingItemUnit; }
            private set { if (value != null) mLastPickingItemUnit = value; }
        }
        public InstanceItem LastPickingItem
        {
            get { return mLastPickingItem; }
            private set { if (value != null) mLastPickingItem = value; }
        }
        public InstanceUnit LastPickableUnit
        {
            get { return mLastPickableUnit; }
            private set { if (value != null) mLastPickableUnit = value; }
        }
       
        public InstanceUnit LastLaunchSkillUnit
        {
            get { return mLastLaunchSkillUnit; }
            private set { if (value != null) mLastLaunchSkillUnit = value; }
        }
        public SkillTemplate LastLaunchSkill
        {
            get { return mLastLaunchSkill; }
            private set { if (value != null) mLastLaunchSkill = value; }
        }
        public SpellTemplate LastLaunchSpell
        {
            get { return mLastLaunchSpell; }
            private set { if (value != null) mLastLaunchSpell = value; }
        }

        public ZoneServer.SendMessageR2B LastRecvMessageR2B
        {
            get { return mLastRecvMessageR2B; }
            private set { if (value != null) mLastRecvMessageR2B = value; }
        }
        public ZoneServer.SendMessageB2R LastSentMessageB2R
        {
            get { return mLastSentMessageB2R; }
            private set { if (value != null) mLastSentMessageB2R = value; }
        }

        private void beginEventsRecord()
        {
//             mLastAddedUnit = null;
//             mLastActivatedUnit = null;
//             mLastRebirthUnit = null;
//             mLastHittedUnit = null;
//             mLastAttackUnit = null;
//             mLastKilledUnit = null;
// 
//             mLastCreatedInstanceItem = null;
//             mLastUnitGotInstanceItem = null;
// 
//             mLastUnitGotItem = null;
//             mLastUnitLostItem = null;
//             mLastUnitUseItem = null;
//             mLastUnitGotBuff = null;
// 
//             mLastPickingItemUnit = null;
//             mLastPickingItem = null;
//             mLastPickableUnit = null;
// 
//             mLastLaunchSkillUnit = null;
//             mLastLaunchSkill = null;
//             mLastLaunchSpell = null;
// 
//             mLastRecvMessageR2B = null;
//             mLastSentMessageB2R = null;
        }


        private HashMap<int, int> mForceTotalDead = new HashMap<int, int>();

        public int GetTotalForceDead(byte force)
        {
            int ret = 0;
            mForceTotalDead.TryGetValue(force, out ret);
            return ret;
        }

        private void statisticForceDead(InstanceUnit unit)
        {
            int ret = 0;
            mForceTotalDead.TryGetValue(unit.Force, out ret);
            ret += 1;
            mForceTotalDead[unit.Force] = ret;
        }

        #region LUA_API

        public int get_total_width()
        {
            return TotalWidth;
        }
        public int get_total_height()
        {
            return TotalHeight;
        }
        public int get_grid_cell_w()
        {
            return GridCellW;
        }
        public int get_grid_cell_h()
        {
            return GridCellH;
        }
        public int get_x_count()
        {
            return XCount;
        }
        public int get_y_count()
        {
            return YCount;
        }
        public ulong get_tick()
        {
            return Tick;
        }
        public long get_pass_time_ms()
        {
            return PassTimeMS;
        }
        public Random get_random()
        {
            return RandomN;
        }

        public bool is_attribute(string key)
        {
            return IsAttribute(key);
        }
        public void set_attribute(string key, object value)
        {
            SetAttribute(key, value);
        }
        public object get_attribute(string key)
        {
            return GetAttribute(key);
        }

        public InstanceUnit get_edit_unit(string name)
        {
            return GetEditUnit(name);
        }

        public InstanceFlag get_edit_flag(string name)
        {
            return GetEditFlag(name);
        }

        public TimeTaskMS add_time_task_ms(int interval, int delay, int repeat, TickHandler handler)
        {
            return AddTimeTask(interval, delay, repeat, handler);
        }

        public TimeTaskMS add_time_delay_ms(int delay, TickHandler handler)
        {
            return AddTimeDelayMS(delay, handler);
        }

        public TimeTaskMS add_time_periodic_ms(int interval, TickHandler handler)
        {
            return AddTimePeriodicMS(interval, handler);
        }

        public void do_client_script(string filename)
        {
            DoClientScript(filename);
        }

        public bool send_script_command(string reason)
        {
            return SendScriptCommand(reason);
        }

        public bool send_game_over(int force, string reason)
        {
            return GameOver(force, reason);
        }


        public InstanceUnit add_unit(int templateID, string name, byte force, int level, float x, float y, float direction)
        {
            return AddUnit(templateID, name, force, level, x, y, direction, false);
        }

        public InstanceItem add_item(int templateID, string name, byte force, float x, float y)
        {
            ItemTemplate info = Templates.getItem(templateID);
            if (info != null)
            {
                return AddItem(info, name, x, y, CMath.RandomAngle(RandomN), force, name, null,0);
            }
            return null;
        }

        public InstanceUnit GetEditUnit(string name)
        {
            return getUnitByName(name);
        }

        public InstanceFlag GetEditFlag(string name)
        {
            return getFlag(name);
        }

        public void DoClientScript(string filename)
        {
            DoScriptEvent evt = new DoScriptEvent(filename);
            queueEvent(evt);
        }

        public bool SendScriptCommand(string reason)
        {
            queueEvent(new ScriptCommandEvent(reason));
            return true;
        }

        public bool GameOver(int force, string reason)
        {
            queueEvent(new GameOverEvent(force, reason));
            return true;
        }

        [Obsolete]
        public List<InstanceUnit> GetAllUnits()
        {
            return new List<InstanceUnit>(this.AllUnits);
        }
        public void GetAllUnits(List<InstanceUnit> ret)
        {
            ret.AddRange(this.AllUnits);
        }
        public int GetAllUnitsCount()
        {
            return this.AllUnitsCount;
        }
        [Obsolete]
        public List<InstanceUnit> GetForceUnits(byte force)
        {
            List<InstanceUnit> ret = new List<InstanceUnit>(this.AllUnitsCount);
            foreach (InstanceUnit u in this.AllUnits)
            {
                if (u.Force == force)
                {
                    ret.Add(u);
                }
            }
            return ret;
        }
        public void GetForceUnits(byte force, List<InstanceUnit> ret)
        {
            foreach (InstanceUnit u in this.AllUnits)
            {
                if (u.Force == force)
                {
                    ret.Add(u);
                }
            }
        }
        public int GetForceUnitsCount(byte force)
        {
            int Count = 0;
            foreach (InstanceUnit u in this.AllUnits)
            {
                if (u.Force == force)
                {
                    Count++;
                }
            }
            return Count;
        }

        public CommonAI.Data.SceneType SceneType
        {
            get { return mSceneType; }
        }

        #endregion
    }
}