using qtc_net_client_2.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace qtc_net_client_2.Controls
{
    public class RoomEntryControl : Control
    {
        public Image Image = Resources.RoomsChatIcon;
        public string RoomName = "Room";
        public bool HideUserCount = false;
        public int RoomUserCount = 0;

        private Font? nameFont;
        private Color nameColor;

        private Font userCountFont = new("Segoe UI", 9, FontStyle.Bold);
        private Color userCountColor = Color.Gray;

        private bool IsHoveredOn = false;

        public event EventHandler? OnRoomDoubleClicked;
        public RoomEntryControl()
        {
            // reduce flicker
            SetStyle(ControlStyles.AllPaintingInWmPaint |
                     ControlStyles.ResizeRedraw |
                     ControlStyles.UserPaint |
                     ControlStyles.OptimizedDoubleBuffer, true);

            Height = 36;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if(IsHoveredOn)
            {
                nameFont = new("Segoe UI", 9, FontStyle.Bold | FontStyle.Underline);
                nameColor = Color.LightGray;
            } else
            {
                nameFont = new("Segoe UI", 9, FontStyle.Bold);
                nameColor = Color.Black;
            }

            int margin = 6;
            int imageSize = 32;

            Rectangle imageRect = new(margin, (Height - imageSize) / 2, imageSize, imageSize);
            e.Graphics.DrawImage(Image, imageRect);

            int nameLeft = imageRect.Right + margin;

            Rectangle nameRect = new(nameLeft, 6, Width - nameLeft - 8, Height - 16);
            TextRenderer.DrawText(e.Graphics, RoomName, nameFont, nameRect, nameColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);

            string userCountText = RoomUserCount.ToString();
            Size countSize = TextRenderer.MeasureText(e.Graphics, userCountText, userCountFont);

            if(!HideUserCount)
            {
                int rightPad = 8;
                int countX = Width - countSize.Width - rightPad;
                int countY = (Height - countSize.Height) / 2;
                Point userCountPoint = new(countX, countY);

                TextRenderer.DrawText(e.Graphics, userCountText, userCountFont, userCountPoint, userCountColor, TextFormatFlags.Left);
            }
        }

        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            OnRoomDoubleClicked?.Invoke(this, EventArgs.Empty);
        }

        protected override void OnMouseHover(EventArgs e)
        {
            base.OnMouseHover(e);
            IsHoveredOn = true;
            Invalidate();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            IsHoveredOn = false;
            Invalidate();
        }
    }
}
