using CPF;
using CPF.Controls;
using CPF.Drawing;
using OVCS.ClientLinux.Tools;

namespace OVCS.ClientLinux
{
    [CPF.Design.DesignerLoadStyle("res://OVCS.ClientLinux/Resource/Stylesheet1.css")]//用于设计的时候加载样式

    /// <summary>
    /// 表情列表 渲染控件
    /// </summary>
    public class EmotionPanel : Control
    {

        public event System.Action<int, Image> EmotionClicked;

        public WrapPanel wrapPanel { set; get; }
        
        protected override void InitializeComponent()
        {//模板定义
            Children.Add(new WrapPanel
            {
                BorderThickness = new Thickness(2, 2, 2, 2),
                BorderType = BorderType.BorderThickness,
                BorderFill = "#eee",
                PresenterFor = this,
                Name = nameof(this.wrapPanel),
                Width = 290,
                Background = "#fff",
            });
            if (!DesignMode)//设计模式下不执行，也可以用#if !DesignMode
            {
                this.wrapPanel = FindPresenterByName<WrapPanel>(nameof(this.wrapPanel));

                //加载表情
                for (int i = 0; i < GlobalResourceManager.EmotionDictionary.Count; i++)
                {
                    this.wrapPanel.Children.Add(GetPicture(i));
                }
            }

        }

        
        //点击事件
        public void Picture_MouseDown(object sender, CPF.Input.MouseButtonEventArgs e)
        {
            Picture pic = (Picture)sender;
            Image bm = (Image)pic.Source;

            if (this.EmotionClicked != null)
            {
                this.EmotionClicked(System.Convert.ToInt32(pic.Tag), bm);
            }
        }

        public Picture GetPicture(int index)
        {
            Picture pic = new Picture();
            pic.Source = GlobalResourceManager.EmotionDictionary[index];
            pic.Width = 25;
            pic.Height = 25;
            pic.Stretch = Stretch.Fill;
            pic.MarginBottom = 3;
            pic.MarginTop = 3;
            pic.MarginLeft = 10;
            pic.Tag = index;
            pic.MouseDown += this.Picture_MouseDown;
            return pic;
        }
    }
}
