//---------------------------------------------------------------------------------------------------------------------------------------------------
// <copyright file="XboxDigitalControl.cs" company="DarkWynter Studios">
//     Copyright (C)2007 DarkWynter Studios.  All rights reserved.
// </copyright>
//---------------------------------------------------------------------------------------------------------------------------------------------------
// {Contact : darkwynter.com for licensing information
//---------------------------------------------------------------------------------------------------------------------------------------------------

namespace DarkWynter.Engine.Controllers
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Input;
    using System.Diagnostics;

    using DarkWynter.Engine.ObjectLib;
    using DarkWynter.Engine.Menus;
    using Globals;
    #endregion

    /// <summary>
    /// Xbox digital event class for mapping a digital button/motion to an event
    /// </summary>
    public class XboxDigitalControl 
    {
        // Time delay between key presses to avoid double input
        private int KEY_TIMER_MAX_MILLI = 100;
        
        private Stopwatch controlTimer = new Stopwatch();

        //Button we are checking for
        private Buttons button;

        /// <summary>
        /// On click delegate type
        /// </summary>
        /// <param name="args">Controller boolean event arguments</param>
        public delegate void OnClickHandler(ControllerBoolEventArgs args);
        private event OnClickHandler click;

        /// <summary>
        /// Click constructor
        /// </summary>
        /// <param name="xboxbutton">Click control type</param>
        /// <param name="onClickHandler">Click handler</param>
        /// <param name="timerValue">Time delay between multiple invocations</param>
        public XboxDigitalControl(Buttons xboxbutton, OnClickHandler onClickHandler, int timerValue)
        {            
            button = xboxbutton;
            click += new OnClickHandler(onClickHandler);
            controlTimer.Start();
            KEY_TIMER_MAX_MILLI = timerValue;
        }

        /// <summary>
        /// Update Method
        /// </summary>
        /// <param name="gamePadState">Updated game pad state</param>
        /// <param name="objectLibrary">Object Library</param>
        /// <param name="menuSystem">Menu System</param>
        public void Update(GamePadState gamePadState, ref ObjectLibrary objectLibrary, ref MenuSystem menuSystem)
        {
            // Check double-input timer
            if (controlTimer.ElapsedMilliseconds > KEY_TIMER_MAX_MILLI)
            {
                // Reset Timer
                controlTimer.Reset();
                controlTimer.Start();
                
                // If button was pressed
                if (gamePadState.IsButtonDown(button))
                {
                    if (click != null)
                    {
                        click(new ControllerBoolEventArgs(true, ref objectLibrary, ref menuSystem));
                    }
                }
            }
        }
    }    
}