// todo
#include "maple.h"

/// <summary>
///	todo
/// </summary>
void MAPLE::cMapleJump::Init()
{	
    m_bConnected = false;    
	maple_device_t* pDevice;
    
    pDevice = maple_enum_dev(m_pController->m_cControllerId, MAPLE_CONTROLLER_PORT_2);  
    if (pDevice && (pDevice->info.functions & MAPLE_FUNC_PURUPURU))
	{			        
        m_pDevice = pDevice;
		m_bConnected = true;	
        m_bInitialised = true;       
        m_cControllerUnit = MAPLE_CONTROLLER_PORT_2;
        m_cControllerId = m_pController->Id();
	}
    else
    {
        pDevice = maple_enum_dev(m_pController->m_cControllerId, MAPLE_CONTROLLER_PORT_1);
        if (pDevice && (pDevice->info.functions & MAPLE_FUNC_PURUPURU))
        {
            m_pDevice = pDevice;
            m_bConnected = true;	
            m_bInitialised = true;       
            m_cControllerUnit = MAPLE_CONTROLLER_PORT_1;
            m_cControllerId = m_pController->Id();
        }
    }	
}

/// <summary>
///	todo
/// </summary>
void MAPLE::cMapleJump::RumbleUpdate()
{    
    if (m_lEffectEnd)
    {
        if (m_bRumbleOff || timer_ms_gettime64() >= m_lEffectEnd)
        {
            if (Status() == Connected)
            {       
                RumbleOff();
            }

            m_cActiveEffect = 0;	
	        m_lEffectEnd = 0;
        }        
    }     
}

void MAPLE::cMapleJump::RumbleOn(int8_t cEffect)
{          
    if (!m_cActiveEffect || cEffect > m_cActiveEffect)
    {        
        if (Status() == Connected)
        {
            m_cActiveEffect = cEffect;
            m_bRumbleOff = false;
            switch (cEffect)
	        {
                case ShortWeakPulse:
                    m_lEffectEnd = timer_ms_gettime64() + 500;                    
                    effect.duration = 0xFF;
                    effect.effect1 = PURUPURU_EFFECT1_INTENSITY(2) | PURUPURU_EFFECT1_PULSE;
                    effect.effect2 = PURUPURU_EFFECT2_LINTENSITY(2) | PURUPURU_EFFECT2_PULSE;
                    effect.special = PURUPURU_SPECIAL_MOTOR1 | PURUPURU_SPECIAL_PULSE;
                    purupuru_rumble(m_pDevice, &effect);
		        break;   
                case LongWeakPulse:                    
                    m_lEffectEnd = timer_ms_gettime64() + 4500;                    
                    effect.duration = 0xFF;
                    effect.effect1 = PURUPURU_EFFECT1_INTENSITY(3) | PURUPURU_EFFECT1_PULSE;
                    effect.effect2 = PURUPURU_EFFECT2_UINTENSITY(1);
                    effect.special = PURUPURU_SPECIAL_MOTOR1;
                    purupuru_rumble(m_pDevice, &effect);
                break;   
                case ShortStrongJolt:
                    m_lEffectEnd = timer_ms_gettime64() + 750;
                    effect.duration = 0xFF;
                    effect.effect1 = PURUPURU_EFFECT1_INTENSITY(6) | PURUPURU_EFFECT1_PULSE;
                    effect.effect2 = PURUPURU_EFFECT2_UINTENSITY(3);
                    effect.special = PURUPURU_SPECIAL_MOTOR1;
                    purupuru_rumble(m_pDevice, &effect);
                break;          
	        }	
        }        
    }    
}

void MAPLE::cMapleJump::RumbleOff()
{             
    effect.duration = 0x00;
    effect.effect2 = 0x00;
    effect.effect1 = 0x00;
    effect.special = PURUPURU_SPECIAL_MOTOR1;
    purupuru_rumble(m_pDevice, &effect);        	
}