using System.Runtime.InteropServices;

namespace WheelchairControlWM
{
    /// <summary>
    /// Class to invoke unmanaged core dlls responsible for device power management.
    /// </summary>
    public static class DevicePowerManagement
    {
        /// <summary>
        /// Core function to reset system idle timer.
        /// </summary>
        [DllImport("coredll.dll")]
        private static extern void SystemIdleTimerReset();

        /// <summary>
        /// Core funcion to set system power state.
        /// </summary>
        /// <param name="pwrState">Desired power state.</param>
        /// <param name="pwrStateFlags">Power state flasgs.</param>
        /// <param name="options">Misc. options.</param>
        /// <returns>Status code.</returns>
        /// <seealso cref="powerState"/>
        [DllImport("coredll.dll")]
        private static extern int SetSystemPowerState(string pwrState,
                       int pwrStateFlags, int options);

        /// <summary>
        /// Power state codes.
        /// </summary>
        enum powerState : int
        {
            /// <summary>
            /// Device on.
            /// </summary>
            POWER_STATE_ON = 0x10000,

            /// <summary>
            /// Deivce of.
            /// </summary>
            POWER_STATE_OFF = 0x20000,

            /// <summary>
            /// Device suspended (on power button short press).
            /// </summary>
            POWER_STATE_SUSPEND = 0x200000,

            /// <summary>
            /// Flag set to force new power mode regardless of device global settings.
            /// </summary>
            POWER_FORCE = 4096
        }

        /// <summary>
        /// Reset internal system idle timer. Set power on state.
        /// </summary>
        /// <seealso cref="powerState"/>
        public static void ResetIdleTimer()
        {
            SetSystemPowerState(null, (int)powerState.POWER_STATE_ON, (int)powerState.POWER_FORCE);
            SystemIdleTimerReset();
        }
    }
}