using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace ryCommon
{
   /// <summary>
   /// 系统函数
   /// </summary>
   public class RySystem
    {
        /// <summary>
        /// 
        /// </summary>
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public struct RAMP
        {
            /// <summary>
            /// 红
            /// </summary>
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
            public UInt16[] Red;
            /// <summary>
            /// 绿
            /// </summary>
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
            public UInt16[] Green;
            /// <summary>
            /// 蓝
            /// </summary>
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
            public UInt16[] Blue;
        }
        /// <summary>
        /// 获取屏幕伽马值
        /// </summary>
        /// <param name="hDC"></param>
        /// <param name="lpRamp"></param>
        /// <returns></returns>
        [DllImport("gdi32.dll", SetLastError = true)]
        public static extern bool GetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp);
        /// <summary>
        /// 设置屏幕伽马值
        /// </summary>
        /// <param name="hDC"></param>
        /// <param name="lpRamp"></param>
        /// <returns></returns>
        [DllImport("gdi32.dll", SetLastError = true)]
        public static extern bool SetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp);

        private static readonly double[] _gammas = { 1, 1, 1 };
        private static short _brightness = 100;
        // 创建结构体用于返回捕获时间
        [StructLayout(LayoutKind.Sequential)]
        struct LASTINPUTINFO
        {
            // 设置结构体块容量
            [MarshalAs(UnmanagedType.U4)]
            public int cbSize;
            // 捕获的时间
            [MarshalAs(UnmanagedType.U4)]
            public uint dwTime;
        }
        [DllImport("user32.dll")]
        private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
        /// <summary>
        /// 获取键盘和鼠标没有操作的时间
        /// </summary>
        /// <returns></returns>
        public static long GetLastInputTime()
        {
            LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
            vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
            // 捕获时间
            if (!GetLastInputInfo(ref vLastInputInfo))
                return 0;
            else
                return Environment.TickCount - (long)vLastInputInfo.dwTime;
        }
        /// <summary>
        /// 设置屏幕 Gamma值
        /// </summary>
        /// <param name="gammaRed"></param>
        /// <param name="gammaGreen"></param>
        /// <param name="gammaBlue"></param>
        /// <returns></returns>
        public static bool SetGamma(double gammaRed, double gammaGreen, double gammaBlue)
        {
            _gammas[0] = gammaRed;
            _gammas[1] = gammaGreen;
            _gammas[2] = gammaBlue;

            return UpdateRamps();
        }
        /// <summary>
        /// 设置屏幕亮度
        /// </summary>
        /// <param name="brightness"></param>
        /// <returns></returns>
        public static bool SetBrightness(short brightness)
        {
            if (brightness > 100)
                brightness = 100;

            if (brightness < 0)
                brightness = 0;

            _brightness = brightness;

            return UpdateRamps();
        }

        private static bool UpdateRamps()
        {
            double brightness = (double)_brightness / 100f;
            RAMP ramp = new RAMP
            {
                Red = new UInt16[256],
                Green = new UInt16[256],
                Blue = new UInt16[256]
            };
            for (int i = 0; i < 256; i++)
            {
                ramp.Red[i] = (ushort)GetGamma(_gammas[0]);
                ramp.Green[i] = (ushort)GetGamma(_gammas[1]);
                ramp.Blue[i] = (ushort)GetGamma(_gammas[2]);
                double GetGamma(double gamma)
                {
                    var r = (Math.Pow(i / 256.0, 1.0 / gamma) * 65535 + 0.5);
                    r *= brightness;
                    if (r > 65535)
                        r = 65535;
                    if (r < 0)
                        r = 0;
                    return r;
                }
            }
            var retVal = SetDeviceGammaRamp(Graphics.FromHwnd(IntPtr.Zero).GetHdc(), ref ramp);

            //Memory allocated through stackalloc is automatically free'd
            //by the CLR.

            return retVal;
        }
        /// <summary>
        /// 获取系统
        /// </summary>
        /// <returns></returns>
        public static string GetOSystemVer()
        {
            switch (System.Environment.OSVersion.Version.Major + "." + System.Environment.OSVersion.Version.Minor)
            {
                case "5.0":
                    return "Windows2000";
                case "5.1":
                    return "WindowsXP";
                case "5.2":
                    return "Windows2003";
                case "6.0":
                    return "Windows2008";
                case "6.1":
                    return "Windows7";
                case "6.2":
                    return "Windows8 Or Windows8.1";
                case "6.3":
                    return "Windows8.1";
                case "10.0":
                    return "Windows10";
                default:
                    return "未知";
            }
        }
    }
}
