using Oraycn.ChatRendering;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace OVCS.ClientWpf.Controls.ChatBox
{
    /// <summary>
    /// ChatBox.xaml 的交互逻辑
    /// </summary>
    public partial class ChatBox : UserControl
    {
        /// <summary>
        /// 控件相关事件与方法
        /// </summary>
        public IChatControl ChatControl { get; private set; }

        public ScrollViewer ScrollViewer => this.scrollViewer;

        public ChatBox()
        {
            InitializeComponent();
            this.ChatControl = this.innerChatBox;           
        }


        /// <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()
        {
            this.scrollViewer.ScrollToEnd();
        }

        private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            this.innerChatBox.ChangeSurfaceScrollOffsetY((int)this.scrollViewer.VerticalOffset);
            this.innerChatBox.InvalidateSurface();
        }

        private void ScrollViewer_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.innerChatBox.ChangeRenderSurfaceSize();
            this.innerChatBox.InvalidateSurface();
        }

        private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            this.innerChatBox.ChangeSurfaceScrollOffsetY((int)this.scrollViewer.VerticalOffset);
            this.innerChatBox.InvalidateSurface();
        }
    }
}
