using parkMonitor.model;
using parkMonitor.view.util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace parkMonitor.view.sysInfoWin {
	/// <summary>
	/// SysInfoWin.xaml 的交互逻辑
	/// </summary>
	public partial class SysInfoWin : BasePopWin {
		static string info = "";

		public SysInfoWin() {
			InitializeComponent();

			//MachineInfo info = MachineInfo.I();

			initSysInfo();
		}

		private void initSysInfo() {
			if(info != "") {
				imgUpdate.Visibility = Visibility.Collapsed;
				txtInfo.Text = info;
				return;
			}

			Task.Run(() => {
				try {
					parkMonitor.language.Language lng = parkMonitor.language.Language.ins;

					string endl = "\r\n";
					info += lng.autoPackSys + " " + lng.centralPort + endl;
					info += lng.version + "：" + SysConst.version() + endl;
					info += endl;

					info += lng.hostNmae+ "：" + Dns.GetHostName() + endl;
					info += lng.os + "：" + getOsVersion() + endl;
					info += lng.cpu + "：" + getCPUName() + endl;
					info += lng.mem + "：" + getMenSize() + endl;
					info += lng.gpu + "：" + getGPUName() + endl;
				} catch(Exception) {

				}

				Dispatcher.Invoke(() => {
					imgUpdate.Visibility = Visibility.Collapsed;
					txtInfo.Text = info;
				});
			});

			return;
		}

		/// <summary>
		/// CPU名
		/// </summary>
		/// <returns></returns>
		private static string getCPUName() {
			try {
				string str = string.Empty;
				ManagementClass mcCPU = new ManagementClass("Win32_Processor");
				ManagementObjectCollection mocCPU = mcCPU.GetInstances();
				foreach(ManagementObject m in mocCPU) {
					string name = m["Name"].ToString();
					return name;
				}
			} catch { }

			return "";
		}

		/// <summary>
		/// 操作系统版本
		/// </summary>
		public string getOsVersion() {
			string str = "Windows 10";
			try {
				string hdId = string.Empty;
				ManagementClass hardDisk = new ManagementClass("Win32_OperatingSystem");
				ManagementObjectCollection hardDiskC = hardDisk.GetInstances();
				foreach(ManagementObject m in hardDiskC) {
					str = m["Name"].ToString().Split('|')[0].Replace("Microsoft", "").Trim();
					break;
				}
			} catch {

			}
			return str;
		}

		/// <summary>
		/// 显卡名
		/// </summary>
		public string getGPUName() {
			string result = "";
			try {

				ManagementClass hardDisk = new ManagementClass("Win32_VideoController");
				ManagementObjectCollection hardDiskC = hardDisk.GetInstances();
				foreach(ManagementObject m in hardDiskC) {
					result = m["VideoProcessor"].ToString();
					break;
				}
			} catch {

			}
			return result;
		}

		/// <summary>
		/// 获取系统内存大小
		/// </summary>
		public string getMenSize() {
			ManagementObjectSearcher searcher = new ManagementObjectSearcher();   //用于查询一些如系统信息的管理对象 
			searcher.Query = new SelectQuery("Win32_PhysicalMemory", "", new string[] { "Capacity" });//设置查询条件 
			ManagementObjectCollection collection = searcher.Get();   //获取内存容量 
			ManagementObjectCollection.ManagementObjectEnumerator em = collection.GetEnumerator();

			long capacity = 0;
			while(em.MoveNext()) {
				ManagementBaseObject baseObj = em.Current;
				if(baseObj.Properties["Capacity"].Value != null) {
					try {
						capacity += long.Parse(baseObj.Properties["Capacity"].Value.ToString());
					} catch {
						return "-GB";
					}
				}
			}
			int gb = 1024 * 1024 * 1024;
			return ((double)capacity / gb).ToString("0.0") + "GB";
		}

		private void btnNew_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
			Close();
		}
	}
}
