#pragma once
//演员对象智能指针Map
extern std::map<uint64_t, ActorPtr>ActorPtrMapObject;

//创建画布
static SQInteger Create_Canvas(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

    SQFloat X;
    sq_getfloat(v, 2, &X);
    SQFloat Y;
    sq_getfloat(v, 3, &Y);

    CanvasPtr _Canvas = new Canvas(PixelSize(X, Y));
    //获取对象的唯一ID
    uint64_t UUID = _Canvas->GetObjectID();

    //将画布对象存入Map管理
    ActorPtrMapObject[UUID] = _Canvas;

    sq_pushinteger(v, UUID);

    return 1;
}

//画布开始渲染
static SQInteger Canvas_BeginDraw(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

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

    if (ActorPtrMapObject.count(ActorUUID)) {
        CanvasPtr _Canvas = dynamic_cast<Canvas*>(ActorPtrMapObject[ActorUUID].Get());
        CanvasRenderContextPtr _CanvasRenderContextPtr = _Canvas->GetContext2D();
        _CanvasRenderContextPtr->SetTextAntialiasMode(TextAntialiasMode::None);
        _CanvasRenderContextPtr->BeginDraw();
        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }

    return 1;
}

//画布结束渲染
static SQInteger Canvas_EndDraw(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

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

    if (ActorPtrMapObject.count(ActorUUID)) {
        CanvasPtr _Canvas = dynamic_cast<Canvas*>(ActorPtrMapObject[ActorUUID].Get());
        CanvasRenderContextPtr _CanvasRenderContextPtr = _Canvas->GetContext2D();
        _CanvasRenderContextPtr->EndDraw();
        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }

    return 1;
}

//画布绘制纹理
static SQInteger Canvas_DrawTexture(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

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

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

    SQInteger gFrame;
    sq_getinteger(v, 4, &gFrame);

    SQFloat X;
    sq_getfloat(v, 5, &X);
    SQFloat Y;
    sq_getfloat(v, 6, &Y);

    if (ActorPtrMapObject.count(ActorUUID)) {
        CanvasPtr _Canvas = dynamic_cast<Canvas*>(ActorPtrMapObject[ActorUUID].Get());

        CanvasRenderContextPtr _CanvasRenderContextPtr = _Canvas->GetContext2D();

        //只有坐标参数
        if (Top == 6) {
            Vec2 POS = Vec2(X, Y);

            TexturePtr t = SquirrelClassEx::GetTexturePtrByImg((char*)OutPutBuffer, gFrame);
            _CanvasRenderContextPtr->DrawTexture(t, POS);
        }
        else if (Top == 8) {
            SQFloat XSize;
            sq_getfloat(v, 7, &XSize);
            SQFloat YSize;
            sq_getfloat(v, 8, &YSize);

            Vec2 POS = Vec2(X, Y);
            Vec2 Size = Vec2(X + XSize, Y + YSize);

            TexturePtr t = SquirrelClassEx::GetTexturePtrByImg((char*)OutPutBuffer, gFrame);
            _CanvasRenderContextPtr->DrawTexture(t, POS, Size);
        }
        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }
    return 1;
}



//画布绘制文字
static SQInteger Canvas_DrawText(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

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

    //得到Squirrel字符串 字体路径Key
    const SQChar* OutPutBuffer;
    sq_getstring(v, 3, &OutPutBuffer);


    //此操作会New出一个字符串需要手动销毁
    char* OutPutTextbuf = SquirrelClassEx::SquirrelU2W((char*)OutPutBuffer);
    std::string OutPutText = OutPutTextbuf;
    //销毁New出来的字符串
    delete[]OutPutTextbuf;

    //通过字体创建文本样式
    if (Top == 7) {
        //得到Squirrel字符串 文字内容
        const SQChar* TextStringBuffer;
        sq_getstring(v, 4, &TextStringBuffer);
        //此操作会New出一个字符串需要手动销毁
        char* TextStringbuf = SquirrelClassEx::SquirrelU2W((char*)TextStringBuffer);
        //需要绘制的文字内容
        std::string TextString = TextStringbuf;
        //销毁New出来的字符串
        delete[]TextStringbuf;

        SQInteger FontColor;
        sq_getinteger(v, 5, &FontColor);

        SQFloat Xpos;
        sq_getfloat(v, 6, &Xpos);

        SQFloat Ypos;
        sq_getfloat(v, 7, &Ypos);


        //从全局Map中获取画布对象
        if (ActorPtrMapObject.count(ActorUUID)) {
            CanvasPtr _Canvas = dynamic_cast<Canvas*>(ActorPtrMapObject[ActorUUID].Get());
            CanvasRenderContextPtr _CanvasRenderContextPtr = _Canvas->GetContext2D();
            TextStyle TsP = TextStyle(FontRecObject[OutPutText]);
            TsP.alignment = TextAlign::Center;
            /*
            // 创建线性渐变样式
            LinearGradientStyle fill_style = LinearGradientStyle(
                Point(_Canvas->GetWidth() / 3, 0.0f),  // 线性样式起点
                Point(_Canvas->GetWidth() * 2 / 3, _Canvas->GetHeight()),  // 线性样式终点
                { GradientStop(0.0f, Color::Yellow), GradientStop(1.0f, Color::Green) }
            );
            */


            BrushPtr fill_brush = new Brush(FontColor);
            _CanvasRenderContextPtr->SetFillBrush(fill_brush);
            _CanvasRenderContextPtr->DrawTextLayout(TextString, TsP, Point(Xpos, Ypos));

            sq_pushbool(v, true);
        }
    }
    else {
        sq_pushbool(v, false);
    }


    return 1;
}
