using System;
using System.Collections.Generic;
using System.Text;
using nft.ui.command;
using nft.ui.mainframe_old;
using nft.framework;
using nft.core.game;
using nft.core.geometry;
using nft.framework.drawing;
using Geocon = nft.core.geometry.GeometricConstants;
using System.Drawing;
using nft.core.view;
using nft.framework.plugin;

namespace nft.impl.view {
    public class ViewCommands {

        public static ICommandProcedure GetRotateRightCommand(VContributionId id){
            return new ViewRotaterR();
        }

        public static ICommandProcedure GetRotateLeftCommand(VContributionId id) {
            return new ViewRotaterL();
        }

        public static ICommandProcedure GetZoomInCommand(VContributionId id) {
            return new ZoomInCommand();
        }

        public static ICommandProcedure GetZoomOutCommand(VContributionId id) {
            return new ZoomOutCommand();
        }

        //public static ICommandProcedure GetAddRandomObjCommand(String id) {
        //    return new AddRandomObjects();
        //}
        
        //class AddRandomObjects : ICommandProcedure {
        //    static private Random rand = new Random();

        //    #region ICommandProcedure メンバ

        //    public void Execute() {
        //        IQuarterView qv = GameManager.theInstance.ActiveView;
        //        if (qv != null) {
        //            MapViewDrawer mvd = qv as MapViewDrawer;
        //            Size3D szw = mvd.TerrainMap.Size;
        //            Rect3DV rct;
        //            QuaterViewRect qvr;
        //            ITextureOld tex;
        //            Font f = new Font(mvd.TargetControl.Font.FontFamily,7.5f);
        //            Scaler sc = mvd.ViewFactor.Scaler;
        //            int uw = sc.Scale(Geocon.CellWidthPixel);
        //            int uh = sc.Scale(Geocon.CellHeightPixel);
        //            mvd.ViewObjects.Clear();
        //            Array kcarr = Enum.GetValues(typeof(KnownColor));
        //            int hmax = 0; // 地表から浮かす高さの最大
        //            for (int i = 0; i < 5; i++) {
        //                int w = Math.Min(szw.sx / 3, szw.sy / 3);
        //                int x = szw.sx / 2 + rand.Next(w) - rand.Next(w);
        //                int y = szw.sy / 2 + rand.Next(w) - rand.Next(w);
        //                int z = mvd.TerrainMap[x, y].MeanHeight + rand.Next(hmax) * Geocon.TerrainHeightStep;
        //                int wx = 2 + rand.Next(3) + rand.Next(3);
        //                int wy = 2 + rand.Next(3) + rand.Next(3);
        //                int wz = (2 + rand.Next(4) + rand.Next(4)) * Geocon.TerrainHeightStep;
        //                x -= wx / 2;
        //                y -= wy / 2;
        //                rct = new Rect3DV(x, y, z, x + wx, y + wy, z + wz);
        //                qvr = new QuaterViewRect(0, 0, wy * uw, wx * uw, wz * uh);
        //                Color col = Color.FromKnownColor((KnownColor)kcarr.GetValue(i * (hmax+1)));
        //                string s = string.Format("{0},{1}-{2},{3}", rct.VX, rct.VY, rct.VX2, rct.VY2, rct.VZ);
        //                tex = PrimitiveTextrueFactory.CreateCellBoundsTexture(qvr, f, s, col);
        //                //tex = PrimitiveTextrueFactory.CreateCell3DBoxTexture(qvr, col);
        //                mvd.ViewObjects.AddObject(tex, rct);
        //            }
        //            /*
        //            rct = new Rect3DV(0, 0, mvd.TerrainMap[0, 0].MeanHeight+1, 1, 1, Geocon.TerrainHeightStep);
        //            qvr = new QuaterViewRect(0, 0, uw, uw, uh * Geocon.TerrainHeightStep);
        //            tex = PrimitiveTextrueFactory.CreateCell3DBoxTexture(qvr, Color.Red);
        //            mvd.ViewObjects.AddObject(tex, rct);
        //             */ 
        //            //mvd.
        //            mvd.TargetControl.Invalidate();
        //        }
        //    }

        //    #endregion
        //}

        class ViewRotaterR : ITriggerCommandProcedure {
            #region ICommandProcedure メンバ

            public void Execute() {
                ViewController view = GameManager.theInstance.ActiveView;
                if (view != null) {
                    InterCardinalDirection dir = view.UpperDirection;
                    view.UpperDirection = Direction.GetRightOf(dir);
                }
            }

            public bool Enable {
                get { return true; }
            }

            public event EventHandler EnableChanged;

            #endregion

        }

        class ViewRotaterL : ITriggerCommandProcedure {
            #region ICommandProcedure メンバ

            public void Execute() {
                ViewController view = GameManager.theInstance.ActiveView;
                if (view != null) {
                    InterCardinalDirection dir = view.UpperDirection;
                    view.UpperDirection = Direction.GetLeftOf(dir);
                }
            }

            public bool Enable {
                get { return true; }
            }

            public event EventHandler EnableChanged;

            #endregion
        }

        class ZoomInCommand : ITriggerCommandProcedure {
            #region ICommandProcedure メンバ

            public void Execute() {
                //IQuarterView qv = GameManager.theInstance.ActiveView;
                //if (qv != null) {
                //    ZoomScale zs = qv.ViewFactor.Scale;
                //    qv.ViewFactor.Scale = Scaler.ZoomIn(zs);
                //}
            }

            public bool Enable {
                get { return true; }
            }

            public event EventHandler EnableChanged;

            #endregion
        }

        class ZoomOutCommand : ITriggerCommandProcedure {
            #region ICommandProcedure メンバ

            public void Execute() {
                //IQuarterView qv = GameManager.theInstance.ActiveView;
                //if (qv != null) {
                //    ZoomScale zs = qv.ViewFactor.Scale;
                //    qv.ViewFactor.Scale = Scaler.ZoomOut(zs);
                //    // Debug
                //    TerrainPolygonExporter.ExportPolygons(qv as MapViewDrawer);
                //}
            }

            public bool Enable {
                get { return true; }
            }

            public event EventHandler EnableChanged;

            #endregion
        }
    }
}
