using System;
using KellysMarvelousAdventure.entities;
using KellysMarvelousAdventure.UI;
using KellysMarvelousAdventure.Utilities;
using Otter;
using TiledSharp;

namespace KellysMarvelousAdventure.Scenes
{
    class Bedroom : Scene
    {
        public const int _width = Global.MW;
        public const int _height = Global.MW;

        public TmxMap tmxMap = new TmxMap(Assets.BEDROOM);
        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 Bedroom(string previousScene)
        {
            #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));
            var playerx = 0;
            var playery = 0;
            if (previousScene == "InsideFarmhouse")
            {
                playerx = 683;
                playery = 406;
            }
            else
            {
                playerx = 590;
                playery = 480;
                SoundManager.PlayRequestedSound("_girlILoveMyFarm");
            }
            Global.player = new Player(playerx, playery);
            Add(Global.player);
            Add(new Stairs(576, 384, "upstairs"));
            Add(new Bed(560, 480));
            //Global.player2 = new Oldman(700, 300);
            //Add(Global.player2);
            //Add(new InsideDoor(768, 544));
            //Add(new RedWagon(720, 300));
            Add(new ClickerWall(300, 0));
            Add(new ClickyTree(300, 0));

            //UI
            WriteWord(330, 4, "Score: ", Color.White, 1f, -10);
            Add(new Score(350, 4));
            WriteWord(550, 4, "Clicks: ", Color.White, 1f, -10);
            Add(new Clicks(550, 4));
            Add(new SapIndicator(570, 180));
            WriteWord(540, 190, "Sap Generatored: ", Color.White, 1f, -10);
            Add(new SappCollected(570, 190));
            WriteWord(540, 220, "SapBombs: ", Color.White, 1f, -10);
            Add(new NumberOfSappBombs(540, 220));
            Add(new SapBombs(670, 220));
            WriteWord(150, 4, "Health: ", Color.White, 1f, -10);
            Add(new Health(130, 4));
            //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
        }

        public override void Update()
        {
            base.Update();
            Global.RunClicks();
            //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);
        }
    }
}
