#include "ir.h"

using namespace TesterCommunication;

Ir::Ir()
{
    isInitialized = false;
}

Ir::~Ir()
{
    Finalize();
}

Result Ir::Initialize()
{
    nn::Result nnResult;

    //初期化
    nnResult = nn::ir::CTR::Low::Initialize();
    if( nnResult.IsFailure())
    {
        return ConvertResult(nnResult);
    }

    isInitialized = true;

    /*必須か不明*/
    nnResult = nn::ir::CTR::Low::SetAutoPowerControl( true );
    if( nnResult.IsFailure())
    {
        return TesterCommunication::Result( Error::Fatal, "Ir AutoPowerOn Error.");
    }

    /*必須か不明*/
    nnResult = nn::ir::CTR::Low::PowerOn();
    if( nnResult.IsFailure())
    {
        return TesterCommunication::Result( Error::Fatal, "Ir PowerOn Error.");
    }
    
    //baudrate設定
    nnResult = nn::ir::CTR::Low::SetBaudRate(nn::ir::CTR::BAUD_RATE_115200);
    if( nnResult.IsFailure())
    {
        return ConvertResult(nnResult);
    }
    
    //イベント監視
    m_SendEvent.Initialize(true);
    m_RecvEvent.Initialize(true);
    nnResult = nn::ir::CTR::Low::GetFinishedSendingEvent( &m_SendEvent );
    if( nnResult.IsFailure())
    {
        return ConvertResult(nnResult);
    }
    nnResult = nn::ir::CTR::Low::GetFinishedReceivingEvent( &m_RecvEvent );
    if( nnResult.IsFailure())
    {
        return ConvertResult(nnResult);
    }
    
    return TesterCommunication::Result( Error::None, "" );
}


Result Ir::Finalize()
{
    if(isInitialized)//初期化されていた場合
    {
        nn::Result nnResult;

        /*ライブラリ終了*/
        nnResult = nn::ir::CTR::Low::Finalize();
        if ( nnResult.IsFailure() )
        {
            return ConvertResult( nnResult );
        }

        m_SendEvent.Finalize();
        m_RecvEvent.Finalize();
        
        isInitialized = false;
    }

    return TesterCommunication::Result( Error::None, "" );
}


Result Ir::Send(Packet *sendPacket)
{
    nn::fnd::TimeSpan sendTimeout = nn::fnd::TimeSpan::FromMilliSeconds(500);

    const string sendString = sendPacket->ToString();
    if( sendString.empty())
    {
        return TesterCommunication::Result( Error::Uninitialized, "Uninitialized Packet.\n" );
    }

    /*KTRにて、長いパケットを送信すると高確率で失敗するため、分割送信*/
    for(unsigned int head = 0; head < sendString.length(); head += 64)
    {
        /*パケット分割*/
        unsigned int tail = nn::math::Min<unsigned int>(head + 64, sendString.length());
        string partString = sendString.substr(head, tail - head);
        
        /*送信前にシグナルクリア*/
        m_SendEvent.ClearSignal();
        
        /*送信*/
        nn::Result nnResult = nn::ir::CTR::Low::Send((u8 *)partString.c_str(), partString.length());
        if ( nnResult.IsFailure() )
        {
            nn::ir::CTR::Low::CancelSend();
            return ConvertResult( nnResult );
        }
        
        /*送信完了待ち*/
        if ( !m_SendEvent.Wait(sendTimeout) )
        {
            nn::ir::CTR::Low::CancelSend();
            return TesterCommunication::Result( Error::Timeout, "Send Packet Timeout.\n" );
        }
        
        /*エラーチェック*/
        nn::ir::CTR::Low::ErrorStatus errorStatus = nn::ir::CTR::Low::GetErrorStatus();
        if (errorStatus != nn::ir::CTR::Low::STATUS_NO_ERROR)
        {
            nn::ir::CTR::Low::CancelSend();
            stringstream ss;
            ss << errorStatus;
            return TesterCommunication::Result( Error::Fatal, "Send Packet Error(ErrorStatus:" +  ss.str() + ").\n");
        }
    }
    
    return TesterCommunication::Result( Error::None, "" );
}


