using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Ycjw.PipeLine.WPF
{
    /****************************************************************************************/
    /*
    功    能:放置三维场景的DocWindow
    作    者: RenZachary
    联系方式: rzcgis@foxmail.com（QQ455745253）
    生成日期: 2021年3月24日
    修改日志: 
    版权说明: @2021 西安云创经纬信息科技有限公司 版权所有 All right reserved.
    注    意: 本内容仅限于云创经纬公司内部使用，禁止用于商业目的。
    */
    /****************************************************************************************/
    /// <summary>
    /// BForm.xaml 的交互逻辑
    /// </summary>
    public partial class BForm : Window
    {
        readonly IResposeWPF m_IResposeWPF;
        public BForm(IResposeWPF respose)
        {
            InitializeComponent();
            Init();
            m_IResposeWPF = respose;
        }
        public System.Windows.Shapes.Ellipse EllipseAll
        {
            get
            {
                return m_EllipseAll;
            }
        }
        public System.Windows.Shapes.Ellipse EllipseWS
        {
            get
            {
                return m_EllipseDL;
            }
        }
        public System.Windows.Shapes.Ellipse EllipseYS
        {
            get
            {
                return m_EllipseDX;
            }
        }

        public System.Windows.Controls.Image ImageInfo
        {
            get
            {
                return m_ImageInfo;
            }
        }
        public System.Windows.Controls.Image ImagePan
        {
            get
            {
                return m_ImagePan;
            }
        }
        public System.Windows.Controls.Image ImageTerrain
        {
            get
            {
                return m_ImageTerrain;
            }
        }

        public void SetInfo(string infoName,NameValueCollection nvc)
        {
            if (this.Visibility == Visibility.Hidden) return;
            if (nvc.Count == 0)
            {
                m_InfoGrid.Visibility = Visibility.Hidden;
                return;
            }
            InfoScrollViewer.Height = this.m_InfoBorder.ActualHeight - 40;
            m_InfoGrid.Visibility = Visibility.Visible;
            m_InfoItems.Children.Clear();
            m_InfoTitle.Content = infoName;
            for (int i = 0;i < nvc.Count; i++)
            {
                var nvKey = nvc.GetKey(i);
                var nvValue = nvc.Get(i);
                WPF.PropertiesEx prop = new WPF.PropertiesEx(nvKey, nvValue);
                m_InfoItems.Children.Add(prop);
            }
        }
        void SetImageSource(System.Windows.Controls.Image image,string fileName)
        {
            var bitmap = new Bitmap(Commons.Paths.ApplicationResourcesDir + "\\" + fileName);
            image.Source = BitmapToBitmapSource(bitmap);
        }
        void Init()
        {
            SetImageSource(m_ImageExist, "exit.png");
            m_ImageExist.MouseUp += ImageExist_MouseUp;

            SetImageSource(m_ImageHome, "home.png");
            m_ImageHome.MouseUp += ImageHome_MouseUp;

            SetImageSource(m_ImageInfo, "info.png");

            SetImageSource(m_ImagePan, "PanInBigScreen.png");

            SetImageSource(m_ImageTerrain, "terrain.png");

            InfoScrollViewer.MouseEnter += InfoScrollViewer_MouseEnter;
            InfoScrollViewer.MouseLeave += InfoScrollViewer_MouseLeave;

            Grid oo = this.Content as Grid;
            UIElementCollection Childrens = oo.Children;
            foreach (UIElement ui in Childrens)
            {
                var gds = FindVisualChildren<Grid>(ui);
                foreach (var gd in gds)
                {
                    if (gd.Tag != null && gd.Tag.ToString() == "BTN")
                    {
                        gd.MouseEnter += Grid_MouseEnter;
                        gd.MouseLeave += Grid_MouseLeave;
                    }
                    if(gd.Parent is StackPanel)
                    {
                        if((gd.Parent as StackPanel).Name.StartsWith("TopContext"))
                        {
                            gd.Opacity = .5;
                            gd.MouseEnter += Grid_MouseEnter;
                            gd.MouseLeave += Grid_MouseLeave;
                        }
                    }
                }
                var ee = FindVisualChildren<System.Windows.Shapes.Ellipse>(ui);
                foreach(var e in ee)
                {
                    if (e.Tag != null && e.Tag.ToString() == "TYPES")
                    {
                        e.Opacity = .3;                        
                        e.Cursor = System.Windows.Input.Cursors.Hand;
                        e.MouseEnter += Ellipse_MouseEnter;
                        e.MouseLeave += Ellipse_MouseLeave;
                        if (e.Parent is Grid)
                        {
                            Grid g = e.Parent as Grid;
                            string path = Commons.Paths.ApplicationResourcesDir + "\\" + e.Name.Replace("m_","") + ".png";
                            if (!System.IO.File.Exists(path)) continue;
                            var bitmap = new Bitmap(path);
                            g.Background = new System.Windows.Media.ImageBrush(BitmapToBitmapSource(bitmap));
                        }
                    }
                }
            }

        }

        private void Sp_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            StackPanel sp = sender as StackPanel;
            sp.Opacity = .5;
        }

        private void Sp_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            StackPanel sp = sender as StackPanel;
            sp.Opacity = 1;
        }

        private void Grid_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Grid gd = sender as Grid;
            gd.Opacity = .5;
        }

        private void Grid_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Grid gd = sender as Grid;
            gd.Opacity = 1;
        }

        public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T t)
                    {
                        yield return t;
                    }

                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }

        private void InfoScrollViewer_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            InfoScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
        }

        private void InfoScrollViewer_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            InfoScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
        }

        private void Ellipse_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            var ell = sender as System.Windows.Shapes.Ellipse;
            if (m_EllipseOpacity == -1) return;
            ell.Opacity = m_EllipseOpacity;

            var tb = ell.FindName(ell.Name.Replace("Ellipse", "TextBlock"));
            if (tb == null) return;
            (tb as TextBlock).Foreground = System.Windows.Media.Brushes.Gray;
            (tb as TextBlock).FontWeight = FontWeights.Normal;
        }

        double m_EllipseOpacity = -1;
        private void Ellipse_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            var ell = sender as System.Windows.Shapes.Ellipse;
            if (m_EllipseOpacity == -1) m_EllipseOpacity = ell.Opacity;
            ell.Opacity = .6;

            var tb = ell.FindName(ell.Name.Replace("Ellipse", "TextBlock"));
            if (tb == null) return;
            (tb as TextBlock).Foreground = System.Windows.Media.Brushes.White;
            (tb as TextBlock).FontWeight = FontWeights.Bold;
        }

        private void ImageHome_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.m_IResposeWPF.ResposeWPF("VIEWENTIRE");
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            if (e.Key == Key.Escape)
                this.Hide();
        }
        private void ImageExist_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.Hide();
        }

        //public DockPanel StackPanel01
        //{
        //    get { return this.m_PanelLeftCahrt01; }
        //}
        public DockPanel StackPanel02
        {
            get { return this.m_PanelLeftCahrt02; }
        }
        public DockPanel StackPanel03
        {
            get { return this.m_PanelLeftCahrt03; }
        }
        public DockPanel StackPanel04
        {
            get { return this.m_PanelLeftCahrt04; }
        }

        [DllImport("gdi32.dll", SetLastError = true)]
        private static extern bool DeleteObject(IntPtr hObject);

        //将 Bitmap转换为BitmapSource
        //使用过System.Drawing.Bitmap后一定要用DeleteObject释放掉对象，不然内存不释放，很快系统内存就消耗光了。
        private static ImageSource BitmapToBitmapSource(Bitmap bitmap)
        {
            IntPtr hBitmap = bitmap.GetHbitmap();
            ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                hBitmap,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
            if (!DeleteObject(hBitmap))//记得要进行内存释放。否则会有内存不足的报错。
            {
                throw new System.ComponentModel.Win32Exception();
            }

            return wpfBitmap;
        }
    }
}
