/********************************
 *
 * 作者:徐贞卫
 * 创建时间: 2019-10-16 13:38:05
 * 
 ********************************/

using System;
using System.Diagnostics;
using System.Linq;
using System.Net.NetworkInformation;

namespace Server {
    /// <summary>
    /// 监控服务应用信息
    /// </summary>
    public class MonitoringAppInfo : IEquatable<MonitoringAppInfo> {

        static DateTime unixbase { get; } = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime();

        /// <summary>
        /// 网卡硬件地址
        /// </summary>
        public string[] MacAddress { get; set; }

        /// <summary>
        /// 系统已启动时间（单位：秒）
        /// </summary>
        public int SysStartTime { get; set; }

        /// <summary>
        /// 进程PID
        /// </summary>
        public int Pid { get; set; }

        /// <summary>
        /// 服务应用完整路径
        /// </summary>
        public string FullFileName { get; set; }

        /// <summary>
        /// 模块ID标识符
        /// </summary>
        public string UniqueId { get; set; }

        /// <summary>
        /// 模块ID
        /// </summary>
        public int ModuleId { get; set; }

        /// <summary>
        /// 节点编号
        /// </summary>
        public int NodeNumber { get; set; }

        /// <summary>
        /// GetListViewItem
        /// </summary>
        /// <returns>GetListViewItem</returns>
        public System.Windows.Forms.ListViewItem GetListViewItem() {
            if(viewItem == null) {
                viewItem = new System.Windows.Forms.ListViewItem(
                    new string[] { ModuleId.ToString(), NodeNumber.ToString(), UniqueId, FullFileName }) { Tag = this };
            }
            return viewItem;
        }

        private System.Windows.Forms.ListViewItem viewItem;

        /// <summary>
        /// 当前应用信息
        /// </summary>
        /// <param name="uniqueId">模块ID标识符</param>
        /// <param name="moduleId">模块ID</param>
        /// <param name="nodeNumber">节点编号</param>
        /// <returns>当前进程的应用信息</returns>
        public static MonitoringAppInfo GetCurrentApplicateInfo(string uniqueId, int moduleId, int nodeNumber) {

            //时间戳
            var now = (int)DateTime.Now.Subtract(unixbase).TotalSeconds;

            //获取当前进程
            var process = Process.GetCurrentProcess();
            var mainModule = process.MainModule;

            var macAddress = GetNetcardMacaddress();
            var sysStartTime = now - (Environment.TickCount / 1000);
            var pId = process.Id;
            var fullFileName = mainModule.FileName;

            return new MonitoringAppInfo {
                MacAddress = macAddress,
                SysStartTime = sysStartTime,
                Pid = pId,
                FullFileName = fullFileName,
                UniqueId = uniqueId,
                ModuleId = moduleId,
                NodeNumber = nodeNumber
            };
        }

        /// <summary>
        /// 获取本机所有工作的以太网卡物理地址
        /// </summary>
        /// <returns></returns>
        public static string[] GetNetcardMacaddress() {
            if (MacAddresss == null) {
                var networkAdapters = NetworkInterface.GetAllNetworkInterfaces().Where(p => p.NetworkInterfaceType == NetworkInterfaceType.Ethernet && p.OperationalStatus == OperationalStatus.Up);
                MacAddresss = networkAdapters.Select(p => p.GetPhysicalAddress()).Select(p => p.ToString()).ToArray();
            }
            return MacAddresss;
        }

        static string[] MacAddresss = null;

        /// <summary>
        /// 指示当前对象是否等于同一类型的另一个对象
        /// </summary>
        /// <param name="other">一个与此对象进行比较的对象。</param>
        /// <returns>如果当前对象等于 other 参数，则为 true；否则为 false。</returns>
        public bool Equals(MonitoringAppInfo other) {
            if(other == null) {
                return false;
            }
            if(Pid != other.Pid) {
                return false;
            }
            if(ModuleId != other.ModuleId) {
                return false;
            }
            if(NodeNumber != other.NodeNumber) {
                return false;
            }
            if(!string.Equals(FullFileName, other.FullFileName, StringComparison.InvariantCultureIgnoreCase)) {
                return false;
            }
            return true;
        }

        /// <summary>
        /// ToString
        /// </summary>
        /// <returns></returns>
        public override string ToString() {
            return $"模块ID:{ModuleId}, 节点ID:{NodeNumber},路径：{FullFileName}";
        }
    }
}
