//
//  IOSGLRenderer.cpp
//  matchempoker
//
//  Created by jarnoh on 4/1/11.
//  Copyright 2011 peekpoke. All rights reserved.
//

#include <stdio.h>
#include "IOSGLRenderer.h"
#include "GLES2.h"


const char *audioSamples[ AUDIO_SAMPLE_COUNT ] = {
    "click.wav",
    "change.wav",                   // milton
    "change_completed.wav",         // milton
    "destroy.wav",                      // milton
    "fanfare.wav",                  // by milton
    "game_over.wav",                    // by milton
    "illegal_move.wav",                 // milton
    "next_level.wav",               // by milton @ freesound
    "exp_bonus.wav",                 // by mattwasser
    "xbonus.wav",                 // by Christianjinnyzoe
};


IOSGLRenderer::IOSGLRenderer()
{
    for(int i = 0;i<AUDIO_SAMPLE_COUNT;i++)
    {
        SystemSoundID snd;
        CFURLRef path=CFURLCreateFromFileSystemRepresentation(NULL, (const UInt8 *)audioSamples[i], strlen(audioSamples[i]), 0);
        AudioServicesCreateSystemSoundID (path, &snd);             
        CFRelease(path);
        
        this->audioSample[i]=snd;
    }
}

IOSGLRenderer::~IOSGLRenderer()
{
    for(int i = 0;i<AUDIO_SAMPLE_COUNT;i++)
    {
        AudioServicesDisposeSystemSoundID(this->audioSample[i]);
        this->audioSample[i]=0;
    }
}


void IOSGLRenderer::effectNotify( eTILEGAME_EFFECT effect, unsigned int arg1, unsigned int arg2 )
{
    GLES2Renderer::ITileRenderer::effectNotify(effect, arg1, arg2);
       printf("effectNotify %x\n", effect);

    int sample=-1;
    
    switch (effect) {
        case eEFFECT_DESTROYING:
        case eEFFECT_DESTROYING_BONUS:
            sample=AUDIO_SAMPLE_DESTROY;
            if(arg1>0) sample=AUDIO_SAMPLE_EXP_BONUS;
            break;
        case eEFFECT_GAMEOVER:
            sample=AUDIO_GAME_OVER;
            break;
        case eEFFECT_NEWLEVEL:
            sample=AUDIO_NEXT_LEVEL;
            break;
        case eEFFECT_LEVELCOMPLETED:
            sample=AUDIO_LEVEL_COMPLETED;
            break;
        case eEFFECT_XBONUS:
            sample=AUDIO_SAMPLE_X_BONUS;
            break;
            
//        case eEFFECT_BLOCK_BEGIN_FINISHED:
//        case eEFFECT_BLOCK_VANISH_STARTED:
        case eCHANGING:
            sample=AUDIO_SAMPLE_CHANGE;
            break;
        case eCHANGE_COMPLETED:
            sample=AUDIO_SAMPLE_CHANGE_COMPLETED;
            break;

        case eILLEGAL_MOVE:
            sample=AUDIO_ILLEGAL_MOVE;
            break;

            
            //        case eEFFECT_SCORECHANGED:
        case eCLICK:
        case eEFFECT_MENU:
            sample=AUDIO_SAMPLE_CLICK;
            break;
            
        default:
            break;
    }
    
    if(sample>=0)
    {
            AudioServicesPlaySystemSound(audioSample[sample]);
    }

    
}
