using CPF;
using CPF.Controls;
using CPF.Drawing;
using CPF.Input;
using OMCS.Cpf;
using OMCS.Linux;
using OMCS.Passive;
using OMCS.Passive.RemoteDesktop;
using OVCS.ClientLinux.Tools;

namespace OVCS.ClientLinux
{
    public class BigDesktop : Window
    {
        private DynamicDesktopConnector desktopConnector = new DynamicDesktopConnector();
        private MultiVideoChatContainer multiVideoChatContainer;

        protected override void InitializeComponent()
        {
            LoadStyleFile("res://OVCS.ClientLinux/Stylesheet1.css");//加载样式文件，文件需要设置为内嵌资源
            this.IsAntiAlias = true;
            this.Title = "远程桌面";
            this.BorderFill = Color.LightGray;
            this.BorderThickness = "1";
            this.BorderType = BorderType.BorderThickness;
            this.Width = 1280;
            this.Height= 720;
            this.MinWidth = 1280;
            this.MinHeight = 720;
            this.Background = "#F7F7F7";
            this.CanResize = true;
            Children.Add(new Grid
            {
                Size = SizeField.Fill,
                Children = //内容元素放这里
                {
                    new Grid
                    {
                        Background = "30,159,255",
                        Size = SizeField.Fill,
                        Children =
                        {
                            new Picture
                            {
                                Width = 24,
                                Height = 24,
                                MarginLeft = 10,
                                Source = CommonHelper.ResourcesCatalog+ "64.png",
                                Stretch = Stretch.UniformToFill
                            },
                            new TextBlock
                            {
                                Text = "远程桌面",
                                MarginLeft = 44,
                            },
                            new TextBlock
                            {
                                VerticalAlignment= VerticalAlignment.Center,
                                Text = "一",
                                MarginTop = 10,
                                MarginRight = 60,
                                Width = 20,
                                FontSize= 20,
                                Height = 20,ToolTip = "最小化",
                                Foreground = Color.White,
                                Cursor = Cursors.Hand,
                                Commands =
                                {
                                    {
                                         nameof(MouseDown),(s,e)=>{MouseButtonEventArgs eventArgs = (MouseButtonEventArgs)e;eventArgs.Handled = true;
                                            if (eventArgs.MouseButton != MouseButton.Left)
                                            {
                                                return;
                                            } this.WindowState = WindowState.Minimized; }
                                    }
                                }
                            },
                            new TextBlock
                            {
                                VerticalAlignment= VerticalAlignment.Center,
                                Text = "口",
                                Foreground = Color.White,
                                MarginRight = 30,
                                Width = 20 ,
                                MarginTop= 5,
                                FontSize= 20,
                                MarginBottom= 0,
                                Height = 20,ToolTip = "最大化",
                                Name = "windowState",
                                PresenterFor = this,
                                Cursor = Cursors.Hand,
                                Commands =
                                {
                                    {
                                         nameof(MouseDown),(s,e)=>{
                                            MouseButtonEventArgs eventArgs = (MouseButtonEventArgs)e;eventArgs.Handled = true;
                                            if (eventArgs.MouseButton != MouseButton.Left)
                                            {
                                                return;
                                            }
                                            if(WindowState != WindowState.Maximized)
                                             {
                                                 this.WindowState = WindowState.Maximized;
                                                 this.button_windowState.ToolTip = "还原";
                                             }
                                             else
                                             {   this.WindowState = WindowState.Normal;
                                                 this.button_windowState.ToolTip = "最大化";
                                             }
                                         }
                                    }
                                }
                            },
                            new TextBlock
                            {
                                VerticalAlignment= VerticalAlignment.Center,
                                Text = "X",ToolTip = "关闭",
                                Foreground = Color.White,
                                MarginRight = 0,
                                MarginTop= 5,
                                MarginBottom= 0,
                                FontSize= 20,
                                Width = 20,
                                Height = 20,
                                Cursor = Cursors.Hand,
                                Commands =
                                {
                                    {
                                         nameof(MouseDown),(s,e)=>{MouseButtonEventArgs eventArgs = (MouseButtonEventArgs)e;eventArgs.Handled = true;
                                            if (eventArgs.MouseButton != MouseButton.Left)
                                            {
                                                return;
                                            } this.Close(); }
                                    }
                                }
                            }
                        },
                        Commands =
                        {
                            {
                                nameof(MouseDown),(s,e)=>{ this.DragMove(); }
                            }
                        },
                        Attacheds =
                        {
                            {
                                Grid.RowIndex,0
                            }
                        }
                    },
                    new DesktopPanel
                    {
                        Size = SizeField.Fill,
                        Name = "fullDeskTop",
                        PresenterFor = this,
                        Attacheds =
                        {
                            {
                                Grid.RowIndex,1
                            }
                        }
                    }
                },
                RowDefinitions =
                {
                    new RowDefinition
                    {
                        Height = 30,
                    },
                    new RowDefinition
                    {

                    }
                }
            }); 
            if (!DesignMode)//设计模式下不执行，也可以用#if !DesignMode
            {  
                this.Icon = CommonHelper.ResourcesCatalog + "64.ico";
            } 
        }
        private DesktopPanel fullDeskTop;
        private TextBlock button_windowState;
        private string ownerID;
        protected override void OnInitialized()
        {
            base.OnInitialized();
            fullDeskTop = this.FindPresenterByName<DesktopPanel>("fullDeskTop");
            button_windowState = this.FindPresenterByName<TextBlock>("windowState");
            if (IsInitialized)
            {
                this.desktopConnector.VideoDrawMode = VideoDrawMode.Scale;
                this.desktopConnector.SetViewer(this.fullDeskTop);
                this.desktopConnector.ConnectEnded += DynamicDesktopConnector_ConnectEnded;
                this.desktopConnector.Disconnected += DynamicDesktopConnector_Disconnected;
                this.desktopConnector.BeginConnect(this.ownerID);
            }
        }
        public BigDesktop(string _ownerID, MultiVideoChatContainer _multiVideoChatContainer)
        {
            this.multiVideoChatContainer = _multiVideoChatContainer;
            this.ownerID = _ownerID;
            this.Closed += BigDesktop_Closed;
        }

        private void DynamicDesktopConnector_Disconnected(string obj1, ConnectorDisconnectedType obj2)
        {
            
        }
        private void DynamicDesktopConnector_ConnectEnded(string ownerID, ConnectResult res)
        {
            UiSafeInvoker.ActionOnUI(delegate
            {
                if (res != OMCS.Passive.ConnectResult.Succeed)
                {
                    MessageBox.Show(res.ToString());
                }
            });
        }

        private void BigDesktop_Closed(object sender, System.EventArgs e)
        {
            this.desktopConnector.Disconnect();
            this.multiVideoChatContainer.DeskTopBtnHitTestVisibleChange(true);
        }
    }
}
