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

namespace Slayter
{
    class BuyScreen:ScreenOrigin
    {
        List<IDrawable> heroDrawables;

        public BuyScreen(CastleScreen screen,String title,Player player,Monster monster,String monsterLabel)
            :base(screen.parent)
        {
            drawables = new List<IDrawable>(screen.Drawables);
            heroDrawables = new List<IDrawable>(screen.heroDraw);
            parent.setMessageScreen(this);

            commingFrom = screen;

            Texture2D messageTex = parent.Content.Load<Texture2D>("message");
            Rectangle messageBox = new Rectangle((640 - (messageTex.Width + 100)) / 2, (480 - (messageTex.Height + 75)) / 2,
                messageTex.Width + 100, messageTex.Height + 75);
            ClickableObject message = new ClickableObject(messageTex, messageBox, delegate(MouseState state, ClickableObject o) { });

            SpriteFont f = parent.Content.Load<SpriteFont>("font2");
            Vector2 v = f.MeasureString(title);
            Text Title = new Text(f, title, new Vector2(messageBox.X + (messageBox.Width - v.X) / 2, messageBox.Y + 5));

            SpriteFont f2 = parent.Content.Load<SpriteFont>("font3");
            TextArea teXt = new TextArea(f2, monster.getInfo(), new Rectangle(messageBox.X + 10, messageBox.Y + 45, 300, 90),parent);

            Vector2 v3 = f.MeasureString((player.gold/monster.Cost).ToString());
            Text text2 = new Text(f, (player.gold / monster.Cost).ToString(), new Vector2(messageBox.X + (messageBox.Width - v3.X) / 2, messageBox.Y + 167));

            Texture2D NewDayButton = parent.Content.Load<Texture2D>("button");
            Rectangle rect2 = new Rectangle(270, 300, NewDayButton.Width, NewDayButton.Height);
            ClickableObject button_ClickableObject2 = new ClickableObject(NewDayButton, rect2, delegate(MouseState state, ClickableObject o)
            {
                int count = screen.hero.units.Count;
                if (player.gold >= monster.Cost && player.currentHero.addMonster(monster, 1))
                {
                    player.gold -= monster.Cost;
                    if (screen.hero.units.Count != count){
                        screen.drawHero();
                        heroDrawables = screen.heroDraw;
                    }
                }
                screen.UpdateText();
                screen.RefreshLables();
                text2.label = (player.gold / monster.Cost).ToString();
                v3 = f.MeasureString((player.gold / monster.Cost).ToString());
                text2.position = new Vector2(messageBox.X + (messageBox.Width - v3.X) / 2, messageBox.Y + 167);
            });
            ClickableObject ground = new ClickableObject(screen.BGROUND.Picture, new Rectangle(0, 0, 640, 480), delegate(MouseState state, ClickableObject o) { parent.removeMessageScreen(this); });

            drawables.Add(message);
            drawables.Add(teXt);
            drawables.Add(button_ClickableObject2);
            drawables.Add(text2);
            drawables.Add(Title);
            clickables.Add(button_ClickableObject2);
            clickables.Add(teXt);
            clickables.Add(message);
            clickables.Add(ground); 
        }

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

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