using Otter;
using SuperRefluxPowergridDefense_.entities;
using System;
using TiledSharp;

namespace SuperRefluxPowergridDefense_.Scenes
{
    class LevelOne : Scene
    {
        public const int _width = Global.MW * 3;
        public const int _height = Global.MW * 2;

        public TmxMap tmxMap = new TmxMap(Assets.LEVELONE);
        public GridCollider grid = null;

        public Tilemap layerOne = new Tilemap(Assets.TILES, _width, _height, Global.GRID_WIDTH, Global.GRID_HEIGHT);
        public Tilemap layerTwo = new Tilemap(Assets.TILES, _width, _height, Global.GRID_WIDTH, Global.GRID_HEIGHT);
        public Tilemap layerThree = new Tilemap(Assets.TILES, _width, _height, Global.GRID_WIDTH, Global.GRID_HEIGHT);
        public Tilemap layerFour = new Tilemap(Assets.TILES, _width, _height, Global.GRID_WIDTH, Global.GRID_HEIGHT);

        private int masterCountRate = 1;
        private int masterTimer = 0;
        private int numberOfReleasedGirls = 20;
        private bool waveOne = true;
        private bool waveTwo = false;
        private bool waveThree = false;
        private bool waveFour = false;
        private bool waveReset = false;

