using Oraycn.ChatRendering;
using OVCS.ClientWpf.Tools;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace OVCS.ClientWpf.Controls.ChatBox
{
    /// <summary>
    /// InnerChatBox.xaml 的交互逻辑  ChatBox.xaml 的内部使用
    /// </summary>
    partial class InnerChatBox : UserControl, IChatControl
    {
        public InnerChatBox()
        {
            InitializeComponent();
        }

        private IChatRender chatRender;

        /// <summary>
        /// 聊天列表渲染器  （添加记录到列表显示、以及列表触发的事件 等）
        /// </summary>
        internal IChatRender ChatRender
        {
            get
            {
                return chatRender;
            }
        }

        private ContentControl parent
        {
            get
            {
                ContentControl scrollViewer = this.Parent as ContentControl;
                return scrollViewer;
            }
        }

        #region IChatControl
        System.Drawing.Size IChatControl.RenderSize
        {
            get
            {
                return new System.Drawing.Size((int)parent?.ActualWidth, (int)parent?.ActualHeight);
            }
        }

        public event Action<System.Drawing.Point> WhenMouseMove;
        public event Action<System.Drawing.Point> WhenMouseLeave;
        public event Action<bool, System.Drawing.Point> WhenMouseDown;
        public event Action<bool, System.Drawing.Point> WhenMouseUp;
        public event Action<System.Drawing.Size> RenderSurfaceSizeChanged;
        public event Action<int> RenderSurfaceYScrollChanged;
        public event Action<bool> VisiableChanged;

        public void InvalidateSurface()
        {
            UiSafeInvoker.BeginActionOnUI(() =>
            {
                this.InvalidateVisual();
            });
        }

        public void SetRenderTotalHeight(int totalHeight)
        {
            UiSafeInvoker.BeginActionOnUI(() => {
                this.Height = totalHeight;
            });
        }
        #endregion


        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="chatRender">聊天消息渲染器。</param>

        public void Initialize(IChatRender chatRender)
        {
            this.chatRender = chatRender;
        }

        /// <summary>
        /// 初始化ChatRender
        /// </summary> 
        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
        }

        public void ChangeRenderSurfaceSize()
        {
            if (this.RenderSurfaceSizeChanged != null)
            {
                this.RenderSurfaceSizeChanged(new System.Drawing.Size((int)parent?.ActualWidth, (int)parent?.ActualHeight));
            }
        }

        public void ChangeSurfaceScrollOffsetY(int offsetY)
        {
            if (this.RenderSurfaceYScrollChanged != null)
            {
                this.RenderSurfaceYScrollChanged(offsetY);
            }
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            this.chatRender.Render(drawingContext);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (this.WhenMouseMove != null)
            {
                Point pt = e.GetPosition(this);
                this.WhenMouseMove(new System.Drawing.Point((int)pt.X, (int)pt.Y));
            }
        }

        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);
            if (this.WhenMouseLeave != null)
            {
                Point pt = e.GetPosition(this);
                this.WhenMouseLeave(new System.Drawing.Point((int)pt.X, (int)pt.Y));
            }
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.LeftButton == MouseButtonState.Pressed || e.RightButton == MouseButtonState.Pressed)
            {
                if (this.WhenMouseDown != null)
                {
                    Point pt = e.GetPosition(this);
                    this.WhenMouseDown(e.LeftButton == MouseButtonState.Pressed, new System.Drawing.Point((int)pt.X, (int)pt.Y));
                }
            }
        }

        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            base.OnMouseUp(e);
            if (this.WhenMouseUp != null)
            {
                Point pt = e.GetPosition(this);
                if (pt.X < 0 || pt.Y < 0)
                {
                    return;
                }
                this.WhenMouseUp(e.ChangedButton == MouseButton.Left, new System.Drawing.Point((int)pt.X, (int)pt.Y));
            }
        }


    }
}
