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

namespace nft.impl.view_old {
    public class ViewController {

        public static ICommandEntity_Old GetRotateRightCommand(String id){
            return new ViewRotaterR();
        }

        public static ICommandEntity_Old GetRotateLeftCommand(String id) {
            return new ViewRotaterL();
        }

        public static ICommandEntity_Old GetZoomInCommand(String id) {
            return new ZoomInCommand();
        }

        public static ICommandEntity_Old GetZoomOutCommand(String id) {
            return new ZoomOutCommand();
        }

        public static ICommandEntity_Old GetAddRandomObjCommand(String id) {
            return new AddRandomObjects();
        }


        class AddRandomObjects : ICommandEntity_Old {
            static private Random rand = new Random();

            #region ICommandEntity_Old メンバ

            public void CommandExecuted(CommandUI_Old cmdUI, object sender) {
                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 : ICommandEntity_Old {
            #region ICommandEntity_Old メンバ

            public void CommandExecuted(CommandUI_Old cmdUI, object sender) {
                IQuarterView qv = GameManager.theInstance.ActiveView;
                if (qv != null) {
                    InterCardinalDirection dir = qv.ViewFactor.ViewDirection;
                    qv.ViewFactor.ViewDirection = Direction.GetLeftOf(dir);
                }
            }

            #endregion
        }

        class ViewRotaterL : ICommandEntity_Old {
            #region ICommandEntity_Old メンバ

            public void CommandExecuted(CommandUI_Old cmdUI, object sender) {
                IQuarterView qv = GameManager.theInstance.ActiveView;
                if (qv != null) {
                    InterCardinalDirection dir = qv.ViewFactor.ViewDirection;
                    qv.ViewFactor.ViewDirection = Direction.GetRightOf(dir);
                }
            }

            #endregion
        }

        class ZoomInCommand : ICommandEntity_Old {
            #region ICommandEntity_Old メンバ

            public void CommandExecuted(CommandUI_Old cmdUI, object sender) {
                IQuarterView qv = GameManager.theInstance.ActiveView;
                if (qv != null) {
                    ZoomScale zs = qv.ViewFactor.Scale;
                    qv.ViewFactor.Scale = Scaler.ZoomIn(zs);
                }
            }

            #endregion
        }

        class ZoomOutCommand : ICommandEntity_Old {
            #region ICommandEntity_Old メンバ

            public void CommandExecuted(CommandUI_Old cmdUI, object sender) {
                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);
                }
            }

            #endregion
        }
    }
}
