using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Slayter
{
    public class CastleScreen: Screen
    {
        const int CenterX = 286;
        const int CenterY = 203;

        const int CornerTopLeftX = 36;
        const int CornerTopLeftY = 20;

        const int CornerTopRightX = 546;
        const int CornerTopRightY = 20;

        const int CornerBottomLeftX = 546;
        const int CornerBottomLeftY = 396;

        const int MiddleBottomLeftX = 162;
        const int MiddleBottomLeftY = 292;

        const int MiddleTopRightX = 409;
        const int MiddleTopRightY = 113;

        Background Bground;
        public override Background BGROUND { get { return Bground; } }

        List<MonsterLabel> labels;
        public List<IDrawable> heroDraw;

        Castle castle;
        public CastleScreen(Castle castle,Game1 game,String background)
            : base(game)
        {
            this.castle = castle;

            commingFrom = game.GameScreen;
            labels = new List<MonsterLabel>();

            Bground = new Background(game.Content.Load<Texture2D>(background), new Rectangle(0, 0, 640, 480));
            drawables.Add(Bground);

            heroDraw=new List<IDrawable>();

            //Texture2D tab_background = game.Content.Load<Texture2D>("tab_background");
            Texture2D tab_background = game.Content.Load<Texture2D>("CastleTabWindowed");


            Background tab_Object = new Background(tab_background, new Rectangle(0, 480, tab_background.Width, tab_background.Height));
            drawables.Add(tab_Object);

            Texture2D NewDayButton = game.Content.Load<Texture2D>("Back_button");
            Rectangle rect2 = new Rectangle(game.Window.ClientBounds.Width -((int)(NewDayButton.Width/1.2) + 10), 490, (int)(NewDayButton.Width/1.2),(int)( NewDayButton.Height/1.2));
            ClickableObject button_ClickableObject2 = new ClickableObject(NewDayButton, rect2, delegate(MouseState state, ClickableObject o) { Console.Out.WriteLine("here"); parent.removeMessageScreen(this); });
            drawables.Add(button_ClickableObject2);
            clickables.Add(button_ClickableObject2);
            AddDragon();
            AddEagle();
            AddWarrior();
            AddLion();
            AddWolf();


            gold = new Text(game.Content.Load<SpriteFont>("font"), "G: ", new Vector2(5, 490));

            AddPlayer(castle.owner);
            drawHero();

            drawables.Add(gold);
        }

        public override void draw(Game1 game, SpriteBatch spriteBatch)
        {
            foreach(IDrawable element in drawables){
                element.draw(spriteBatch);
            }

            foreach (IDrawable element in heroDraw){
                element.draw(spriteBatch);
            }
        }

        public override void AddPlayer(Player player)
        {
            this.player = player;
            hero = player.currentHero;
            UpdateText();
        }

        public void drawHero()
        {
            SpriteFont font = parent.Content.Load<SpriteFont>("font");
            heroDraw.Clear();
            labels.Clear();
            for (int i = 0; i < hero.units.Count; i++)
            {
                Rectangle  monRect=new Rectangle(41+120*i,529,hero.units[i].Texture.Width,hero.units[i].Texture.Height);
                Background mon = new Background(hero.units[i].Texture,monRect);
                heroDraw.Add(mon);
                MonsterLabel count = new MonsterLabel(hero.units[i], font, hero.units[i].Quantity + "", 
                    new Vector2(monRect.X, monRect.Y + (monRect.Width - (font.MeasureString("1").X + 5))));
                labels.Add(count);
                heroDraw.Add(count);
            }
        }

        public void RefreshLables()
        {
            foreach (MonsterLabel label in labels){
                label.Refresh();
            }
        }

        public void AddDragon()
        {
            Texture2D drag = parent.Content.Load<Texture2D>("DragonIcon");
            Rectangle rect2 = new Rectangle(MiddleBottomLeftX, MiddleBottomLeftY, drag.Width, drag.Height);
            ClickableObject dragon = new ClickableObject(drag,rect2,delegate(MouseState state, ClickableObject o){
                BuyScreen message = new BuyScreen(this, "Dragons", player, new Dragon(drag),"dragon");
            });
            clickables.Add(dragon);
            drawables.Add(dragon);
        }

        public void AddWarrior()
        {
            Texture2D war = parent.Content.Load<Texture2D>("WarriorIcon");
            Rectangle rect2 = new Rectangle(CornerTopLeftX, CornerTopLeftY, war.Width, war.Height);
            ClickableObject warrior = new ClickableObject(war, rect2, delegate(MouseState state, ClickableObject o)
            {
                BuyScreen message = new BuyScreen(this, "Warriors",player,new Warrior(war),"warrior");
            });
            clickables.Add(warrior);
            drawables.Add(warrior);
        }

        internal void AddLion()
        {
            Texture2D lio = parent.Content.Load<Texture2D>("LionIcon");
            Rectangle rect2 = new Rectangle(CornerTopRightX, CornerTopRightY, lio.Width, lio.Height);
            ClickableObject lion = new ClickableObject(lio, rect2, delegate(MouseState state, ClickableObject o)
            {
                BuyScreen message = new BuyScreen(this, "Lion", player, new Lion(lio), "lion");
            });
            clickables.Add(lion);
            drawables.Add(lion);
        }

        internal void AddWolf()
        {
            Texture2D wol = parent.Content.Load<Texture2D>("WolfIcon");
            Rectangle rect2 = new Rectangle(CornerBottomLeftX, CornerBottomLeftY, wol.Width, wol.Height);
            ClickableObject wolf = new ClickableObject(wol, rect2, delegate(MouseState state, ClickableObject o)
            {
                BuyScreen message = new BuyScreen(this, "Wolf", player, new Wolf(wol), "wolf");
            });
            clickables.Add(wolf);
            drawables.Add(wolf);
        }

        public void AddEagle()
        {
            Texture2D eag = parent.Content.Load<Texture2D>("EagleIcon");
            Rectangle rect2 = new Rectangle(MiddleTopRightX, MiddleTopRightY, eag.Width, eag.Height);
            ClickableObject eagle = new ClickableObject(eag, rect2, delegate(MouseState state, ClickableObject o)
            {
                BuyScreen message = new BuyScreen(this, "Eagle", player, new Eagle(eag), "eagle");
            });
            clickables.Add(eagle);
            drawables.Add(eagle);
        }
    }
}
