using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    /// <summary>
    /// 这是个工具类,为力求简短,只用了一个字母。
    /// </summary>
    class L
    {
        public static int randomSeed = 0;

        public static String FormatChoiceQuestionTextString(String srcString)
        {
            if (srcString == null)
                return null;
            if (srcString.Length == 0)
                return "";

            return srcString.Replace("\n", "").Replace("\r", "").Replace("\t", "").Replace(" ", "").Replace(" ", "").Replace("〓", "");
        }

        public static int randomMinValue = 0;

        //这个方法似乎有问题。是从Java中移植过来的。
        public static int[] GetRandomIntArray(int size)
        {
            int[] send = new int[size];

            for (int i = 0; i < send.Length; i++)
            {
                send[i] = i;
            }

            int temp1, temp2, temp3;
            Random random = new Random(L.randomSeed);
            L.randomSeed += 7;
            if (L.randomSeed >= int.MaxValue - 14) L.randomSeed = 0;

            for (int i = 0; i < send.Length; i++)// 随机交换10次。
            {
                temp1 = random.Next(randomMinValue, randomMinValue + 10) % (send.Length);// 随机产生一个位置。
                randomMinValue += 10;
                if (randomMinValue >= 1000) randomMinValue = 10;

                //System.Threading.Thread.Sleep(7);//否则得到的重复值太多,选7是因为它是质数。

                temp2 = random.Next(randomMinValue, randomMinValue + 10) % (send.Length);// 随机产生另一个位置。
                if (randomMinValue >= 1000) randomMinValue = 10;

                if (temp1 != temp2)
                {
                    temp3 = send[temp1];
                    send[temp1] = send[temp2];
                    send[temp2] = temp3;
                }
            }

            if (randomMinValue >= 1000) randomMinValue = 10;

            return send;
        }

        /// <summary>
        /// 将指定数字转换成字母(1→A,2→B...)。
        /// </summary>
        /// <param name="i">要转换成对应字母的数字。</param>
        public static string ConvertToAlpha(int i)
        {
            if (i < 1 || i > 26) return string.Empty;

            return ((char)((int)'A' + i - 1)).ToString() + ".";
        }

        /// <summary>
        /// 根据键盘上的按键值取对应字母。
        /// </summary>
        /// <param name="key">按键值。</param>
        public static string GetAlpha(Key key)
        {
            if (key >= Key.A && key <= Key.Z)
            {
                return ((char)((int)'A' + key - Key.A)).ToString();
            }

            return string.Empty;
        }

        /// <summary>
        /// 当需要覆盖被预览的图像文件时,会出现“文件被锁定”的问题。这是因为:
        /// BitmapImage没有Dispose()方法,系统虽然删除了image,但是图片文件仍然被当前进程占用着。
        /// 解决的办法是:不再给BitmapImage直赋filePath,而是先根据filePath读取图片的二进制格式,赋给BitmapImage的Source,这样就可以在图片读取完毕后关闭流。
        /// </summary>
        public static BitmapImage BuildBitmapImage(string filePath)
        {
            var bitmapImage = new BitmapImage();
            using (BinaryReader reader = new BinaryReader(File.Open(filePath, FileMode.Open)))
            {
                FileInfo fi = new FileInfo(filePath);
                byte[] bytes = reader.ReadBytes((int)fi.Length);
                reader.Close();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = new MemoryStream(bytes);
                bitmapImage.EndInit();
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
            }
            return bitmapImage;
        }
    }
}