using System;
using System.Collections.Generic;

using System.Text;

using System.Drawing;
using System.Drawing.Drawing2D;

using CommonAI.RTS;
using CommonLang.Vector;
using CommonAI.Zone;
using CommonAI.Zone.Instance;
using CommonAI.RTS.Manhattan;
using CommonAI.Zone.ZoneEditor;
using System.Windows.Forms;
using CommonAIEditor;
using CommonLang;
using System.Collections;
using GameEditorPlugin.Win32;
using GameEditorPlugin;
using CommonAIClient.Client;
using CommonAI.ZoneClient;
using CommonLang.Protocol;
using CommonLang.IO;
using CommonAI.Zone.Helper;
using CommonAI.Zone.Replay;
using GameEditorPlugin.Win32.BattleClient;

namespace GameEditorPlugin.Win32.Runtime
{
    public class RuntimeGameLocal : DisplayBattleLocal<BattleLocalPlay>
    {
        private EditorScene zone;

        public EditorTemplates Templates { get; private set; }

        public RuntimeGameLocal(EditorTemplates templates, int scene_id, BattleRecorder recorder)
        {
            EnvironmentVar.ALWAYS_SYNC_ENVIRONMENT_VAR = true;

            this.ShowHP = true;
            this.ShowLog = true;

            this.Templates = templates;

            SceneData scene = this.Templates.LoadScene(scene_id);

            base.InitClient(new BattleLocalPlay(Templates, scene));
            this.Battle.SetRecoder(recorder);
            this.Battle.Layer.MessageReceived += this.processEvent;

            this.zone = Battle.Zone;
            ZoneRegion startRegion = zone.GetEditStartRegion(0);
            if (startRegion != null)
            {
                initStartRegion(zone.Data.GetStartRegions().Get(0));
                return;
            }
            foreach (CommonAI.Zone.ZoneEditor.RegionData rg in zone.Data.GetStartRegionsList())
            {
                initStartRegion(rg);
                return;
            }

            MessageBox.Show("没有玩家出身点!!!");
        }

        public override void Dispose()
        {
            base.Dispose();
        }

        private void initStartRegion(CommonAI.Zone.ZoneEditor.RegionData startRegion)
        {
            PlayerStartAbilityData tgd = startRegion.GetAbilityOf<PlayerStartAbilityData>();
            int actorTemplateID = tgd.TestActorTemplateID;
            UnitInfo info = Templates.Templates.getUnit(actorTemplateID);
            if (info != null)
            {
                info = info.Clone() as UnitInfo;
                info.UType = UnitInfo.UnitType.TYPE_PLAYER;
                InstancePlayer actor = zone.AddUnit(info, "ACTOR", (byte)tgd.START_Force, tgd.TestActorLevel, startRegion.X, startRegion.Y, 0) as InstancePlayer;
                if (actor != null)
                {
                    var loc = actor.GenLockActorEvent(actor.Name, zone.Data.ClientSyncRange, zone.Data.ClientSyncRange, zone.UpdateIntervalMS);
                    //zone.queueEvent(loc);
                    base.Layer.ProcessMessage(loc);
                }
                this.setCamera(actor.X, actor.Y);
            }
            else
            {
                MessageBox.Show("主角资源没有 : " + actorTemplateID);
            }
            return;
        }

        public void update(int intervalMS, float turbo)
        {
            int totalMS = (int)(intervalMS * turbo);
            while (totalMS > 0)
            {
                int tick = Math.Min(totalMS, intervalMS);
                base.update(tick);
                totalMS -= intervalMS;
            }
            base.update_flush();
        }

        private void processEvent(ZoneLayer layer, IMessage e)
        {
            if (e is GameOverEvent)
            {
                GameOverEvent over = e as GameOverEvent;
                MessageBox.Show(over.message, e.GetType().Name);
            }
            else if (e is TestMessageBox)
            {
                TestMessageBox over = e as TestMessageBox;
                MessageBox.Show(over.msg, e.GetType().Name);
            }
            else if (e is ServerExceptionB2C)
            {
                ServerExceptionB2C over = e as ServerExceptionB2C;
                MessageBox.Show(over.Message + "\n" + over.StackTrace, e.GetType().Name);
            }
            else if (e is ScriptCommandEvent)
            {
                ScriptCommandEvent over = e as ScriptCommandEvent;
                MessageBox.Show(over.message, e.GetType().Name);
            }
        }

    }
}