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

namespace Igdc.Scenes.UI.Controls
{
    public class TextBlock : ControlBase
    {
        public FontBase Font { get; set; }
        public Color TextColor { get; set; }
        public Texture2D BorderTexture { get; set; }
        public int BorderWidth { get; set; } = 1;
        public Color BorderColor { get; set; }

        public string Text
        {
            get { return _text; }
            set
            {
                _text = value;
                _lines = Font.PrepareLines(Text, Size.X);
            }
        }
        private string _text;
        private List<string> _lines;
        private int _skipLines;

        public override void Update(SceneIterationArgument e)
        {
            FocusDirection = FocusDirection.None;
            if (!IsAllSet(Font, Text, TextColor))
            {
                FocusDirection = FocusDirection.Auto;
                return;
            }
            if (e.Control.UiNextButton.Pressed())
            {
                var height = Font.Height * (_lines.Count - _skipLines);
                if (height - Size.Y > 0)
                    _skipLines++;
                else
                    FocusDirection = FocusDirection.Next;
            }
            if (e.Control.UiPrevButton.Pressed())
            {
                if (_skipLines > 0)
                    _skipLines--;
                else
                    FocusDirection = FocusDirection.Prev;
            }
        }

        public override void Draw(SceneIterationArgument e)
        {
            if (!IsAllSet(Font, Text)) return;
            var offset = -_skipLines * Font.Height;
            var visibleLines = 0;
            foreach (var line in _lines)
            {
                if (Location.Y + offset < Location.Y)
                {
                    offset += Font.Height;
                    continue;
                }
                if (Location.Y + offset + Font.Height > Location.Y + Size.Y) break;
                e.Batch.DrawText(Font, line, new Vector2(Location.X, Location.Y + offset), TextColor);
                visibleLines++;
                offset += Font.Height;
            }
            if (FocusDirection == FocusDirection.None)
            {
                //Border
                e.Batch.Draw(BorderTexture, new Rectangle(Location.X, Location.Y, BorderWidth, Size.Y), BorderColor);
                e.Batch.Draw(BorderTexture, new Rectangle(Location.X, Location.Y, Size.X, BorderWidth), BorderColor);
                e.Batch.Draw(BorderTexture, new Rectangle(Location.X + Size.X - BorderWidth, Location.Y, BorderWidth, Size.Y), BorderColor);
                e.Batch.Draw(BorderTexture, new Rectangle(Location.X, Location.Y + Size.Y - BorderWidth, Size.X, BorderWidth), BorderColor);
                //Scrollbar
                var scrollHeight = visibleLines * Size.Y / _lines.Count;
                var invnisibleLines = _lines.Count - visibleLines;
                if (invnisibleLines == 0) return;
                var emptyScroll = Size.Y - scrollHeight;
                var scrollOffset = emptyScroll * _skipLines / invnisibleLines;
                e.Batch.Draw(BorderTexture, new Rectangle(Location.X + Size.X - 3, Location.Y + scrollOffset, 1, scrollHeight), BorderColor);
            }
        }
    }
}