﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Arch.CFramework.AppInternals.Components.Impl
{
    public class HostInfo : ComponentBase
    {
        Process process = null;
        public HostInfo()
        {
            HostName = Environment.MachineName;
            var ips = System.Net.Dns.GetHostAddresses(HostName).Where(a => a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToList();
            StringBuilder sb = new StringBuilder();

            foreach (var ip in ips)
            {
                sb.AppendFormat("{0},", ip.ToString());
            }
            //IP = sb.Remove(sb.Length - 1, 1).ToString();
            IP = sb.ToString(0, sb.Length - 1);
            OSVersion = Environment.OSVersion.VersionString;
            //Platform = Environment.OSVersion.Platform.ToString();
            Bit = Environment.Is64BitOperatingSystem ? "64" : "32";
            //SP = Environment.OSVersion.VersionString;
            int ticks = Environment.TickCount;
            DateTime dt = DateTime.Now;
            if (ticks < 0)
            {
                dt = dt.AddMilliseconds(-1 * int.MaxValue);
            }
            OsStartUpTime = dt.AddMilliseconds(-1 * (ticks & int.MaxValue)).ToString("yyyy-MM-dd HH:mm:ss:fff");
            //OS = "";
            AppStartUpTime = InnerAppInternals.StartUpTime;
            NetFrameworkVersion = Environment.Version.ToString();
            ProcessorCount = Environment.ProcessorCount.ToString();
            SystemPageSize = Environment.SystemPageSize.ToString();
            UserName = Environment.UserName;
            UserDomainName = Environment.UserDomainName;
            process = Process.GetCurrentProcess();
        }

        public string HostName { get; set; }
        public string IP { get; set; }
        public string OSVersion { get; set; }
        public string Bit { get; set; }
        public string OsStartUpTime { get; set; }
        public string AppStartUpTime { get; set; }
        //public string OS { get; set; }

        public string NetFrameworkVersion { get; set; }
        public string ProcessorCount { get; set; }
        public string SystemPageSize { get; set; }
        public string UserName { get; set; }
        public string UserDomainName { get; set; }
        public string WorkSet
        {
            get { try { return (process.WorkingSet64 / 1024).ToString(); } catch { return "0"; } }
            set
            {

            }
        }
        //public string PrivateSize
        //{
        //    get { try { return (process.PrivateMemorySize64 / 1024).ToString(); } catch { return "0"; } }
        //    set { }
        //}
    }
}
