using qtc_net_client_2.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace qtc_net_client_2.Controls
{
    public class ChatMessageControl : Control
    {
        public Image? Avatar { get; set; }
        public string Username = "Username";
        public string Message = "Message";

        private Font usernameFont = new("Segoe UI", 9, FontStyle.Bold);
        private Font messageFont = new("Segoe UI", 9, FontStyle.Regular);
        private Font messageFontBold = new("Segoe UI", 10, FontStyle.Bold);

        public ChatMessageControl()
        {
            DoubleBuffered = true;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            int avatarSize = 32;
            int padding = 6;

            // first, draw the avatar
            Rectangle pfpRect = new(padding, padding, avatarSize, avatarSize);
            e.Graphics.DrawImage(Avatar ?? Resources.DefaultPfp, pfpRect);

            // then draw the username
            int textLeft = padding * 2 + avatarSize;
            int textWidth = Width - textLeft - padding;

            Rectangle usernameRect = new(textLeft, padding, textWidth, 20);
            e.Graphics.DrawString(Username, usernameFont, Brushes.Black, usernameRect);

            // finally draw the message
            int messageTop = usernameRect.Bottom + 2;

            Rectangle messageRect = new(textLeft, messageTop, textWidth, Height - messageTop - padding);
            StringFormat fmt = new()
            {
                Trimming = StringTrimming.Word,
                FormatFlags = 0,
                Alignment = StringAlignment.Near
            };

            if (Username.Contains("Server"))
            {
                e.Graphics.DrawString(Message, messageFontBold, Brushes.Black, messageRect, fmt);
                return;
            }

            e.Graphics.DrawString(Message, messageFont, Brushes.Black, messageRect, fmt);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Height = CalculateHeight(Width);
        }

        public int CalculateHeight(int width)
        {
            int padding = 6;
            int avatarSize = 32;
            int textLeft = padding * 2 + avatarSize;
            int textWidth = width - textLeft - padding;

            using (Graphics g = CreateGraphics())
            {
                SizeF usernameSize = g.MeasureString(Username, usernameFont, textWidth);
                SizeF messageSize = g.MeasureString(Message, messageFont, textWidth);

                int totalHeight = padding + (int)usernameSize.Height + 2 + (int)messageSize.Height + padding + 5;
                return Math.Max(totalHeight, avatarSize + 2 * padding);
            }
        }
    }

    public class ChatMessageControlMinimal : Control
    {
        public string Username = "Username";
        public string Message = "Message";

        private Font usernameFont = new("Segoe UI", 9, FontStyle.Bold);
        private Font messageFont = new("Segoe UI", 9, FontStyle.Regular);

        public ChatMessageControlMinimal()
        {
            DoubleBuffered = true;
            MinimumSize = new Size(150, 20);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            int padding = 6;

            // first draw the username
            int textWidth = Width - padding * 2;

            Rectangle usernameRect = new(padding, padding, textWidth, 20);
            e.Graphics.DrawString(Username, usernameFont, Brushes.Black, usernameRect);

            // finally draw the message
            int messageTop = usernameRect.Bottom + 2;

            Rectangle messageRect = new(padding, messageTop, textWidth, Height - messageTop - padding);
            StringFormat fmt = new()
            {
                Trimming = StringTrimming.Word,
                FormatFlags = 0,
                Alignment = StringAlignment.Near
            };

            e.Graphics.DrawString(Message, messageFont, Brushes.Black, messageRect, fmt);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Height = CalculateHeight(Width);
        }

        public int CalculateHeight(int width)
        {
            int padding = 6;
            int textWidth = width - padding * 2;

            using (Graphics g = CreateGraphics())
            {
                SizeF usernameSize = g.MeasureString(Username, usernameFont, textWidth);
                SizeF messageSize = g.MeasureString(Message, messageFont, textWidth);

                int totalHeight = padding + (int)usernameSize.Height + 2 + (int)messageSize.Height + padding + 5;
                return totalHeight;
            }
        }
    }
}
