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

namespace UIControl
{
    public static class Utility
    {

        public static Rectangle OffsetRect(this Rectangle rect, int x,int y)
        {
            var retval = new Rectangle(rect.X + x, rect.Y + y, rect.Width, rect.Height);
            return retval;
        }

        public static void DrawString(this Graphics g,
             string msg,
             Font font,
             Color fcolor,
             RectangleF rect,
             ContentAlignment textalign = ContentAlignment.MiddleCenter,
             Color? ShadowColor = null,
             Brush BackBrush = null,
             Pen BorderPen = null)
        {
            DrawString(g, msg, font, fcolor, rect.toRect(), textalign, ShadowColor, BackBrush, BorderPen);
        }

        public static void DrawRectangle(this Graphics g, Pen pen, RectangleF rect)
        {
            g.DrawRectangle(pen, rect.Left, rect.Top, rect.Width, rect.Height);
        }

        public static void DrawString(this Graphics g,
        string msg,
        Font font,
        Color fcolor,
        Rectangle rect,
        ContentAlignment textalign = ContentAlignment.MiddleCenter,
        Color? ShadowColor = null,
        Brush BackBrush = null,
        Pen BorderPen = null)
        {
            //배경색 지정되어 있다면 
            if (BackBrush != null) g.FillRectangle(BackBrush, rect);

            //테두리 지정되어 있다면
            if (BorderPen != null) g.DrawRectangle(BorderPen, rect);

            //그립자가 지정되있다면 별도 처리를 한다
            if (ShadowColor != null && ShadowColor != Color.Transparent)
            {
                SizeF fsize;
                var drawPT = GetTextPosition(g, msg, font, rect, textalign, out fsize);

                //그림자를 먼저 그린다
                g.DrawString(msg, font, new SolidBrush((Color)ShadowColor), drawPT.X + 1, drawPT.Y + 1);
                g.DrawString(msg, font, new SolidBrush(fcolor), drawPT.X, drawPT.Y);
            }
            else
            {
                var sf = new StringFormat();

                if (textalign == ContentAlignment.BottomCenter) { sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Near; }
                else if (textalign == ContentAlignment.BottomLeft) { sf.Alignment = StringAlignment.Near; sf.LineAlignment = StringAlignment.Near; }
                else if (textalign == ContentAlignment.BottomRight) { sf.Alignment = StringAlignment.Far; sf.LineAlignment = StringAlignment.Near; }
                else if (textalign == ContentAlignment.MiddleCenter) { sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; }
                else if (textalign == ContentAlignment.MiddleLeft) { sf.Alignment = StringAlignment.Near; sf.LineAlignment = StringAlignment.Center; }
                else if (textalign == ContentAlignment.MiddleRight) { sf.Alignment = StringAlignment.Far; sf.LineAlignment = StringAlignment.Center; }
                else if (textalign == ContentAlignment.TopCenter) { sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Far; }
                else if (textalign == ContentAlignment.TopLeft) { sf.Alignment = StringAlignment.Near; sf.LineAlignment = StringAlignment.Far; }
                else if (textalign == ContentAlignment.TopRight) { sf.Alignment = StringAlignment.Far; sf.LineAlignment = StringAlignment.Far; }
                else { sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; }


                g.DrawString(msg, font, new SolidBrush(fcolor), rect, sf);

                sf.Dispose();
            }
        }

        public static void DrawRect(this Graphics g, Rectangle rect, Color color, Color shadowColor, int pensize = 1)
        {
            //우측하단에 선을 그려준다. 일단은 검은색
            g.DrawLine(new Pen(shadowColor, pensize), rect.Left + 1, rect.Bottom + 1, rect.Right - 1, rect.Bottom + 1);
            g.DrawLine(new Pen(shadowColor, pensize), rect.Right + 1, rect.Top + 1, rect.Right + 1, rect.Bottom - 1);
            DrawRect(g, rect, color, pensize);
        }
        public static void DrawRect(this Graphics g, Rectangle rect, Color color, int pensize = 1)
        {
            g.DrawRectangle(new Pen(color, pensize), rect);
        }

        public static void DrawRect(this Graphics g, RectangleF rect, Color color, int pensize = 1, Boolean shadow = false)
        {
            g.DrawRectangle(new Pen(color, pensize), rect.Left, rect.Top, rect.Width, rect.Height);
        }

        /// <summary>
        /// Rectangle 개체를 반환 합니다. 단순 float -> int 로 값을 변환 합니다.
        /// </summary>
        /// <param name="rect"></param>
        /// <returns></returns>
        public static Rectangle toRect(this RectangleF rect)
        {
            return new Rectangle((int)rect.Left, (int)rect.Top, (int)rect.Width, (int)rect.Height); ;
        }

        static PointF GetTextPosition(Graphics g, string data, Font f, Rectangle rect, ContentAlignment align, System.Windows.Forms.Padding Padding, out SizeF fsize)
        {
            float x = 0;
            float y = 0;
            fsize = g.MeasureString(data, f);
            if (align == ContentAlignment.MiddleCenter)
            {
                x = (rect.Width - fsize.Width) / 2 + Padding.Left - Padding.Right;
                y = (rect.Height - fsize.Height) / 2 + Padding.Top - Padding.Bottom;
            }
            else if (align == ContentAlignment.MiddleLeft)
            {
                x = Padding.Left;
                y = (rect.Height - fsize.Height) / 2;
            }
            else if (align == ContentAlignment.MiddleRight)
            {
                x = rect.Width - fsize.Width - Padding.Right;
                y = (rect.Height - fsize.Height) / 2;
            }
            else if (align == ContentAlignment.BottomLeft)
            {
                x = Padding.Left;
                y = rect.Height - fsize.Height - Padding.Bottom;
            }
            else if (align == ContentAlignment.BottomRight)
            {
                x = rect.Width - fsize.Width - Padding.Right;
                y = rect.Height - fsize.Height - Padding.Bottom;
            }
            else if (align == ContentAlignment.BottomCenter)
            {
                x = (rect.Width - fsize.Width) / 2;
                y = rect.Height - fsize.Height - Padding.Bottom;
            }
            else if (align == ContentAlignment.TopLeft)
            {
                x = Padding.Left;
                y = Padding.Top;
            }
            else if (align == ContentAlignment.TopRight)
            {
                x = rect.Width - fsize.Width - Padding.Right;
                y = Padding.Top;
            }
            else if (align == ContentAlignment.TopCenter)
            {
                x = (rect.Width - fsize.Width) / 2;
                y = Padding.Top;
            }
            return new PointF(x + rect.Left, y + rect.Top);
        }
        static PointF GetTextPosition(Graphics g, string data, Font f, Rectangle rect, ContentAlignment align, out SizeF fsize)
        {
            return GetTextPosition(g, data, f, rect, align, new System.Windows.Forms.Padding(0), out fsize);
        }
    }
}
