using ActionGame.UserInterface.Helper;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;


namespace ActionGame.UserInterface.Control
{
    public class Label : IControl
    {
        public IList<IControl> Controls { get; }

        public SpriteFont Font { get; set; }
        
        public Boolean IsSelected { get; set; }
        public Boolean CropTextToFitWidth { get; set; }
        public Boolean IsSelectable { get; }
        private Rectangle location;
        private Vector2 textLocation;
        private String textToDraw;


        public Label()
        {
            this.Controls = new List<IControl>();
            this.IsSelectable = false;
            this.CropTextToFitWidth = false;
        }


        public String Text { 
            get 
            {
                return textToDraw;
            } 
            set
            {
                if (CropTextToFitWidth == false) this.textToDraw = value;
                else this.textToDraw = TextHelper.GetCroppedText(value, this.Font, this.Location.Width);
            }
        }


        public Rectangle Location
        {
            get
            {
                return this.location;
            }
            set
            {
                this.location = value;

                this.textLocation = GetBestTextLocation();
            }
        }


        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            Vector2 v = new Vector2(Location.X, Location.Y);

            spriteBatch.DrawString(this.Font, this.Text, v, Color.White);
        }


        public void Update(GameTime gameTime)
        {

        }


        public void Press(GameTime gameTime)
        {

        }


        private Vector2 GetBestTextLocation()
        {
            Vector2 vector = new Vector2(this.Location.X, this.Location.Y);

            if ((this.Text == null) || (this.Text.Equals(""))) return vector;

            vector.Y += 16;
            vector.X += (this.Location.Width / 2) - (this.Text.Length * 8);

            return vector;
        }

    }
}
