using System;
using System.Collections.Generic;
using System.Text;
using Vortex;
using Vortex.Drawing;

namespace Vortex.Tutorials.TextDrawing {

    ///<summary>Vortex2D.NET example how to work with text</summary>
    class TextDrawing : Game{
        //define long text constant objects
        readonly string LONG_TEXT = "What is the name for a sentence that contains all 26 letters of the alphabet?\n\nThis type of sentence is called a pangram. The most widely known pangram is the one that's traditionally been used to test typewriters or keyboards: \"The quick brown fox jumps over the lazy dog\". Some letters in this sentence are used more than once: there's no ready example of a sentence that uses all the letters of the alphabet once only.";
        readonly Rect LONG_TEXT_RECT = Rect.FromBox(20, 160, 600, 300);
        
        SpriteFont _TimeNewRomanFont;
        SpriteFont _TahomaFont;
        SpriteFont _CorridaFont;

        SpriteFont[] _SpriteFonts;

        //Prepare all possible layouts
        TextLayout[] _Layouts = new TextLayout[] { 
            TextLayout.Left, TextLayout.Center, TextLayout.Right,
            TextLayout.Left | TextLayout.Middle, TextLayout.Center | TextLayout.Middle, TextLayout.Right | TextLayout.Middle,
            TextLayout.Left | TextLayout.Bottom, TextLayout.Center | TextLayout.Bottom, TextLayout.Right | TextLayout.Bottom
        };

        protected override void Load () {
            //loading fonts... Only font name is required, no extensions
			_TimeNewRomanFont = new SpriteFont(@"{fonts}\Times New Roman, 24px");
			_TahomaFont = new SpriteFont(@"{fonts}\Tahoma, 24px");
            _CorridaFont = new SpriteFont(@"{fonts}\CorridaC, 28px");

            //Preparing font carousel, note Game.ConsoleFont is built in font accessible always
            _SpriteFonts = new SpriteFont[] { _TimeNewRomanFont, _TahomaFont, _CorridaFont, SpriteFont.Console };
        }

        protected override void Unload () {
            //freeing of all fonts
            _TimeNewRomanFont.Dispose();
            _TahomaFont.Dispose();
            _CorridaFont.Dispose();

        }

        protected override void Render (Canvas2D canvas) {
            canvas.Clear(ColorU.Navy);
            //simple string drawing without any layout, but supporting \n characters
            canvas.DrawText(_TimeNewRomanFont, new Vector2(20, 50), "The quick brown fox jumps over the lazy dog, 1234567890 () +-*/ ,:? ...", ColorU.White);

            //taking current layout
            TextLayout layout = GetLayout();

            
            string infoText = "Text Layout: " + layout.ToString();
            //calculating size of layout information string
            Vector2 infoTextSize = Canvas2D.MeasureText(SpriteFont.Console, infoText);
            //drawing of text layout information
            canvas.DrawText(SpriteFont.Console, new Vector2(20, 120), "Text Layout: " + layout.ToString(), ColorU.Yellow);
            //drawing layout information text bounding rect
            canvas.DrawRect(20, 120, infoTextSize.X, infoTextSize.Y, ColorU.Red);

            //shade text bounding region
            canvas.DrawFilledRect(LONG_TEXT_RECT, ColorU.White.MultiplyAlpha(0.2f));

            //drawing of some font and text with layout specified
            canvas.DrawTextLayout(GetFont(), LONG_TEXT_RECT, LONG_TEXT, layout, ColorU.LightCyan);
        }

        ///<summary>Picks some layout from carousel</summary>
        private TextLayout GetLayout () {
            return _Layouts[(int)(Time.TotalGameTime * 0.5f % _Layouts.Length)];
        }

        ///<summary>Picks some font from carousel</summary>
        private SpriteFont GetFont () {
            return _SpriteFonts[(int)(Time.TotalGameTime * 0.25 % _SpriteFonts.Length)];
        }
    }

}
