using ESBasic;
using OVCS.ClientWpf.Tools;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace OVCS.ClientWpf
{
    /// <summary>
    /// 表情列表 渲染控件
    /// </summary>
    public partial class EmotionPanel : UserControl
    {
        private List<ImageSource> emotionList;
        public List<ImageSource> EmotionList
        {
            get { return emotionList; } 
        }
        private Dictionary<int , ImageSource> emojiDic = new Dictionary<int, ImageSource>();
        public Dictionary<int, ImageSource> EmojiDic
        {
            get { return emojiDic; }
        }
        public event CbGeneric<int, ImageSource> EmotionClicked;
        public EmotionPanel()
        {
            InitializeComponent();
            
            this.LoadEmotion();
            this.Initialize();
        }

        public void Initialize()
        {
            for (int i = 0; i < this.EmotionList.Count; i++)
            {
                var img = new Image();
                img.Source = this.EmotionList[i];
                img.Width = 25;
                img.Height = 25;
                img.Margin = new Thickness(3, 3, 0, 0);
                img.Tag = i;
                img.PreviewMouseLeftButtonDown += Img_PreviewMouseLeftButtonDown;
                this.Emotion_WrapPanel.Children.Add(img);
            }
        }
        private void Img_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var img = (Image)sender;
            var imageSource = img.Source;


            if (this.EmotionClicked != null)
            {
                this.EmotionClicked(Convert.ToInt32(img.Tag), imageSource);
            }
        }
       
        /// <summary>
        /// 加载表情资源
        /// </summary> 
        private void LoadEmotion()
        {
            List<string> tempList = ESBasic.Helpers.FileHelper.GetOffspringFiles(AppDomain.CurrentDomain.BaseDirectory + "Emotion\\");
            List<string> emotionFileList = new List<string>();
            foreach (string file in tempList)
            {
                string name = file.ToLower();
                if (name.EndsWith(".bmp") || name.EndsWith(".jpg") || name.EndsWith(".jpeg") || name.EndsWith(".png") || name.EndsWith(".gif"))
                {
                    emotionFileList.Add(name);
                }
            }
            emotionFileList.Sort(new Comparison<string>(CompareEmotionName));
            emotionList = new List<ImageSource>();
            for (int i = 0; i < emotionFileList.Count; i++)
            {
                emotionList.Add((System.Drawing.Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Emotion\\" + emotionFileList[i])).GetImageSource(""));
                emojiDic.Add(i,(System.Drawing.Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Emotion\\" + emotionFileList[i])).GetImageSource(""));
            }
        }

        private static int CompareEmotionName(string a, string b)
        {
            if (a.Length != b.Length)
            {
                return a.Length - b.Length;
            }

            return a.CompareTo(b);
        }
    }
}
