using CPF.Controls;
using CPF.Drawing;
using ESBasic.ObjectManagement.Managers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace OVCS.ClientLinux.Tools
{
    public class GlobalResourceManager
    {
        public static ObjectManager<string, Image> HeadPicManger;
        public static ObjectManager<string, Image> FileTypeManger; 
        public static Dictionary<int, object> EmotionDictionary; 
        public static Bitmap Icon64;

        public static void Initialize()
        { 
            #region HeadImage

            GlobalResourceManager.HeadPicManger = new ObjectManager<string, Image>();
            GlobalResourceManager.FileTypeManger = new ObjectManager<string, Image>();
            EmotionDictionary = new Dictionary<int, object>();
            //C#遍历指定文件夹中的所有文件
            DirectoryInfo directoryInfo = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "Head/");
            FileInfo[] files = directoryInfo.GetFiles("*.png");
            Image firstImage = null;
            for (int i = 0; i < files.Length; i++)
            {
                string headFilePath = AppDomain.CurrentDomain.BaseDirectory + $"Head/{i + 1}.png";
                if (File.Exists(headFilePath))
                {
                    GlobalResourceManager.HeadPicManger.Add((i + 1).ToString(), Image.FromFile(headFilePath));
                }
            }
            #endregion 

            #region FileIcon 

            string fileIconDirectory = AppDomain.CurrentDomain.BaseDirectory + "FileIcon/";
            DirectoryInfo fileDirectoryInfo = new DirectoryInfo(fileIconDirectory);
            FileInfo[] fileFiles = fileDirectoryInfo.GetFiles("*.png");
            for (int i = 0; i < fileFiles.Length; i++)
            {
                string fileRealPath = fileFiles[i].ToString();
                if (File.Exists(fileRealPath))
                {
                    Image image = Bitmap.FromFile(fileRealPath);
                    string name = fileFiles[i].Name;
                    int v = name.LastIndexOf("_");
                    int v1 = name.LastIndexOf(".");
                    string str = fileFiles[i].Name.Substring(v + 1, v1 - v - 1);
                    GlobalResourceManager.FileTypeManger.Add(str, image);
                }
            }

            #endregion

            #region Emotion

            string emotionDirectory = AppDomain.CurrentDomain.BaseDirectory + "Emotion/";
            DirectoryInfo emotionInfo = new DirectoryInfo(emotionDirectory);
            FileInfo[] fileInfos = emotionInfo.GetFiles("*.png");
            List<FileInfo> fileInfos1 = new List<FileInfo>(fileInfos);
            fileInfos1 = fileInfos1.OrderBy(x => x.Name).ToList();
            for (int i = 0; i < fileInfos1.Count; i++)
            {
                string path = fileInfos1[i].ToString();
                if (File.Exists(path))
                {
                    EmotionDictionary.Add(i, Bitmap.FromFile(path));
                }
            }

            #endregion
             

            GlobalResourceManager.Icon64 = CommonHelper.FindAssetsBitmap("64.png");
        }
    }
}
