using CPF.Controls;
using CPF.Documents;
using OVCS.ClientLinux.Tools;
using System.Text;

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

    //聊天面板控件
    //自定义的输入框控件
    public class Inputbox : TextBox
    {

        protected override void InitializeComponent()
        {
            base.InitializeComponent();
        }
        /// <summary>
        /// 将表情添加到输入框的焦点位置
        /// </summary> 
        public void AppentEmoji(int innerEmotionIndex)
        {
            int index = this.CaretIndex.Count == 0 ? 0 : (int)this.CaretIndex[0];
            this.Document.Children.Insert(index, this.CreateEmotionInlineUIContainer(innerEmotionIndex));
        }

        /// <summary>
        /// 获取输入框中的文本（所有表情均使用字符串表示，[000]表示的就是第一个表情,[001]即第二个表情，以此类推）
        /// </summary> 
        public string GetInputContent()
        {
            StringBuilder sb = new StringBuilder("");
            for (int i = 0; i < this.Document.Children.Count; i++)
            {
                IDocumentElement item = this.Document.Children[i];
                DocumentChar text = item as DocumentChar;
                InlineUIContainer image = item as InlineUIContainer;
                if (text != null)
                {
                    if (!string.IsNullOrWhiteSpace(text.Char.ToString()))
                    {
                        sb.Append(text.Char);
                    }
                    else if (text.Char == '\n')
                    {
                        sb.Append('\n');
                    }
                    else
                    {
                        sb.Append(' ');
                    }
                }
                if (image != null)
                {
                    Picture picture = image.UIElement as Picture;
                    string path = picture.Source.ToString();
                    int index1 = path.LastIndexOf(".");
                    int index2 = path.LastIndexOf(CommonHelper.SeparAtor);
                    string real = path.Substring(index2 + 1, index1 - index2 - 1);
                    int v = int.Parse(real) - 1;
                    string imgIndex = v.ToString().PadLeft(3, '0');
                    sb.Append($"[{imgIndex}]");
                }
            }
            string str = sb.ToString();
            return str.TrimEnd(' ');
        }

        /// <summary>
        /// 获取一个包含表情图片的InlineUIContainer控件
        /// </summary> 
        public InlineUIContainer CreateEmotionInlineUIContainer(int innerEmotionIndex)
        {
            string index = (innerEmotionIndex + 1).ToString();
            if (innerEmotionIndex < 9)
            {
                index = "0" + index;
            }
            string imgPath = $"{CommonHelper.EmotionPath}{index}.png";
            return new InlineUIContainer
            {
                UIElement = new Picture
                {
                    MarginTop = 4,
                    MarginRight = 2,
                    MaxWidth = 24,
                    MaxHeight = 24,
                    IsAntiAlias = true,
                    Stretch = Stretch.Uniform,
                    StretchDirection = StretchDirection.DownOnly,
                    MarginBottom = 0,
                    MarginLeft = 4,
                    Source = imgPath,
                    Tag = innerEmotionIndex
                }
            };
        }

        /// <summary>
        /// 清除聊天框内容
        /// </summary>
        public void Clear()
        {
            this.Document.Children.Clear();
            this.Text = string.Empty;
            this.ScrollToEnd();//将光标指针移动到最初的状态
        }
    }
}