        public LevelOne()
        {
            #region MapCreation
            grid = new GridCollider(_width, _height, Global.GRID_WIDTH, Global.GRID_HEIGHT, (int)Global.Type.SOLID);
            Entity gridEntity = new Entity(0, 0, null, grid);
            Add(gridEntity);

            layerOne.DefaultLayerName = tmxMap.Layers[3].Name;
            layerTwo.DefaultLayerName = tmxMap.Layers[2].Name;
            layerThree.DefaultLayerName = tmxMap.Layers[1].Name;
            layerFour.DefaultLayerName = tmxMap.Layers[0].Name;

            for (int z = 0; z < tmxMap.Layers.Count; z++)
            {
                TmxLayer layer = tmxMap.Layers[z];

                if (layer.Name == "Layer 1")
                    layerOne.AddLayer(layer.Name, tmxMap.Layers.Count - z);
                else if (layer.Name == "Layer 2")
                    layerTwo.AddLayer(layer.Name, tmxMap.Layers.Count - z);
                else if (layer.Name == "Layer 3")
                    layerThree.AddLayer(layer.Name, tmxMap.Layers.Count - z);
                else
                    layerFour.AddLayer(layer.Name, tmxMap.Layers.Count - z);

                for (int x = 0; x < tmxMap.Width; x++)
                {
                    for (int y = 0; y < tmxMap.Height; y++)
                    {
                        int i = y * tmxMap.Width + x;
                        if (layer.Name == "Layer 1")//walls
                        {
                            if (layer.Tiles[i].Gid != 0)
                                layerOne.SetTile(x, y, layer.Tiles[i].Gid - 1, layer.Name);
                            if (layer.Tiles[i].Gid != 0)
                                grid.SetTile(x, y, true);
                        }
                        else if (layer.Name == "Layer 2")//main tiles
                        {
                            if (layer.Tiles[i].Gid != 0)
                                layerTwo.SetTile(x, y, layer.Tiles[i].Gid - 1, layer.Name);
                        }
                        else if (layer.Name == "Layer 3")//used to be water. haha
                        {
                            if (layer.Tiles[i].Gid != 0)
                                layerThree.SetTile(x, y, layer.Tiles[i].Gid - 1, layer.Name);
                        }
                        else//bottom layer?
                            layerFour.SetTile(x, y, layer.Tiles[i].Gid - 1, layer.Name);
                    }
                }
            }
            #endregion

            AddGraphic(layerFour);
            AddGraphic(layerThree);
            AddGraphic(layerTwo);
            AddGraphic(layerOne);

            #region AddPlayer(s)
            if (Global.NumberOfPlayers == 1)
            {
                Global.player = new Player(Global.MW + 38, Global.MH + 38);
                Add(Global.player);
            }
            //else if (Global.NumberOfPlayers == 2)
            //{
            //    Global.player = new Player(Global.MW, Global.MH - 32);
            //    Add(Global.player);
            //Global.player2 = new Player2(Global.MW * 3 - 32, Global.MH - 32);
            //Add(Global.player2);
            //    System.Console.WriteLine("player two will go here.");
            //}
            #endregion
            #region Objects
            Add(new NoSound(0, 0));
            #region Teslas and generators
            //middle base
            Add(new Generator(Global.MW - 60, Global.MH + 30));
            Add(new Generator(Global.MW + 60, Global.MH + 30));
            Add(new Tesla(Global.MW + 20, Global.MH - 40));
            Add(new Tesla(Global.MW - 40, Global.MH - 40));
            //right bases
            Add(new Tesla(998, 562));
            Add(new Tesla(1254, 482));
            Add(new Tesla(1206, 630));
            Add(new Generator(1170, 594));
            Add(new Generator(1174, 522));
            //right north
            Add(new Tesla(958, 294));
            Add(new Tesla(1142, 134));
            Add(new Tesla(1226, 150));
            Add(new Tesla(1198, 196));
            Add(new Generator(1182, 134));
            Add(new Generator(1138, 202));
            Add(new Generator(886, 358));
            //north
            Add(new Tesla(742, 270));
            Add(new Tesla(770, 102));
            Add(new Tesla(710, 62));
            Add(new Generator(766, 186));
            Add(new Generator(722, 122));
            //left north
            Add(new Tesla(498, 262));
            Add(new Tesla(358, 70));
            Add(new Tesla(326, 138));
            Add(new Generator(606, 314));
            Add(new Generator(410, 194));
            //left
            Add(new Tesla(414, 378));
            Add(new Tesla(210, 298));
            Add(new Tesla(198, 378));
            Add(new Generator(306, 402));
            Add(new Generator(550, 414));
            //left bottom
            Add(new Tesla(510, 618));
            Add(new Tesla(226, 690));
            Add(new Tesla(214, 790));
            Add(new Generator(262, 718));
            Add(new Generator(262, 794));
            Add(new Generator(598, 606));
            //bottom base
            Add(new Generator(690, 778));
            Add(new Generator(800, 778));
            Add(new Generator(746, 778));
            Add(new Generator(858, 778));
            Add(new Tesla(760, 844));
            Add(new Tesla(702, 844));
            Add(new Tesla(634, 846));
            Add(new Tesla(850, 846));
            #endregion
            Add(new entities.Grid(Global.MW, Global.MH + 200));
            Add(new PowerCell(Global.MW - 60, Global.MH + 222));
            Add(new BatteryCell(Global.MW + 23, Global.MH + 222));
            for (var i = 0; i < 100; i++)//initial Fuel
            {
                Add(new Fuel(Global.MW + 150, Global.MH + 200));
            }
            Add(new TeslaSpeedMachine(Global.MW + 5, Global.MH + 5));//middle for this is fine
            #endregion
            #region Walls
            //bottom section
            Add(new WallLeft(570, 870));
            Add(new WallDown(600, 900));
            Add(new WallDown(700, 900));
            Add(new WallDown(800, 900));
            Add(new WallDown(900, 900));
            Add(new WallRight(930, 870));
            //right side
            Add(new WallRight(930, 770));
            Add(new WallDown(960, 740));
            Add(new WallDown(1060, 740));
            Add(new WallDown(1160, 740));
            Add(new WallDown(1260, 740));
            //north right
            Add(new WallRight(1290, 710));
            Add(new WallRight(1290, 610));
            Add(new WallRight(1290, 510));
            Add(new WallRight(1290, 410));
            Add(new WallRight(1290, 310));
            Add(new WallUp(1260, 380));
            Add(new WallUp(1160, 380));
            Add(new WallUp(1060, 380));
            Add(new WallRight(1290, 210));
            Add(new WallRight(1290, 110));
            Add(new WallUp(1260, 80));
            Add(new WallUp(1160, 80));
            Add(new WallUp(1060, 80));
            Add(new WallUp(960, 80));
            //north
            Add(new WallRight(930, 70));
            Add(new WallRight(930, 170));
            Add(new WallRight(930, 270));
            Add(new WallUp(900, 40));
            Add(new WallUp(800, 40));
            Add(new WallUp(700, 40));
            Add(new WallUp(600, 40));
            //north left
            Add(new WallLeft(570, 70));
            Add(new WallLeft(570, 170));
            Add(new WallUp(500, 40));
            Add(new WallUp(400, 40));
            Add(new WallUp(300, 40));
            Add(new WallLeft(270, 70));
            Add(new WallLeft(270, 170));
            Add(new WallUp(240, 200));
            Add(new WallUp(140, 200));
            Add(new WallLeft(110, 230));
            //left
            Add(new WallLeft(110, 330));
            Add(new WallLeft(110, 430));
            Add(new WallDown(140, 460));
            Add(new WallDown(240, 460));
            Add(new WallDown(340, 460));
            Add(new WallDown(440, 460));
            Add(new WallLeft(470, 490));
            Add(new WallDown(440, 520));
            Add(new WallDown(340, 520));
            Add(new WallDown(240, 520));
            Add(new WallDown(140, 520));
            //bottom left
            Add(new WallLeft(110, 520));
            Add(new WallLeft(110, 620));
            Add(new WallLeft(110, 720));
            Add(new WallLeft(110, 820));
            Add(new WallDown(140, 850));
            Add(new WallDown(240, 845));
            Add(new WallDown(340, 845));
            Add(new WallDown(440, 840));
            Add(new WallDown(540, 835));
            #endregion
        }
        public override void Update()
        {
            base.Update();
            //System.Console.WriteLine("What is my Global Power? {0}", Global.TotalGlobalPower);
            //System.Console.WriteLine("Max Power: {0}", Global.MaxPowerOutput);
            //System.Console.WriteLine("Battery: {0}", Global.Battery);
            //System.Console.WriteLine("MaxBattery: {0}", Global.MaxBattery);
            //test the system game.
            //Console.WriteLine("masterTimer: {0}", masterTimer);
            //Console.WriteLine("masterCountRate: {0}", masterCountRate);
            //Console.WriteLine("waveOne: {0}", waveOne);
            //Console.WriteLine("waveTwo: {0}", waveTwo);
            //Console.WriteLine("waveThree: {0}", waveThree);
            //Console.WriteLine("waveFour: {0}", waveFour);
            //Console.WriteLine("waveReset: {0}", waveReset);
            masterTimer += masterCountRate;
            if (masterTimer >= 20 && waveOne)
            {
                waveOne = false;
                waveTwo = true;
                for (var i = 0; i < numberOfReleasedGirls; i++)
                {
                    Add(new ScaryGirl(Otter.Rand.Float(-16, 1480), Otter.Rand.Float(-16, 0)));
                }
            }
            else if (masterTimer >= 1220 && waveTwo)
            {
                waveTwo = false;
                waveThree = true;
                for (var i = 0; i < numberOfReleasedGirls; i++)
                {
                    Add(new ScaryGirl(Otter.Rand.Float(1440, 1480), Otter.Rand.Float(-16, 0)));
                }
            }
            else if (masterTimer >= 2420 && waveThree)
            {
                waveThree = false;
                waveFour = true;
                for (var i = 0; i < numberOfReleasedGirls; i++)
                {
                    Add(new ScaryGirl(Otter.Rand.Float(-16, 0), Otter.Rand.Float(0, 950)));
                }
            }
            else if (masterTimer >= 3620 && waveFour)
            {
                waveFour = false;
                waveReset = true;
                for (var i = 0; i < numberOfReleasedGirls; i++)
                {
                    Add(new ScaryGirl(Otter.Rand.Float(-16, 1480), Otter.Rand.Float(930, 950)));
                }
            }
            else if (masterTimer >= 4820 && waveReset)
            {
                waveOne = true;
                masterTimer = 0;
                masterCountRate++;
                numberOfReleasedGirls+=2;
                Global.scaryGirlSpeed+=.4f;
            }
            //Add(new Enemies(0, 400));//this was the UI for the enimies. heh
            #region Win Conditions
            //
            #endregion

            #region Lose Conditions
            if (Global.PlayerHealth <= 0)//more like player lives == 0. player health makes no sense. lol
            {//TODO
                //this might actually be a power generator that dies, when all the generators die. Maybe. I'm not sure yet.
                //Global.GAME.RemoveScene();
                // Global.GAME.AddScene(new LoseScreen());
            }
            #endregion
        }
        public override void Render()
        {
            base.Render();
            //grid.Render();
        }
    }
}