#pragma once
#include "SoundManager.h"

//音频对象智能指针Map
extern std::map<size_t, SoundPtr>SoundActorPtrMapObject;
//音频资源Map
extern std::map<std::string, Sound*>SoundRecObject;
//音频管理器
static SoundManagerPtr SoundEffectObject = new SoundManager();


//Sound 创建音频对象
static SQInteger Create_Sound(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }


    const SQChar* OutPutBuffer;
    sq_getstring(v, 2, &OutPutBuffer);

    size_t Id =  SoundEffectObject->Save_Load((char*)OutPutBuffer);

    sq_pushinteger(v, Id);
    //if (SoundRecObject.count((char*)OutPutBuffer)) {
    //    sq_pushuserpointer(v, SoundRecObject[(char*)OutPutBuffer]);
    //}
    //else {
    //    Sound* Sound_Obj = new Sound();
    //    Sound_Obj->Load((char*)OutPutBuffer);

    //    sq_pushuserpointer(v, Sound_Obj);
    //}

    return 1;
}

//Sound 设置音频工作
static SQInteger Set_Sound_Task(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    SQInteger SoundId;
    sq_getinteger(v, 2, &SoundId);


    SQInteger Type;
    sq_getinteger(v, 3, &Type);

    //播放
    if (Type == 0) {
        //获取播放次数 -1 为无限循环 
        SQInteger Count;
        sq_getinteger(v, 4, &Count);

        SoundPtr Sound_Obj = SoundEffectObject->GetSaveSound(SoundId);
        Sound_Obj->Play(Count);
    }
    //暂停
    else if (Type == 1) {
        SoundPtr Sound_Obj = SoundEffectObject->GetSaveSound(SoundId);
        Sound_Obj->Pause();
    }
    //继续
    else if (Type == 2) {
        SoundPtr Sound_Obj = SoundEffectObject->GetSaveSound(SoundId);
        Sound_Obj->Resume();
    }
    //关闭
    else if (Type == 3) {
        SoundEffectObject->ReleaseSaveSoundByIndex(SoundId);
        //SoundPtr Sound_Obj = SoundEffectObject->GetSaveSound(SoundId);
        //Sound_Obj->Close();
    }

    return 0;
}

//Sound 获取音频音量
static SQInteger Get_Sound_Volume(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    SQInteger SoundId;
    sq_getinteger(v, 2, &SoundId);

    SoundPtr Sound_Obj = SoundEffectObject->GetSaveSound(SoundId);

    sq_pushfloat(v, Sound_Obj->GetVolume());

    return 1;
}

//Sound 设置音频音量
static SQInteger Set_Sound_Volume(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    SQInteger SoundId;
    sq_getinteger(v, 2, &SoundId);

    SoundPtr Sound_Obj = SoundEffectObject->GetSaveSound(SoundId);

    SQFloat VolumeCalue;
    sq_getfloat(v, 3, &VolumeCalue);

    Sound_Obj->SetVolume(VolumeCalue);
    return 0;
}




//Sound 临时播放音效
static SQInteger Squirrel_PlaySoundEffect(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    const SQChar* Name;
    sq_getstring(v, 2, &Name);

    //此操作会New出一个字符串需要手动销毁
    char* Namebuf = SquirrelClassEx::SquirrelU2W((char*)Name);
    //需要绘制的文字内容
    std::string SoundName = Namebuf;
    //销毁New出来的字符串
    delete[]Namebuf;

    if (SoundName.find(".ogg") != std::string::npos || SoundName.find(".mp3") != std::string::npos) {
        size_t Id = SoundEffectObject->Load(SoundName);
        SoundEffectObject->Play(Id, 0);
    }
    else {
        if (GameAudioList.count(SoundName)) {
            size_t Id = SoundEffectObject->Load(GameAudioList[SoundName]);
            SoundEffectObject->Play(Id, 0);
        }
    }

    /*
    SoundPtr Sound_Obj = new Sound();
    Sound_Obj->Load(SoundName);

    if (Top == 3) {
        SQFloat Volume;
        sq_getfloat(v, 3, &Volume);
        Sound_Obj->SetVolume(Volume);
    }

    Sound_Obj->Play();
    
    SoundEffectMap[Sound_Obj->GetObjectID()] = Sound_Obj;
    */

    return 0;
}

//Sound 释放临时音效
static SQInteger Squirrel_ReleseSoundEffect(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    //新架构自动调用析构函数
    //Map<size_t, SoundPtr> S_M = SoundEffectObject->GetAllSound();


    //for (auto it = SoundEffectVector.begin(); it != SoundEffectVector.end(); ++it) {
    //    size_t id = *it;
    //    if (!SoundEffectObject->IsPlaying(id)) {
    //        //SoundPtr Sound_Obj = SoundEffectObject->GetSound(id);
    //        //Sound_Obj->Close();
    //    }
    //}
    //SoundEffectObject->ClearCache();


    return 0;
}