using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameCardGenerator
{
    class GameCards
    {
        public static Bitmap Create(
            int width, int height,
            string title, string cost, string guid,
            Color titleColor, Color costColor, Color guidColor, Color qrColor,
            Font titleFont, Font costFont, Font guidFont,
            int qrEccLevel, int qrPpm

        )
        {
            Bitmap bitmap = new Bitmap(width, height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                g.Clear(Color.White);

                int w = width, h = height;

                // Outline Border
                Pen borderPen = new Pen(qrColor, 3.0f);
                g.DrawRectangle(borderPen, 1, 1, w - 3, h - 3);

                // Top Background
                float titleBarHeight = h / 7;
                g.FillRectangle(borderPen.Brush, 2, 2, w - 4, titleBarHeight);

                // Title Text
                if (title != "")
                {
                    SizeF titleSize = g.MeasureString(title, titleFont);
                    float titlePadding = (titleBarHeight - titleSize.Height) / 2.0f + 3;
                    PointF titlePoint = new PointF(w / 2 - titleSize.Width / 2, titlePadding);
                    g.DrawString(title, titleFont, new SolidBrush(titleColor), titlePoint);
                }
                
                QRCoder.QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
                QRCoder.QRCodeData qrData = qrGenerator.CreateQrCode(guid, (QRCoder.QRCodeGenerator.ECCLevel)qrEccLevel);
                QRCoder.QRCode qrCode = new QRCoder.QRCode(qrData);
                Bitmap qrBitmap = qrCode.GetGraphic(qrPpm, qrColor, Color.White, false);
                PointF qrPoint = new PointF(w - qrBitmap.Width - titleBarHeight / 2, h / 2 - qrBitmap.Height / 2 + titleBarHeight / 2 - 9);
                g.DrawImage(qrBitmap, qrPoint);

                // Cost Text
                if (cost != "")
                {
                    SizeF costSize = g.MeasureString(cost, costFont);

                    while (costSize.Width < qrPoint.X)
                    {
                        costFont = new Font(costFont.FontFamily, costFont.Size + 0.25f, costFont.Style);
                        costSize = g.MeasureString(cost, costFont);
                    }

                    while (costSize.Width > qrPoint.X || costSize.Height > h - titleBarHeight)
                    {
                        costFont = new Font(costFont.FontFamily, costFont.Size - 0.25f, costFont.Style);
                        costSize = g.MeasureString(cost, costFont);
                    }

                    PointF costPoint = new PointF(qrPoint.X / 2 - costSize.Width / 2, h / 2 - costSize.Height / 2 + titleBarHeight / 2 + 3);
                    g.DrawString(cost, costFont, new SolidBrush(costColor), costPoint);
                }

                // Guid Text
                if (guid != "")
                {
                    SizeF guidSize = g.MeasureString(guid, guidFont);
                    PointF guidPoint = new PointF(w / 2 - guidSize.Width / 2, h - guidSize.Height - 6);
                    g.DrawString(guid, guidFont, new SolidBrush(guidColor), guidPoint);
                }
            }

            return bitmap;
        }
    }
}