using Otter;
using TiledSharp;
using PenniesAreGold.Utility;
using PenniesAreGold.objects;
using PenniesAreGold.player;
using PenniesAreGold.ui;
using System;
using PenniesAreGold.utility;

namespace PenniesAreGold
{
    class LevelOne : Scene
    {
        public const int _width = Global.MW * 6;
        public const int _height = Global.MW * 4;

        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);

        public LevelOne()
        {
            #region MapCreation 01
            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 if (layer.Name == "Layer 4")
                    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")//light blue water
                        {
                            if (layer.Tiles[i].Gid != 0)
                                layerThree.SetTile(x, y, layer.Tiles[i].Gid - 1, layer.Name);
                        }
                        else//dark blue water
                            layerFour.SetTile(x, y, layer.Tiles[i].Gid - 1, layer.Name);
                    }
                }
            }
            #endregion

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

            #region Add Players and UI
            Add(new UpperUI(0, 0));
            Global.player = new Player(Global.MW/2, Global.MH/2 - 160);
            Add(Global.player);
            Add(new MineButton(1, 1));
            Add(new TroopButton(48, 1));
            Add(new Cost(-30, 38));
            Add(new Score(206, 4));
            WriteWord(330, 4, "Score!", Color.White, 1f, -10);
            Add(new Money(206, 20));
            WriteWord(330, 20, "Coins!", Color.Orange, 1f, -10);
            Add(new Kills(206, 36));
            WriteWord(330, 36, "Kills!", Color.FromBytes(255,50,50), 1f, -10);
            Add(new NoSound(700, 2));
            #endregion
            #region enemies
            //northwest
            Add(new EnemyTroopBuilding(100, 100));
            Add(new EnemyTroopBuilding(140, 100));
            Add(new EnemyTroopBuilding(180, 100));
            Add(new EnemyTroopBuilding(220, 100));
            Add(new EnemyTroopBuilding(260, 100));
            Add(new EnemyTroopBuilding(100, 140));
            Add(new EnemyTroopBuilding(140, 140));
            Add(new EnemyTroopBuilding(180, 140));
            Add(new EnemyTroopBuilding(220, 140));
            Add(new EnemyTroopBuilding(260, 140));
            //north
            Add(new EnemyTroopBuilding(Global.MW/2, 100));
            //northeast
            Add(new EnemyTroopBuilding(Global.MW -32, 100));
            Add(new EnemyTroopBuilding(Global.MW -32, 140));
            Add(new EnemyTroopBuilding(Global.MW -32, 180));
            Add(new EnemyTroopBuilding(Global.MW -32, 220));
            Add(new EnemyTroopBuilding(Global.MW -32, 260));
            Add(new EnemyTroopBuilding(Global.MW -32, 300));
            //east
            Add(new EnemyTroopBuilding(Global.MW - 32, Global.MH / 2 - 160));
            //southeast
            Add(new EnemyTroopBuilding(Global.MW - 32, Global.MH - 20));
            Add(new EnemyTroopBuilding(Global.MW - 32, Global.MH - 40));
            Add(new EnemyTroopBuilding(Global.MW - 32, Global.MH - 60));
            Add(new EnemyTroopBuilding(Global.MW - 32, Global.MH - 80));
            Add(new EnemyTroopBuilding(Global.MW - 32, Global.MH - 120));
            Add(new EnemyTroopBuilding(Global.MW - 64, Global.MH - 20));
            Add(new EnemyTroopBuilding(Global.MW - 64, Global.MH - 40));
            Add(new EnemyTroopBuilding(Global.MW - 64, Global.MH - 60));
            Add(new EnemyTroopBuilding(Global.MW - 64, Global.MH - 80));
            Add(new EnemyTroopBuilding(Global.MW - 64, Global.MH - 120));
            //south
            Add(new EnemyTroopBuilding(Global.MW /2, Global.MH - 40));
            //southwest
            Add(new EnemyTroopBuilding(100, Global.MH - 32));
            Add(new EnemyTroopBuilding(140, Global.MH - 32));
            Add(new EnemyTroopBuilding(180, Global.MH - 32));
            Add(new EnemyTroopBuilding(220, Global.MH - 32));
            Add(new EnemyTroopBuilding(260, Global.MH - 32));
            Add(new EnemyTroopBuilding(100, Global.MH - 64));
            Add(new EnemyTroopBuilding(140, Global.MH - 64));
            Add(new EnemyTroopBuilding(180, Global.MH - 64));
            Add(new EnemyTroopBuilding(220, Global.MH - 64));
            Add(new EnemyTroopBuilding(260, Global.MH - 64));
            //west
            Add(new EnemyTroopBuilding(100, Global.MH / 2 - 160));
            #endregion
        }

        public override void Update()
        {
            base.Update();
            //Console.WriteLine("number of coins: {0}", Global.numberOfCoins);
            //for (var i = 0; i < 1; i++)
            // {
            //    Add(new Penny(Rand.Int(16, Global.MW - 16), Rand.Int(16, Global.MH - 32)));
            // }
            //Global.cam.followPlayer(Global.MW, Global.MH, Global.PlayerX + 100, Global.PlayerY + 100, Global.PlayerSpeed);
            #region End Game
            var troopBuildings = GetEntities<EnemyTroopBuilding>();
            if (troopBuildings.Count <= 0)
            {
                NoSound.music.Stop();
                Global.GAME.RemoveScene();
                Global.GAME.AddScene(new WinScreen());
            }
            #endregion
            //Add(new Enemies(0, 400));//this was the UI for the enimies.
            #region Win Conditions
            #endregion

            #region Lose Conditions
            //if (Global.PlayerHealth <= 0)//more like player lives == 0. player health makes no sense. lol
            //{//TODO
                //Global.GAME.RemoveScene();
                // Global.GAME.AddScene(new LoseScreen());
            //}
            #endregion

            //spawn them over time in a random area all around the map every 300 seconds.
            //if (bubbleSpawnTimer <= 0)
            //{
            //   for (var i = 0; i < 300; i++)
            //        Add(new GreenBubble(Rand.Int(0, Global.MW), Rand.Int(0, Global.MH)));
            //    bubbleSpawnTimer = 300;
            //}
            //if (bubbleSpawnTimer > 0)
            //    bubbleSpawnTimer--;
        }

        public void WriteWord(float x, float y, string word, Color color, float scale, int layer)
        {
            var a = 0;
            for (var i = 0; i < word.Length; i++)
            {
                Add(new Numbers(x + a, y, word[i].ToString(), color, scale, layer));
                a += 8;
            }
        }

        public override void Render()
        {
            base.Render();
            grid.Render();
            //right.Render(Color.Green);
            //left.Render(Color.Yellow);
            //down.Render(Color.Magenta);
            //up.Render(Color.Grey);
        }
    }
}

