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
{
    public class TextArea:Text,IDrawable,IClickable
    {
        Rectangle area;
        int MaxLength;
        List<String> strings;
        int maxRows;
        private int begin;
        private int Begin
        {
            get
            {
                return begin;
            }
            set
            {
                if (value>=0 && value + maxRows <= strings.Count)
                {
                    begin = value;
                }
            }
        }
        private int end;
        private int End
        {
            get
            {
                return end;
            }
            set
            {
                if (value <= strings.Count && value - maxRows >= 0)
                {
                    end = value;
                }
                if (value > strings.Count)
                {
                    end = strings.Count;
                }
            }
        }
        ClickableObject arrowUp;
        ClickableObject arrowDown;

        List<IDrawable> drawables;
        List<IClickable> clickables;
        public TextArea(SpriteFont f, string l, Rectangle p,Game1 game)
            : base(f, l, new Vector2(p.X,p.Y)) {
                drawables = new List<IDrawable>();
                clickables = new List<IClickable>();
                arrowUp = new ClickableObject(game.Content.Load<Texture2D>("up"), new Rectangle(p.X + p.Width + 5, p.Y, 16, 16),
                    delegate(MouseState state, ClickableObject o){Begin = Begin - 1; End = End - 1;});
                arrowDown = new ClickableObject(game.Content.Load<Texture2D>("down"), new Rectangle(p.X + p.Width + 5, p.Y+p.Height-16, 16, 16),
                        delegate(MouseState state, ClickableObject o) { Begin = Begin + 1; End = End + 1;});
                drawables.Add(arrowUp);
                drawables.Add(arrowDown);
                clickables.Add(arrowUp);
                clickables.Add(arrowDown);
                area = p;
                SplitString();
        }

        public Boolean isClicked(MouseState state)
        {
            return area.X <= state.X && (area.X + area.Width +21) >= state.X && //21 zaradi bytonite
                        area.Y <= state.Y && (area.Y + area.Height) >= state.Y;
        }

        public void Click(MouseState state)
        {
            foreach(IClickable element in clickables){
                if (element.isClicked(state))
                {
                    element.Click(state);
                    return;
                }
            }
        }

        private void SplitString()
        {
            String x = " ";
            MaxLength=0;
            while(font.MeasureString(x).X<area.Width){
                x = x + " ";
                MaxLength++;
            };
            maxRows = area.Height / (int) font.MeasureString(x).Y;
            int i=0;
            int j = 0;
            while (i < label.Length)
            {
                j++;
                if (label[i] == '\n')
                {
                    j = 0;
                }
                if (j == MaxLength)
                {
                    int k = i;
                    while (k>=0 && label[k] != ' ' && label[k] != '\n' )
                    {
                        k--;
                    }
                    if (k<0 || label[k] != ' ')
                    {
                        label = label.Insert(i, "\n");
                    }
                    else
                    {
                        label = label.Insert(k+1, "\n");
                    }
                    j = 0;
                }
                i++;
            }
            strings=new List<String>(label.Split('\n'));
            Begin = 0;
            End = maxRows;
        }

        public override void draw(SpriteBatch spriteBatch)
        {
            Vector2 vec;
            int j = 0;
            for (int i = Begin; i < End; i++)
            {
                vec = new Vector2(area.X,area.Y+font.MeasureString(strings[i]).Y*j);
                j++;
                spriteBatch.DrawString(font, strings[i], vec, Color.White);
            }
            foreach(IDrawable drawable in drawables){
                drawable.draw(spriteBatch);
            }
        }
    }
}
