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

namespace Vortex.Tutorials.StyledLines {

    ///<summary>Demonstration of styled lines drawing</summary>
    public class StyledLines : Game {
        static readonly string HELP_TEXT_FORMAT = "Q, A : adjust line width ({0:F1})\n1 - 6: line styles\n\nEsc : exit";

        float _lineWidth = 4;
        int _strokeIndex = 0;
        LineStroke[] _strokes;

        public StyledLines() {
            Window.Size = new System.Drawing.Size(1024, 600);
        }

		protected override void Load() {
			_strokes = new LineStroke[] { LineStroke.Solid, LineStroke.Dotted, LineStroke.Dashed, LineStroke.DotDashed, LineStroke.Dual };
		}

        protected override void Update(GameTime time) {
            if (Keyboard.IsDown(Key.Q)) {
                _lineWidth += _lineWidth * time.FrameTime;
            }

            if (Keyboard.IsDown(Key.A)) {
                _lineWidth -= _lineWidth *time.FrameTime;
            }

            if (Keyboard.IsPressed(Key.D1)) _strokeIndex = 0;
            if (Keyboard.IsPressed(Key.D2)) _strokeIndex = 1;
            if (Keyboard.IsPressed(Key.D3)) _strokeIndex = 2;
            if (Keyboard.IsPressed(Key.D4)) _strokeIndex = 3;
            if (Keyboard.IsPressed(Key.D5)) _strokeIndex = 4;

            _lineWidth = Math.Max(1f, Math.Min(_lineWidth, 32.0f));
        }

        protected override void Render(Canvas2D canvas) {
            canvas.Clear(ColorU.Black);

            LineStyle style = new LineStyle(_lineWidth, _strokes[_strokeIndex]);

            canvas.DrawEllipse(50, 50, 150, 100, style, ColorU.Red);
            canvas.DrawRect(300, 50, 150, 100, style, ColorU.Green);

            canvas.DrawEllipse(50, 400, 150, 100, style, ColorU.Red.MultiplyAlpha(0.4f));
            canvas.DrawRect(300, 400, 150, 100, style, ColorU.Green.MultiplyAlpha(0.4f));

            using (canvas <= new Translation(650, 100)) {
                DrawRombus(canvas, style, ColorU.Yellow);
            }
            using (canvas <= new Translation(650, 450)) {
                DrawRombus(canvas, style, ColorU.Yellow.MultiplyAlpha(0.4f));
            }


            using (canvas <= new Translation(880, 100)) {
                DrawCross(canvas, style, ColorU.White);
            }
            using (canvas <= new Translation(880, 450)) {
                DrawCross(canvas, style, ColorU.White.MultiplyAlpha(0.4f));
            }


            Vector2[] points = new Vector2[90];
            for (int n = 0; n < 90; ++n ) {
                points[n] = new Vector2(50 + n * 10, 230 + (float)(Math.Sin(n / 5f + Time.TotalGameTime) * 50f));
            }

            canvas.DrawPolyline(points, style, ColorU.Blue);
            using (canvas <= new Translation(0, 50)) {
                canvas.DrawPolyline(points, style, ColorU.Blue.MultiplyAlpha(0.5f));
                using (canvas <= new Translation(0, 50)) {
                    canvas.DrawPolyline(points, style, ColorU.Blue.MultiplyAlpha(0.25f));
                }
            }

            canvas.DrawTextLayout(SpriteFont.Console, canvas.Region.Deflate(new Vector2(4, 4)), string.Format(HELP_TEXT_FORMAT, _lineWidth), TextLayout.Bottom, ColorU.White);
            
        }

        private static void DrawRombus(Canvas2D canvas, LineStyle style, ColorU color) {
            using (canvas <= new Rotation((float)Math.PI / 4)) {
                canvas.DrawRect(-50, -50, 100, 100, style, color);
            }
        }

        private static void DrawCross(Canvas2D canvas, LineStyle style, ColorU color) {
            canvas.DrawLine(-50, -50, 50, 50, style, color);
            canvas.DrawLine(50, -50, -50, 50, style, color);
        }
    }
}
