using LOoSE_sLOTS.entities;
using LOoSE_sLOTS.Utilities;
using LOoSE_sLOTS.UI;
using Otter;
using TiledSharp;
using LOoSE_sLOTS.objects;

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

        public TmxMap tmxMap = new TmxMap(Assets.CASINO);
        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 bool firstWave = true;
        private bool firstWaveActive = false;
        private bool secondWave = false;
        private bool secondWaveActive = false;
        private bool thirdWave = false;
        private bool thirdWaveActive = false;
        private bool bonusReceived;

        public Casino()
        {
            #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
            Add(new Player(640, 475));
            #endregion
            #region Objects
            var wInc = 75;
            for (var y = 0; y < 9; y++)
            {
                var incrementer = 70;
                for (var i = 0; i < 44; i++)//initial Fuel
                {
                    if (i < 19 || i > 20)
                        Add(new RealSlotMachine(incrementer, wInc));
                    incrementer += 30;
                }
                wInc += 100;
            }            
            var wInc2 = 55;
            for (var y = 0; y < 9; y++)
            {
                var incrementer2 = 50;
                for (var i = 0; i < 44; i++)//initial Fuel
                {
                    if(incrementer2 <= Global.MW-42)
                        Add(new Civilian(incrementer2, wInc2));
                    incrementer2 += 100;
                }
                wInc2 += 100;
            }
            Global.WriteWord(200, 10, "Coins:", Color.Gold, 2f, -100, this, 0);//shadow first
            Global.WriteWord(200 - 2, 10 - 2, "Coins:", Color.White, 2f, -100, this, 0);//regular text on top(second)  
            Add(new CoinsAreLife(80, 5));
            Global.WriteWord(400, 10, "Snipers:", Color.Green, 2f, -100, this, 0);//shadow first
            Global.WriteWord(400 - 2, 10 - 2, "Snipers:", Color.White, 2f, -100, this, 0);//regular text on top(second)  
            Add(new SnipersUI(270, 5));
            Global.WriteWord(10, 10, "Boss:", Color.Red, 2f, -100, this, 0);//shadow first
            Global.WriteWord(10 - 2, 10 - 2, "Boss:", Color.White, 2f, -100, this, 0);//regular text on top(second)  
            Add(new BossUI(-150, 5));
            #endregion
        }
        public override void Update()
        {
            base.Update();
            if (Timer >= 150 && firstWave)
            {
                firstWave = false;
                SoundManager.PlayRequestedSound("_getThatMoney");
                Add(new Sniper(Global.MW / 2 - 80, Global.MH - 80));
                Add(new Sniper(Global.MW / 2 - 80, 60));
                Add(new Sniper(80, Global.MH / 2));
                Add(new Sniper(Global.MW - 80, Global.MH / 2));
                Global.NumberOfSnipers = 4;
                firstWaveActive = true;
            }
            if (Global.NumberOfSnipers <= 0 && firstWaveActive)
            {
                firstWaveActive = false;
                secondWave = true;
            }
            if (!firstWave && secondWave)
            {
                secondWave = false;
                secondWaveActive = true;
                SoundManager.PlayRequestedSound("_getThatMoney");
                var slots = GetEntities<RealSlotMachine>();
                foreach(var item in slots)
                {//lower all machines to 5
                    item.Health = 5;
                }
                Add(new Sniper(Global.MW / 2 - 80, Global.MH - 70));
                Add(new Sniper(Global.MW / 2 - 80, 60));
                Add(new Sniper(80, Global.MH / 2));
                Add(new Sniper(Global.MW - 80, Global.MH / 2));
                Add(new Sniper(Global.MW / 2 - 80, Global.MH - 70));
                Add(new Sniper(Global.MW / 2 - 80, 60));
                Add(new Sniper(80, Global.MH / 2));
                Add(new Sniper(Global.MW - 80, Global.MH / 2));
                Global.NumberOfSnipers = 8;
            }
            if (Global.NumberOfSnipers <= 0 && secondWaveActive)
            {
                secondWaveActive = false;
                thirdWave = true;
            }
            if (!firstWave && !secondWave && thirdWave)
            {
                thirdWave = false;
                thirdWaveActive = true;
                SpawnBoss();
                var slots = GetEntities<RealSlotMachine>();
                foreach (var item in slots)
                {//lower all machines to 1
                    item.Health = 1;
                }
                Add(new Sniper(Global.MW / 2 - 80, Global.MH - 80));
                Add(new Sniper(Global.MW / 2 - 80, 60));
                Add(new Sniper(80, Global.MH / 2));
                Add(new Sniper(Global.MW - 80, Global.MH / 2));
                Add(new Sniper(Global.MW / 2 - 80, Global.MH - 80));
                Add(new Sniper(Global.MW / 2 - 80, 60));
                Add(new Sniper(80, Global.MH / 2));
                Add(new Sniper(Global.MW - 80, Global.MH / 2));
                Add(new Sniper(Global.MW / 2 - 80, Global.MH - 80));
                Add(new Sniper(Global.MW / 2 - 80, 60));
                Add(new Sniper(80, Global.MH / 2));
                Add(new Sniper(Global.MW - 80, Global.MH / 2));
                Add(new Sniper(Global.MW / 2 - 80, Global.MH - 80));
                Add(new Sniper(Global.MW / 2 - 80, 60));
                Add(new Sniper(80, Global.MH / 2));
                Add(new Sniper(Global.MW - 80, Global.MH / 2));
                Global.NumberOfSnipers = 16;
            }
            if (Global.NumberOfSnipers <= 0 && thirdWaveActive && !bonusReceived)
            {
                bonusReceived = true;
                Global.score += 1000;
                for(var i = 0; i < 100; i++)
                {
                    Add(new GameCoin(Global.MW / 2, Global.MH / 2, Global.MH/2+12));
                }
            }
        }
        public void SpawnBoss()
        {
            var rand = Rand.Int(1, 5);
            if (rand == 1)
                Add(new Boss(Global.MW / 2 - 80, Global.MH - 80));
            if (rand == 2)
                Add(new Boss(Global.MW / 2 - 80, 60));
            if (rand == 3)
                Add(new Boss(80, Global.MH / 2));
            if (rand == 4)
                Add(new Boss(Global.MW - 80, Global.MH / 2));

        }
        public override void Render()
        {
            base.Render();
            //grid.Render();
        }
    }
}

