using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Otter;
using Puppyups.entities;
using Puppyups.objects;
using Puppyups.UI;
using TiledSharp;

namespace Puppyups.scenes
{
    class Yard : Scene
    {
        public const int _w = Global.MW;
        public const int _h = Global.MH;

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

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

        public Yard()
        {
            #region Create the Map
            grid = new GridCollider(_w, _h, 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);

            for (var p = 0; p < 100; p++)
            {
                Add(new Dog(Rand.Float(64, Global.MW-64), Rand.Float(64, Global.MH-64)));
            }
            Add(new Player(2+60, Global.MH / 2));

            Add(new Player2(2+40, Global.MH / 2));

            WriteWord(14, 2, "P1 Score:", Color.Gold, 1f, -2);
            Add(new Score(64, 4));
            WriteWord(204, 2, "DogsLeft:", Color.Green, 1f, -2);
            Add(new DogsScore(244, 4));
            if (Global.IsTwoPlayerActive)
            {
                WriteWord(470, 2, "P2 Score:", Color.Magenta, 1f, -2);
                Add(new Score2(570, 4));
            }
            Add(new SmogMonster(Global.MW / 2, Global.MH / 2));
        }

        public override void Update()
        {
            base.Update();
            Global.DogsLeft = GetEntities<Dog>().Count;
        }

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