using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Blindfold.Objects;

namespace Blindfold.Managers
{
    public class LevelManager
    {
        private static char[] dataFileSeparator = { ',' };
        private static int pitNumber;

        /* 
         * Loads the level from an Image file, and creates Sprites according to the pixel color.
         * Attention: Filetype must not be put into the name, for example "level" instead of "level.png"
         */
        public static void loadImage(Main game, string path, string name, int tileWidth, int tileHeight)
        {
            Texture2D texture = game.Content.Load<Texture2D>(path + name);
            int width = texture.Width;
            pitNumber = 0;
            int height = texture.Height;
            int textureNumber;
            //Sets up an array of Colors with size = to width*height so that each pixel of the image can be stored
            Color[] colors = new Color[width * height];
            texture.GetData(colors);
            Vector2 position = Vector2.Zero;

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    position = new Vector2(x * tileWidth, y * tileHeight);
                    textureNumber = getTextureNumber(colors[x + y * width]);
                   
                    if (textureNumber != 0) //not black or unhandled
                    {
                        switch (textureNumber)
                        {
                            case 1:
                                //TODO Implement walls
                                Wall wall = new Wall(new Vector3(position, 1.8f));
                                game.OBJECTMANAGER.add(wall);
                                break;
                            case 2:
                                Player player = new Player(new Vector3(position, 1.8f));
                                game.PLAYER = player;
                                game.OBJECTMANAGER.add(player);
                                break;
                            case 3:
                                Guider guider = new Guider(new Vector3(position, 1.8f));
                                game.GUIDER = guider;
                                game.OBJECTMANAGER.add(guider);
                                break;
                            case 4:
                                Monster monster = new Monster(new Vector3(position, 1.8f),0);
                                game.MONSTER = monster;
                                game.OBJECTMANAGER.add(monster);
                                break;
                            case 5:
                                Checkpoint finish = new Checkpoint(new Vector3(position, 1.8f));
                                game.FINISH = finish;
                                game.OBJECTMANAGER.add(finish);
                                break;
                            case 6:
                                Checkpoint cp = new Checkpoint(new Vector3(position, 1.8f));
                                game.CPList.Add(cp);
                                game.OBJECTMANAGER.add(cp);
                                break;
                            case 7:
                                Monster monster1 = new Monster(new Vector3(position, 1.8f), 1);
                                game.MONSTER = monster1;
                                game.OBJECTMANAGER.add(monster1);
                                break;
                            case 8:
                                Monster monster2 = new Monster(new Vector3(position, 1.8f), 2);
                                game.MONSTER = monster2;
                                game.OBJECTMANAGER.add(monster2);
                                break;
                            case 9:
                                EasterEgg egg = new EasterEgg(new Vector3(position, 1.8f));
                                game.OBJECTMANAGER.add(egg);
                                break;
                        }
                    }
                }
            }
            //texture.Dispose();
        }

        /*
         * Gets the TextureNumber for the Sprite that should be created, when a certain color appears.
         */
        private static int getTextureNumber(Color color)
        {
            //add in needed textureNumbers for each color needed, not mentioned colors wont be handled
            if (color.Equals(Color.Black))
            {
                return 1;
            }
            else if (color.Equals(new Color(0, 38, 255)))
            {
                return 2;
            }
            else if (color.Equals(new Color(255,216,0)))
            {
                return 3;
            }
            else if (color.Equals(new Color(255, 0, 0)))
            {
                return 4;//Monster Stage 1
            }
            else if (color.Equals(new Color(76, 255, 0)))
            {
                return 5;//Goal
            }
            else if (color.Equals(new Color(255, 106, 0)))
            {
                return 6;//Waypoint
            }
            else if (color.Equals(new Color(127, 0, 0)))
            {
                return 7;//Monster Stage 2
            }
            else if (color.Equals(new Color(127, 0, 55)))
            {
                return 8;//Monster Stage 3
            }
            else if (color.Equals(new Color(183, 255, 0)))
            {
                return 9;//Easter Egg
            }
            else
            {
                return 0;
            }
        }
    }
}
