using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace battle
{
    public class classBattle
    {
        #region "enumerated types"
        public enum enuDirections { N_ = 0, NE, SE, S_, SW, NW, _num_directions }
        public enum enuInitialUnitLocDirIndices { Red_N, Red_NE, Red_E, Red_SE, Red_S, Red_SW, Red_W, Red_NW, _num_RedInitialLocIndices, Blue }
        public enum enuUnitTypes { cavalry = 0, infantry, artillery, _num_UnitTypes }
        public enum enuUnitColors { red = 0, blue, _numUnitColors }
        public enum enuUnitRanks { lieutenant = 0, captain, major, colonel, general, field_marshall, _numUnitRanks }
        public enum enuEvenOdd { even = 0, odd }
        public enum enuTerrainTypes { grass, forest, road, water, mountain, castle, _numTerrainTypes }
        public enum enuUnitMoveMapElement { move, no_move, target }
        public enum enuAttackTypes { normal, charge, barrage, _numAttackTypes }
        public enum enuColorControlledBy { player, computer }
        public enum enuTerrainImageSetting { normal, shaded, blackened }
        
        #endregion

        #region "structures"
        //[Serializable]
        //public class classCartesian
        //{
        //    public int x;
        //    public int y;
        //}

        [Serializable]
        public class Point_Array { public Point[] pt;}
        [Serializable]
        public class classRedLaunchLocations { public Point_Array[] dir;}

        [Serializable]
        public class classMaxMin
        {
            public double Max;
            public double Min;
        }

        [Serializable]
        public class classTerrain
        {
            public enuTerrainTypes type;
            //public System.Windows.Forms.PictureBox pic;
            public classBattleUnit unit;
        }

        [Serializable]
        public class classBattleFieldFileInfo
        {
            public Size mapsize;
        }


        #endregion

        #region "constants"
        public const int intMaxInvasionForce = 10;
        #endregion

        public System.Windows.Forms.Form mainForm;

        public classBattle(System.Windows.Forms.Form mainForm_local)
        {
            mainForm = mainForm_local;
        }

        public void enQUnitToBattleField(ref classBattleField battlefield, ref classBattleUnit unit)
        {
            // unit.battlefield = battlefield;
            battlefield.enQUnit(ref unit);
        }

        public void testNeedToRebuildCmbLB_Maps()
        {}

        public classBattleField getBattleField()
        {
            return new classBattleField(this, mainForm);
        }

        public Point getPos(int intX, int intY)
        {
            Point udrRetVal = new Point();
            udrRetVal.X = intX;
            udrRetVal.Y = intY;
            return udrRetVal;
        }

    public    classBattle.enuDirections turnLeft(classBattle.enuDirections dir)
        { return (classBattle.enuDirections)(((int)dir - 1 + (int)classBattle.enuDirections._num_directions) % (int)classBattle.enuDirections._num_directions); }

       public classBattle.enuDirections turnRight(classBattle.enuDirections dir)
        { return (classBattle.enuDirections)(((int)dir + 1) % (int)classBattle.enuDirections._num_directions); }


        public Image getTerrainImage(classBattle.enuTerrainTypes terrainType) { return getTerrainImage(terrainType, classBattle.enuTerrainImageSetting.normal); }
        public Image getTerrainImage(classBattle.enuTerrainTypes terrainType, classBattle.enuTerrainImageSetting setting)
        {
            switch (setting)
            {
                case classBattle.enuTerrainImageSetting.normal:
                    switch (terrainType)
                    {
                        case classBattle.enuTerrainTypes.grass:
                            return Battlefield_Simulator_2019.Resources.terrain_grass;
                                
                        case classBattle.enuTerrainTypes.forest:
                            return Battlefield_Simulator_2019.Resources.terrain_forest;

                        case classBattle.enuTerrainTypes.road:
                            return Battlefield_Simulator_2019.Resources.terrain_road;

                        case classBattle.enuTerrainTypes.water:
                            return Battlefield_Simulator_2019.Resources.terrain_water;

                        case classBattle.enuTerrainTypes.mountain:
                            return Battlefield_Simulator_2019.Resources.terrain_mountain;

                        case classBattle.enuTerrainTypes.castle:
                            return Battlefield_Simulator_2019.Resources.terrain_castle;
                    }
                    break;

                case classBattle.enuTerrainImageSetting.blackened:
                    switch (terrainType)
                    {
                        case classBattle.enuTerrainTypes.grass:
                            return Battlefield_Simulator_2019.Resources.terrain_grass_target;

                        case classBattle.enuTerrainTypes.forest:
                            return Battlefield_Simulator_2019.Resources.terrain_forest_target;

                        case classBattle.enuTerrainTypes.road:
                            return Battlefield_Simulator_2019.Resources.terrain_road_target;

                        case classBattle.enuTerrainTypes.water:
                            return Battlefield_Simulator_2019.Resources.terrain_water_target;

                        case classBattle.enuTerrainTypes.mountain:
                            return Battlefield_Simulator_2019.Resources.terrain_mountain_target;

                        case classBattle.enuTerrainTypes.castle:
                            return Battlefield_Simulator_2019.Resources.terrain_castle_target;
                    }
                    break;

                case classBattle.enuTerrainImageSetting.shaded:
                    switch (terrainType)
                    {
                        case classBattle.enuTerrainTypes.grass:
                            return Battlefield_Simulator_2019.Resources.terrain_grass_shaded;

                        case classBattle.enuTerrainTypes.forest:
                            return Battlefield_Simulator_2019.Resources.terrain_forest_shaded;

                        case classBattle.enuTerrainTypes.road:
                            return Battlefield_Simulator_2019.Resources.terrain_road_shaded;

                        case classBattle.enuTerrainTypes.water:
                            return Battlefield_Simulator_2019.Resources.terrain_water_shaded;

                        case classBattle.enuTerrainTypes.mountain:
                            return Battlefield_Simulator_2019.Resources.terrain_mountain_shaded;

                        case classBattle.enuTerrainTypes.castle:
                            return Battlefield_Simulator_2019.Resources.terrain_castle_shaded;
                    }
                    break;
            }
            return Battlefield_Simulator_2019.Resources.terrain_grass;
        }

        public classBattleUnit getUnit(classBattle.enuUnitTypes typeUnit,
                                       classBattle.enuUnitColors color,
                                       classBattle.enuUnitRanks rank)
        {
            classBattleUnit RetVal = new classBattleUnit(typeUnit, color, rank);
            return RetVal;
        }
       
        public double cleanAngle(ref double angle)
        {
            while (angle < 0)
                angle += 2 * Math.PI;

            while (angle > 2 * Math.PI)
                angle -= 2 * Math.PI;

            return angle;
        }
        public double arcTan(double dX, double dY)
        {
            double dblAngle = 0;
            if (dX == 0)
                if (dY < 0)
                    dblAngle = 3.0 * Math.PI / 2.0;
                else
                    dblAngle = Math.PI / 2.0;
            else
            {
                dblAngle = Math.Atan((double)dY / (double)dX);
                //  Q2 Q1
                //  Q3 Q4
                if (dX < 0)
                {
                    if (dY < 0) // Q3
                        dblAngle = Math.PI + dblAngle;
                    else              // Q2
                        dblAngle = Math.PI + dblAngle;
                }
                else if (dX > 0)
                {
                    if (dY < 0) // Q4
                        dblAngle = 2.0 * Math.PI + dblAngle;
                    else              // Q1
                        ;
                }
            }
            cleanAngle(ref dblAngle);
            return dblAngle;
        }

       public enuUnitTypes getTypeFromString(string strType)
        {
            if (strType != null)
            {
                strType = strType.ToUpper();
                for (enuUnitTypes typeCounter = 0; typeCounter < enuUnitTypes._num_UnitTypes; typeCounter++)
                    if (typeCounter.ToString().ToUpper().CompareTo(strType) == 0)
                        return typeCounter;
            }
            return enuUnitTypes._num_UnitTypes;
        }

        public enuUnitRanks getRankFromString(string strRank)
        {
            if (strRank != null)
            {
                strRank = strRank.ToUpper();
                for (enuUnitRanks rankCounter = 0; rankCounter < enuUnitRanks._numUnitRanks; rankCounter++)
                    if (rankCounter.ToString().ToUpper().CompareTo(strRank) == 0)
                        return rankCounter;
            }
            return enuUnitRanks._numUnitRanks;
        }
        
        public Bitmap getFontBitMap(string strText)
        {
            {
                //Image[] img = new Image[strText.Length];
                //int intHeight =0;
                //int intWidth = 0;
                //for (int intCharCounter = 0; intCharCounter < strText.Length; intCharCounter++)
                //{
                //    img[intCharCounter] = getFontImage(strText[intCharCounter]);
                //    if (intHeight < img[intCharCounter].Height)
                //        intHeight = img[intCharCounter].Height;
                //    intWidth += img[intCharCounter].Width;
                //}
                //Bitmap bmpRetVal = new Bitmap(intWidth, intHeight);

                //int intLeft = 0;
                //using (Graphics g = Graphics.FromImage(bmpRetVal))
                //{
                //    for (int intImageCounter = 0; intImageCounter < img.Length; intImageCounter++)
                //    {
                //        g.DrawImage(img[intImageCounter], intLeft, intHeight - img[intImageCounter].Height);
                //        intLeft += img[intImageCounter].Width;
                //    }
                //}
            }

            Font fnt = new Font("Arial", 72, FontStyle.Bold);
            Size szText = System.Windows.Forms.TextRenderer.MeasureText(strText, fnt);

            Bitmap bmpRetVal = new Bitmap(szText.Width, szText.Height);
            using (Graphics g = Graphics.FromImage(bmpRetVal))
            {
                g.FillRectangle(Brushes.White, new RectangleF(0, 0, szText.Width, szText.Height));
                g.DrawString(strText, fnt, Brushes.Black, new Point(0, 0));
            }

            return bmpRetVal;
        }

        public Image getFontImage(char c)
        {
            switch (c)
            {
                case '0':
                    return Battlefield_Simulator_2019.Resources.Font_0;
                case '1':
                    return Battlefield_Simulator_2019.Resources.Font_1;
                case '2':
                    return Battlefield_Simulator_2019.Resources.Font_2;
                case '3':
                    return Battlefield_Simulator_2019.Resources.Font_3;
                case '4':
                    return Battlefield_Simulator_2019.Resources.Font_4;
                case '5':
                    return Battlefield_Simulator_2019.Resources.Font_5;
                case '6':
                    return Battlefield_Simulator_2019.Resources.Font_6;
                case '7':
                    return Battlefield_Simulator_2019.Resources.Font_7;
                case '8':
                    return Battlefield_Simulator_2019.Resources.Font_8;
                case '9':
                    return Battlefield_Simulator_2019.Resources.Font_9;
                case '/':
                    return Battlefield_Simulator_2019.Resources.Font_RightSlash;
                default:
                    return Battlefield_Simulator_2019.Resources.Font_0;
            }
        }
    }

}