using System;
using System.Collections;
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 Puppyups.Utilities;
using TiledSharp;

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

        public TmxMap tmxMap = new TmxMap(Assets.INLAB);
        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);
        private bool one = true;
        private bool two;
        private bool three;
        private bool soundOne = true;
        private bool soundTwo = true;
        private bool soundThree = true;

        public InLab()
        {
            #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);

            Add(new PA(300, 80));
            Add(new SpinningLight(200, 80));
            Add(new SpinningLight(400, 80));
            Add(new SpinningLight(600, 80));

            Add(new Player(Global.MW/2, Global.MH / 2+50));

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

            WriteWord(14, 2, "P1 Score:", Color.Gold, 1f, -2);
            Add(new Score(104, 4));
            if (Global.IsTwoPlayerActive)
            {
                WriteWord(470, 2, "P2 Score:", Color.Magenta, 1f, -2);
                Add(new Score2(570, 4));
            }
        }

        public override void Update()
        {
            base.Update();
            //this is where we start our story. We have nothing so we need to tell theplayer how to move around. 
            if (Timer > 50 && soundOne)
            {
                soundOne = false;
                SoundManager.PlayRequestedSound("_pabeep");
            }
            if (Timer > 100 && one)//HA a scene has a built in timer. YES.
            {
                one = false;
                two = true;
                Global.GAME.Coroutine.Start(Wait(2));
                WriteWord(221, 201, "INTERCOM:", Color.Gold, 1f, 1, 7);
                WriteWord(222, 201, "INTERCOM:", Color.White, 1f, 1, 7);
                WriteWord(221, 221, "WARNING:", Color.Red, 1f, 1, 7);
                WriteWord(222, 221, "WARNING:", Color.White, 1f, 1, 7);
                WriteWord(221, 231, "SMOG MONSTER HAS INVADED THE CITY!", Color.Orange, 1f, 1, 7);
                WriteWord(222, 231, "SMOG MONSTER HAS INVADED THE CITY!", Color.White, 1f, 1, 7);
                WriteWord(221, 241, "TAKE WHAT YOU CAN, BUT SACRIFICES MUST BE MADE!", Color.Black, 1f, 1, 7);
                WriteWord(222, 241, "TAKE WHAT YOU CAN, BUT SACRIFICES MUST BE MADE!", Color.White, 1f, 1, 7);
            }
            if (Timer > 550 && soundTwo)
            {
                soundTwo = false;
                SoundManager.PlayRequestedSound("_pabeep");
            }
            if (Timer > 600 && two)
            {
                var entities = GetEntities<Numbers>(); //remove intercom previous message.
                foreach (var item in entities)
                {
                    item.Destroy();
                }
                WriteWord(14, 2, "P1 Score:", Color.Gold, 1f, -2);
                Add(new Score(104, 4));
                if (Global.IsTwoPlayerActive)
                {
                    WriteWord(470, 2, "P2 Score:", Color.Magenta, 1f, -2);
                    Add(new Score2(570, 4));
                }
                two = false;
                three = true;
                SoundManager.PlayRequestedSound("_paattention");
                WriteWord(221, 201, "INTERCOM:", Color.Gold, 1f, 1, 7);
                WriteWord(222, 201, "INTERCOM:", Color.White, 1f, 1, 7);
                WriteWord(221, 221, "Controls:", Color.Orange, 1f, 1, 7);
                WriteWord(222, 221, "Controls:", Color.White, 1f, 1, 7);
                WriteWord(221, 231, "PlayerOne!", Color.Blue, 1f, 1, 7);
                WriteWord(222, 231, "PlayerOne!", Color.White, 1f, 1, 7);
                WriteWord(221, 241, "W, A, S, D, To move around! Try it!", Color.Black, 1f, 1, 7);
                WriteWord(222, 241, "W, A, S, D, To move around! Try it!", Color.White, 1f, 1, 7);
                if (Global.IsTwoPlayerActive)
                {
                    WriteWord(221, 251, "PlayerTwo!", Color.Magenta, 1f, 1, 7);
                    WriteWord(222, 251, "PlayerTwo!", Color.White, 1f, 1, 7);
                    WriteWord(221, 261, "ARROW KEYS To move around! Try it!", Color.Black, 1f, 1, 7);
                    WriteWord(222, 261, "ARROW KEYS To move around! Try it!", Color.White, 1f, 1, 7);
                }
            }
            if (Timer > 1150 && soundThree)
            {
                soundThree = false;
                SoundManager.PlayRequestedSound("_pabeep");
            }
            if (Timer > 1200 && three)
            {
                var entities = GetEntities<Numbers>(); //remove intercom previous message.
                foreach (var item in entities)
                {
                    item.Destroy();
                }
                WriteWord(14, 2, "P1 Score:", Color.Gold, 1f, -2);
                Add(new Score(104, 4));
                if (Global.IsTwoPlayerActive)
                {
                    WriteWord(470, 2, "P2 Score:", Color.Magenta, 1f, -2);
                    Add(new Score2(570, 4));
                }
                three = false;
                SoundManager.PlayRequestedSound("_paattention");
                WriteWord(221, 201, "INTERCOM:", Color.Gold, 1f, 1, 7);
                WriteWord(222, 201, "INTERCOM:", Color.White, 1f, 1, 7);
                WriteWord(221, 221, "Go:", Color.Orange, 1f, 1, 7);
                WriteWord(222, 221, "Go:", Color.White, 1f, 1, 7);
                WriteWord(221, 231, "Save your stuff!", Color.Blue, 1f, 1, 7);
                WriteWord(222, 231, "Save your stuff!", Color.White, 1f, 1, 7);
                WriteWord(221, 241, "------------------>>>>>>>", Color.Black, 1f, 1, 7);
                WriteWord(222, 241, "------------------>>>>>>>", Color.White, 1f, 1, 7);
            }

        }

        public static IEnumerator Wait(int frames)
        {
            SoundManager.PlayRequestedSound("_pawarning");
            yield return Coroutine.Instance.WaitForFrames(50);
            SoundManager.PlayRequestedSound("_pasmog");
            yield return Coroutine.Instance.WaitForFrames(150);
            SoundManager.PlayRequestedSound("_patake");
        }

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