Result Ir::SendAndRecvReply(Packet *sendPacket, nn::fnd::TimeSpan replyTimeout, int retryCount)
{
    int retry;

    for(retry = 0; retry <= retryCount; retry++)
    {
        /*送信*/
        Result result;
        result = Send(sendPacket);
        if ( result.GetErrorType() != Error::None )
        {
            return result;
        }

        /*返答受信*/
        Packet replyPacket;
        result = Recv(&replyPacket, replyTimeout);
        if(result.GetErrorType() == Error::Timeout)
        {
            if(retry >= retryCount)
            {
                return TesterCommunication::Result( Error::Timeout, "Recv Reply Timeout.\n" );
            }
            else
            {
                /*リトライ*/
                continue;
            }
        }
        else if(result.GetErrorType() != Error::None)
        {
            return result;
        }

        /*返答判定*/
        if(replyPacket.formatId == 0x00)
        {
            //TODO.対象packetId,対象retryCountもチェック
            Reply *replyData = (Reply *)replyPacket.mainData;
            if(replyData->replyStatus == Reply::Ok)
            {
                /*Reply受信成功*/
                break;
            }
        }

        //TODO.他の返答ステータスも入れて、実装を綺麗に
        
        /*異常返答受信時*/
        if(retry >= retryCount)
        {
            /*受信失敗*/
            return TesterCommunication::Result( Error::Fatal, "Recv Ng Reply.\nRecv : " + replyPacket.ToString() + "\n" );
        }
        else
        {
            continue;
        }
    }

    NN_ASSERT(retry <= retryCount);

    return TesterCommunication::Result( Error::None, "" );
}


Result Ir::Recv( Packet *retPacket, nn::fnd::TimeSpan recvTimeout)
{
    nn::Result nnResult;
    TesterCommunication::Result result;
    size_t recvSize;

    if ( retPacket == NULL )
    {
        return TesterCommunication::Result( Error::Uninitialized, "" );
    }

    /*IR受信バッファクリア*/
    memset(s_IrRecvBuffer, 0x0, sizeof(s_IrRecvBuffer));

    /*受信前にシグナルクリア*/
    m_RecvEvent.ClearSignal();
    
    /*ヘッダ受信開始*/
    nnResult = nn::ir::CTR::Low::StartReceive( s_IrRecvBuffer, RECV_BUFFER_SIZE, RECV_BUFFER_SIZE, false );
    if( nnResult.IsFailure())
    {
        nn::ir::CTR::Low::EndReceive( &recvSize );
        return ConvertResult( nnResult );
    }    
    
    /*パケット受信待ち*/
    if ( !m_RecvEvent.Wait(recvTimeout) )
    {
        nn::ir::CTR::Low::EndReceive( &recvSize );
        return TesterCommunication::Result( Error::Timeout, "Receive Packet Timeout.\n" );
    }
    //下記理由によりエラー処理無し
    //もし変なデータを受信していた場合は、後続の処理でエラーとなる(はず)
    
    //受信が成功しても、STATUS_RECEIVE_TIMEOUT(0x03)が返ってくるため(バグ?)、エラー処理を割愛
    /*
    nn::ir::CTR::Low::ErrorStatus errorStatus = nn::ir::CTR::Low::GetErrorStatus();
    if (errorStatus != nn::ir::CTR::Low::STATUS_NO_ERROR)
    {
        nn::ir::CTR::Low::EndReceive( &recvSize );
        stringstream ss;
        ss << errorStatus;
        return TesterCommunication::Result( Error::Fatal, "Receive Packet Error(ErrorStatus:" + ss.str() + ").\n");
    }*/

    /*受信終了*/
    nnResult = nn::ir::CTR::Low::EndReceive( &recvSize );
    if ( nnResult.IsFailure() )
    {
        return ConvertResult( nnResult );
    }
    
    /*retPacketに値をセット*/
    string recvString = (char *)s_IrRecvBuffer;
    TesterCommunication::Result recvResult = retPacket->Recv( recvString ) ;
    if ( recvResult.GetErrorType() != Error::None )
    {
        return recvResult;
    }
    
    return TesterCommunication::Result( Error::None, "" );
}


Result Ir::ConvertResult( nn::Result result )
{
    if ( result == nn::ir::CTR::ResultAlreadyInitialized() ) 
    {
        return TesterCommunication::Result( Error::Fatal, "Ir Already Initialized.\n" );
    }
    else if ( result == nn::ir::CTR::ResultNotInitialized() )
    {
        return TesterCommunication::Result( Error::Uninitialized, "Ir Not Uninitialized.\n" );
    }
    else if ( result == nn::ir::CTR::ResultReceiveAlreadyCompleted() )
    {
        return TesterCommunication::Result( Error::Fatal, "Ir Already Received.\n" );
    }
    else if ( result == nn::ir::CTR::ResultMachineSleep() )
    {
        return TesterCommunication::Result( Error::Fatal, "Machine Sleeping.\n" );
    }
    else if ( result == nn::ir::CTR::ResultModuleIsBusy() )
    {
        return TesterCommunication::Result( Error::Fatal, "Ir Module is Busy.\n" );
    }
    else if ( result == nn::ir::CTR::ResultFatalError() )
    {
        return TesterCommunication::Result( Error::Fatal, "Ir Module may be Broken.\n" );
    }
    else
    {
        return TesterCommunication::Result( Error::Undefined, "Ir Undefined Error.\n" );
    }
}
