using SuperMap.Mapping;
using SuperMap.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace WWPipeLine.MapBasic.Results
{
	public partial class ResultWindow : ToolWindowExtend
	{
		///// <summary>
		///// 存储参数
		///// </summary>
		//private SortedDictionary<string, object> m_BaseParams = new SortedDictionary<string, object>();
		///// <summary>
		///// 设置参数
		///// </summary>
		///// <param name="key">参数名</param>
		///// <param name="value">参数值</param>
		//public void SetParams(string key, object value)
		//{
		//	if (m_BaseParams.ContainsKey(key))
		//	{
		//		return;
		//	}
		//	m_BaseParams.Add(key, value);
		//}
		//protected string SelectLayerNamelResultWindow
		//{
		//	get
		//	{
		//		if (m_BaseParams.ContainsKey("SelectLayerNamelResultWindow"))
		//			return m_BaseParams["SelectLayerNamelResultWindow"] as string;
		//		return null;
		//	}
		//}

		//public string FindLayerName { get; set; }
		/// <summary>
		/// 构造并初始化查询结果ResultWindow窗体
		/// </summary>
		public ResultWindow()
		{
			InitializeComponent();

			///关闭按钮可见、可用
			this.CloseButtonVisible = true;
			this.CloseButton = true;
			this.DockAreas = DockAreas.DockLeft
				| DockAreas.DockRight
				| DockAreas.DockTop
				| DockAreas.DockBottom
				| DockAreas.Document
				| DockAreas.Float;
		}


		/// <summary>
		/// 将DataGridView的数据导出至Excel
		/// </summary>
		/// <param name="dg"></param>
		public void ExportToExcel(System.Windows.Forms.DataGridView dg)
		{
			if (dg == null) return;
			var errMsg = Commons.ExportDgvToExcel.ExportExcel(dg);
			if (string.IsNullOrEmpty(errMsg))
			{
				ComsStatic.ShowOKLog("导出Excel文件成功");
			}
			else
			{
				if (errMsg == Commons.ExportDgvToExcel.CANCELRESULTSTRING)
					return;
				else
					ComsStatic.ShowErrorLog("导出Excel文件失败,原因是" + errMsg);
			}
		}
		/// <summary>
		/// 将Control导出至Image
		/// </summary>
		/// <param name="ctrl"></param>
		public void ExportToImage(Control ctrl)
		{
			SaveFileDialog sfd = new SaveFileDialog
			{
				DefaultExt = "jpg",
				Filter = "图片文件(*.jpg)|*.jpg",
				RestoreDirectory = true,
				Title = "图片文件保存路径"
			};
			if (sfd.ShowDialog() == DialogResult.OK)
			{
				Bitmap bmp = new Bitmap(ctrl.Width, ctrl.Height);
				ctrl.DrawToBitmap(bmp, ctrl.ClientRectangle);
				bmp.Save(sfd.FileName);
			}

		}

		/// <summary>
		/// 显示数据非BasicToolBar 必须实现此方法
		/// </summary>
		/// <param name="data"></param>
		public virtual void ShowData(object data)
		{
			throw new Exception("未实现WWPipeLine.MapTools.Results.ResultWindow.ShowData");
		}

		/// <summary>
		/// 导出Excel按钮
		/// </summary>
		protected Sunny.UI.UISymbolButton uiSymbolButtonExportToExcel;
		/// <summary>
		/// 导出图片按钮
		/// </summary>
		protected Sunny.UI.UISymbolButton uiSymbolButtonExportToImage;
		/// <summary>
		/// 创建并添加,“导出到Excel”按钮
		/// </summary>
		protected void CreateExportExcelButton()
		{
			this.uiSymbolButtonExportToExcel = new Sunny.UI.UISymbolButton
			{
				Cursor = System.Windows.Forms.Cursors.Hand,
				Dock = System.Windows.Forms.DockStyle.Bottom,
				Font = new System.Drawing.Font("微软雅黑", 12F),
				Location = new System.Drawing.Point(0, 415),
				MinimumSize = new System.Drawing.Size(1, 1),
				Name = "uiSymbolButtonExportToExcel",
				Size = new System.Drawing.Size(800, 20),
				Symbol = 61582,
				TabIndex = 0,
				Style = Sunny.UI.UIStyle.Gray,
				Text = "导出到Excel"
			};
			this.Controls.Add(this.uiSymbolButtonExportToExcel);
		}

		/// <summary>
		/// 创建并添加,“导出图片”和“导出到Excel”按钮
		/// </summary>
		protected void CreateExportImageAndExcelButton()
		{
			Sunny.UI.UIPanel panel = new Sunny.UI.UIPanel
			{
				Dock = System.Windows.Forms.DockStyle.Bottom,
				Height = 20
			};
			this.Controls.Add(panel);
			this.uiSymbolButtonExportToExcel = new Sunny.UI.UISymbolButton
			{
				Dock = System.Windows.Forms.DockStyle.Fill,
				Cursor = System.Windows.Forms.Cursors.Hand,
				Font = new System.Drawing.Font("微软雅黑", 12F),
				Location = new System.Drawing.Point(0, 415),
				MinimumSize = new System.Drawing.Size(1, 1),
				Name = "uiSymbolButtonExportToExcel",
				Size = new System.Drawing.Size(800, 20),
				Symbol = 61582,
				TabIndex = 0,
				Style = Sunny.UI.UIStyle.Gray,
				Text = "导出到Excel",
				RectSides = System.Windows.Forms.ToolStripStatusLabelBorderSides.None,
				RadiusSides = Sunny.UI.UICornerRadiusSides.None
			};
			panel.Controls.Add(this.uiSymbolButtonExportToExcel);

			this.uiSymbolButtonExportToImage = new Sunny.UI.UISymbolButton
			{
				Dock = System.Windows.Forms.DockStyle.Left,
				Cursor = System.Windows.Forms.Cursors.Hand,
				Font = new System.Drawing.Font("微软雅黑", 12F),
				Location = new System.Drawing.Point(0, 415),
				MinimumSize = new System.Drawing.Size(1, 1),
				Name = "uiSymbolButtonExportToImage",
				Size = new System.Drawing.Size(800, 20),
				Symbol = 61893,
				TabIndex = 1,
				Style = Sunny.UI.UIStyle.Gray,
				Text = "导出图片",
				RectSides = System.Windows.Forms.ToolStripStatusLabelBorderSides.None,
				RadiusSides = Sunny.UI.UICornerRadiusSides.None
			};
			panel.Controls.Add(this.uiSymbolButtonExportToImage);

			this.SizeChanged += ResultWindow_SizeChanged;
		}

		/// <summary>
		/// 窗体发生变化时候按钮自动缩放
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ResultWindow_SizeChanged(object sender, EventArgs e)
		{
			uiSymbolButtonExportToExcel.Width = this.Width / 2;
			uiSymbolButtonExportToImage.Width = this.Width / 2;
		}
	}
}