using GiraffeRoller.entities;
using GiraffeRoller.entities.enemies;
using GiraffeRoller.ui;
using GiraffeRoller.Utility;
using Otter;
using TiledSharp;

namespace GiraffeRoller.scenes.Levels
{
    class LevelOne : Scene
    {
        public const int _width = Global.MW * 3;
        public const int _height = Global.MH * 2;
        private bool _addGlobalUI = true;
        private bool isBossSpawned = true;

        #region level 01
        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 bool _addGiraffesScoreMissing;
        #endregion

        public LevelOne(float playerX, float playerY)
        {
            #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
                    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);

            Global.cam = new Camera(100, 100);
            #region Add Players and UI
            Global.player = new Player(playerX, playerY);
            Add(Global.player);
            Add(new Score(10, 10));
            //Add(new Lives(-106));
            Add(new GiraffesStolenDisplay(450, 10));
            Add(new PlayerHealthDisplay(240, 10));
            #endregion
            if (Global.music == null)
                Global.music = new Music(Assets.CRICKETAMBIENCE);
            if (!Global.music.IsPlaying)
            {
                Global.music.Play();
                Global.music.Volume = 2f;
                Global.music.Loop = true;
            }
            Add(Global.cam);
            for (var i = 0; i < 300; i++)
                Add(new WildGiraffe(Rand.Int(16, Global.MW - 16), Rand.Int(16, Global.MH - 32)));
            for (var i = 0; i < 2000; i++)
                Add(new YGrass(Rand.Int(-25, Global.MW + 25), Rand.Int(-25, Global.MH + 25), Color.White, Rand.Float(.5f, 2.5f), Rand.Int(-1, 6)));
            for (var i = 0; i < 200; i++)
                Add(new STree(Rand.Int(-25, Global.MW + 25), Rand.Int(-25, Global.MH + 25), Color.White, Rand.Float(.5f, 2.5f), Rand.Int(-1, 6)));
            //always add this guy so we can pause the game:
            Add(new PausePerson());
        }

        public override void Begin()
        {
            base.Begin();
            Global.cam.adjustToPlayer(Global.MW, Global.MH, Global.PlayerX, Global.PlayerY);
        }
        public override void Update()
        {
            base.Update();
            if (!IsGroupPaused(0))
            {
                if (_addGlobalUI)
                {
                    _addGlobalUI = false;
                    //Global.AddAllLevelUI(Global.GAME.Scene);//so i don't have to add the same ui every level if all levels have it.
                }
                Shared.LevelUpdate(Global.GAME.Scene);
                //all other unique level updates go here:
                if (!_addGiraffesScoreMissing)
                {
                    _addGiraffesScoreMissing = true;
                    Global.WriteWord(380 - 1, 10 - 1, "Giraffes Stolen:", Color.FromBytes(221, 25, 29, 255), 1f, -101, this, 0, 10);//regular text  
                    Global.WriteWord(380, 10, "Giraffes Stolen:", Color.FromBytes(182, 0, 3, 255), 1f, -100, this, 0, 10);//shadow (layers sort this)
                    Global.WriteWord(20 - 1, 10 - 1, "Score:", Color.FromBytes(255, 143, 0, 255), 1f, -101, this, 0, 10);//regular text  
                    Global.WriteWord(20, 10, "Score:", Color.FromBytes(216, 121, 0, 255), 1f, -100, this, 0, 10);//shadow (layers sort this)
                    Global.WriteWord(220 - 1, 10 - 1, "Health:", Color.FromBytes(235, 177, 36, 255), 1f, -101, this, 0, 10);//regular text  
                    Global.WriteWord(220, 10, "Health:", Color.FromBytes(196, 139, 0, 255), 1f, -100, this, 0, 10);//shadow (layers sort this)
                }
                if (Timer >= 400 && isBossSpawned)
                {
                    isBossSpawned = false;
                    Add(new Boss(Global.MW - 32, Global.MH / 2));
                }
                if (Global.PlayerSession.Controller.Button("P1Start").Pressed)
                {
                    Global.GAME.RemoveScene();
                    Global.GAME.AddScene(new LoseScreen());// Add next level here or, where you want to go
                }
            }
        }//end of UPDATE

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