using CPF;
using CPF.Controls;
using CPF.Drawing;
using OMCS.Cpf;
using Oraycn.ChatRendering;
using OVCS.ClientLinux.Tools;

namespace OVCS.ClientLinux.Controls.ChatBox
{
    public class ChatBox : Control
    {
        /// <summary>
        /// 控件相关事件与方法
        /// </summary>
        public IChatControl ChatControl => innerChatBox;

        public ScrollViewer ScrollViewer;

        private InnerChatBox innerChatBox;  

        //模板定义
        protected override void InitializeComponent()
        {
            Size = SizeField.Fill;
            Children.Add(new ScrollViewer()
            {
                Name = nameof(this.ScrollViewer),
                PresenterFor = this,
                Size = SizeField.Fill,
                BorderThickness = new Thickness(1),
                BorderFill = Color.FromRgb(231, 231, 231),
                BorderType = BorderType.BorderThickness,
                Content = new InnerChatBox
                {
                    Name = nameof(innerChatBox),
                    Size = SizeField.Fill,
                    MarginTop = 0,
                    PresenterFor = this,
                    FontSize = 14,
                    IsAntiAlias = true,
                },
            });
            SetControlEvent();
        }

        private void SetControlEvent()
        {
            this.ScrollViewer = this.FindPresenterByName<ScrollViewer>(nameof(this.ScrollViewer));
            this.innerChatBox = this.FindPresenterByName<InnerChatBox>(nameof(this.innerChatBox));
            this.innerChatBox.Parent = this;
            this.ScrollViewer.PropertyChanged += ScrollViewer_PropertyChanged;
            this.ScrollViewer.MouseDown += ScrollViewer_MouseDown;
            this.MouseWheel += ChatBox_MouseWheel;
        }

        private void ScrollViewer_MouseDown(object sender, CPF.Input.MouseButtonEventArgs e)
        {
            this.innerChatBox.OnMouseDown2(e);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="chatRender">聊天消息渲染器</param>
        public void Initialize(IChatRender chatRender)
        {
            this.innerChatBox.Initialize(chatRender);
        }

        /// <summary>
        /// 更改渲染器尺寸
        /// </summary>
        public void ChangeRenderSurfaceSize()
        {
            this.innerChatBox.ChangeRenderSurfaceSize();
        }

        /// <summary>
        /// 内容滚动到最下方
        /// </summary>
        public void ScrollToEnd()
        {
            UiSafeInvoker.ActionOnUI(() => { ScrollViewer.VerticalOffset = ScrollViewer.ExtentHeight - ScrollViewer.ViewportHeight; });
        }

        private void ChatBox_MouseWheel(object sender, CPF.Input.MouseWheelEventArgs e)
        {
            this.innerChatBox.ChangeSurfaceScrollOffsetY((int)this.ScrollViewer.VerticalOffset);
            this.innerChatBox.InvalidateSurface();
        }

        private void ScrollViewer_PropertyChanged(object sender, CPFPropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ActualSize")
            {
                this.innerChatBox.ChangeRenderSurfaceSize();
                this.innerChatBox.InvalidateSurface();
                return;
            }
            if (e.PropertyName == "VerticalOffset")
            {
                this.innerChatBox.ChangeSurfaceScrollOffsetY((int)this.ScrollViewer.VerticalOffset);
                this.innerChatBox.InvalidateSurface();
                return;
            }
        }
    }
}
