using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Windows.Forms;
using TheArtOfDev.HtmlRenderer.Adapters.Entities;

namespace lib
{
    class Utils
    {

        public static Dictionary<string, string> parseArrayData(Dictionary<string, string> cols, string[] data)
        {
            int i = 0;
            foreach (KeyValuePair<string, string> col in cols)
            {
                cols[col.Key] = data[i++];
            }

            return cols;
        }

        public static List<string> regexPattern(string input, string pattern)
        {
            List<string> output = new List<string>();
            MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.IgnoreCase);
            object[] words = new object[1024];

            int index = 0;
            foreach (Match match in matches)
            {
                match.Groups.CopyTo(words, index);
                index += 2;
            }

            // Display captured groups.
            for (int ctr = 1; ctr <= words.GetUpperBound(0); ctr += 2)
            {
                if (words[ctr] == null)
                    break;
                output.Add(words[ctr].ToString());
            }

            return output;
        }

        public static string getCsvFormatString(int cols)
        {
            string tmp = "";
            for (int i = 0; i < cols; i++)
            {
                tmp += (i > 0 ? "," : "") + "\"{" + i + "}\"";
            }

            return tmp;
        }

        static Random random = new Random();
        public static string GetRandomHexNumber(int digits = 16)
        {
            byte[] buffer = new byte[digits / 2];
            random.NextBytes(buffer);
            string result = String.Concat(buffer.Select(x => x.ToString("X2")).ToArray());
            if (digits % 2 == 0)
                return result;
            return result + random.Next(16).ToString("X");
        }





        /// <summary>
        /// Convert from WinForms point to core point.
        /// </summary>
        public static RPoint Convert(PointF p)
        {
            return new RPoint(p.X, p.Y);
        }

        /// <summary>
        /// Convert from WinForms point to core point.
        /// </summary>
        public static PointF[] Convert(RPoint[] points)
        {
            PointF[] myPoints = new PointF[points.Length];
            for (int i = 0; i < points.Length; i++)
                myPoints[i] = Convert(points[i]);
            return myPoints;
        }

        /// <summary>
        /// Convert from core point to WinForms point.
        /// </summary>
        public static PointF Convert(RPoint p)
        {
            return new PointF((float)p.X, (float)p.Y);
        }

        /// <summary>
        /// Convert from core point to WinForms point.
        /// </summary>
        public static Point ConvertRound(RPoint p)
        {
            return new Point((int)Math.Round(p.X), (int)Math.Round(p.Y));
        }

        /// <summary>
        /// Convert from WinForms size to core size.
        /// </summary>
        public static RSize Convert(SizeF s)
        {
            return new RSize(s.Width, s.Height);
        }

        /// <summary>
        /// Convert from core size to WinForms size.
        /// </summary>
        public static SizeF Convert(RSize s)
        {
            return new SizeF((float)s.Width, (float)s.Height);
        }

        /// <summary>
        /// Convert from core size to WinForms size.
        /// </summary>
        public static Size ConvertRound(RSize s)
        {
            return new Size((int)Math.Round(s.Width), (int)Math.Round(s.Height));
        }

        /// <summary>
        /// Convert from WinForms rectangle to core rectangle.
        /// </summary>
        public static RRect Convert(RectangleF r)
        {
            return new RRect(r.X, r.Y, r.Width, r.Height);
        }

        /// <summary>
        /// Convert from core rectangle to WinForms rectangle.
        /// </summary>
        public static RectangleF Convert(RRect r)
        {
            return new RectangleF((float)r.X, (float)r.Y, (float)r.Width, (float)r.Height);
        }

        /// <summary>
        /// Convert from core rectangle to WinForms rectangle.
        /// </summary>
        public static Rectangle ConvertRound(RRect r)
        {
            return new Rectangle((int)Math.Round(r.X), (int)Math.Round(r.Y), (int)Math.Round(r.Width), (int)Math.Round(r.Height));
        }

        /// <summary>
        /// Convert from WinForms color to core color.
        /// </summary>
        public static RColor Convert(Color c)
        {
            return RColor.FromArgb(c.A, c.R, c.G, c.B);
        }

        /// <summary>
        /// Convert from core color to WinForms color.
        /// </summary>
        public static Color Convert(RColor c)
        {
            return Color.FromArgb(c.A, c.R, c.G, c.B);
        }

        /// <summary>
        /// mono has issue throwing exception for no reason.
        /// </summary>
        /// <param name="control">the control to create graphics object from</param>
        /// <returns>new graphics object or null in mono if failed</returns>
        public static Graphics CreateGraphics(Control control)
        {
#if MONO
            try
            {
                return control.CreateGraphics();
            }
            catch
            {
                return null;
            }
#else
            return control.CreateGraphics();
#endif
        }


    }
}
