using Igdc.Controls;
using Igdc.Fonts;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;

namespace Igdc.Scenes.UI.Controls
{
    public class Button : ControlBase
    {
        public FontBase Font { get; set; }
        public Texture2D Texture { get; set; }
        public Color ActiveBackground { get; set; }
        public Color InactiveBackground { get; set; }
        public Color ActiveForeground { get; set; }
        public Color InactiveForeground { get; set; }
        public string Content { get; set; }
        public Action Command { get; set; }

        public override void Update(SceneIterationArgument e)
        {
            FocusDirection = FocusDirection.None;
            if (e.Control.UiNextButton.Pressed())
                FocusDirection = FocusDirection.Next;
            if (e.Control.UiPrevButton.Pressed())
                FocusDirection = FocusDirection.Prev;
            if (e.Control.UiAcceptButton.Pressed())
                Command?.Invoke();
        }

        public override void Draw(SceneIterationArgument e)
        {
            if (!IsAllSet(Font, Texture, Content)) return;
            var background = FocusDirection == FocusDirection.None ? ActiveBackground : InactiveBackground;
            var foreground = FocusDirection == FocusDirection.None ? ActiveForeground : InactiveForeground;
            e.Batch.Draw(Texture, new Rectangle(Location, Size), background);
            var offsetX = (Size.X - Font.MeasureString(Content)) / 2;
            var offsetY = (Size.Y - Font.Height) / 2;
            e.Batch.DrawText(Font, Content, new Vector2(Location.X + offsetX, Location.Y + offsetY), foreground);
        }
    }
}