#pragma once

extern std::map<uint64_t, ActorPtr>ActorPtrMapObject;
extern std::map<std::string, FontPtr>FontRecObject;

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


    // 创建一个文本角色
    TextActorPtr text = new TextActor();

    //获取文字对象的UUID
    uint64_t UUID = text->GetObjectID();

    //将文字对象存入Map管理
    ActorPtrMapObject[UUID] = text;

    //将HshName返回给虚拟机
    sq_pushinteger(v, UUID);

    return 1;
}

//设置文字对象文本
static SQInteger Squirrle_Set_Text_Object_Text(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字符串
    const SQChar* OutPutBuffer;
    sq_getstring(v, 3, &OutPutBuffer);

    //此操作会New出一个字符串需要手动销毁
    char* OutPutText = SquirrelClassEx::SquirrelU2W((char*)OutPutBuffer);

    //从全局Map中获取文本角色
    if (ActorPtrMapObject.count(ActorUUID)) {
        TextActorPtr text = dynamic_cast<TextActor*>(ActorPtrMapObject[ActorUUID].Get());
        text->SetText(OutPutText);
        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }

    //销毁New出来的字符串
    delete[]OutPutText;

    return 1;
}

//设置文字对象颜色
static SQInteger Squirrle_Set_Text_Object_Color(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);

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

    //从全局Map中获取文本角色
    if (ActorPtrMapObject.count(ActorUUID)) {
        TextActorPtr text = dynamic_cast<TextActor*>(ActorPtrMapObject[ActorUUID].Get());
        text->SetFillColor(TextColor);
        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }

    return 1;
}

//设置文字对象字体样式
static SQInteger Squirrle_Set_Text_Object_TextStyle(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* OutPutText = SquirrelClassEx::SquirrelU2W((char*)OutPutBuffer);
    std::string FontName = OutPutText;
    //销毁New出来的字符串
    delete[]OutPutText;


    //通过字体创建文本样式
    if (Top == 3) {
        //从全局Map中获取文本角色
        if (ActorPtrMapObject.count(ActorUUID)) {
            TextActorPtr text = dynamic_cast<TextActor*>(ActorPtrMapObject[ActorUUID].Get());
            if (FontRecObject.count(FontName)) {
                TextStyle TsP = TextStyle(FontRecObject[FontName]);
                text->SetStyle(TsP);
            }
            sq_pushbool(v, true);
        }
    }
    else if (Top == 8) {
        //得到文字对其方式
        SQInteger gTextAlign;
        sq_getinteger(v, 4, &gTextAlign);

        //得到文字自动换行宽度
        SQFloat gwrap_width;
        sq_getfloat(v, 5, &gwrap_width);

        //得到文字行间距
        SQFloat gline_spacing;
        sq_getfloat(v, 6, &gline_spacing);

        //得到文字显示下划线
        SQBool gshow_underline;
        sq_getbool(v, 7, &gshow_underline);

        //得到文字显示删除线
        SQBool gshow_strikethrough;
        sq_getbool(v, 8, &gshow_strikethrough);

        //从全局Map中获取文本角色
        if (ActorPtrMapObject.count(ActorUUID)) {
            TextActorPtr text = dynamic_cast<TextActor*>(ActorPtrMapObject[ActorUUID].Get());
            if (FontRecObject.count(FontName)) {
                TextStyle TsP = TextStyle(FontRecObject[FontName]);
                switch (gTextAlign)
                {
                case 0:
                    TsP.alignment = TextAlign::Left;
                    break;
                case 1:
                    TsP.alignment = TextAlign::Right;
                    break;
                case 2:
                    TsP.alignment = TextAlign::Center;
                    break;
                case 3:
                    TsP.alignment = TextAlign::Justified;
                    break;
                }
                TsP.wrap_width = gwrap_width;
                TsP.line_spacing = gline_spacing;
                TsP.show_underline = gshow_underline;
                TsP.show_strikethrough = gshow_strikethrough;
                text->SetStyle(TsP);
            }
            sq_pushbool(v, true);
        }
    }
    else {
        sq_pushbool(v, false);
    }


    return 1;
}



//设置文字对其方式
static SQInteger Squirrle_Set_Text_Object_Alignment(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);

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

    //从全局Map中获取文本角色
    if (ActorPtrMapObject.count(ActorUUID)) {
        TextActorPtr text = dynamic_cast<TextActor*>(ActorPtrMapObject[ActorUUID].Get());
        switch (TextAlignment)
        {
        case 0:
            text->SetAlignment(TextAlign::Left);
            break;
        case 1:
            text->SetAlignment(TextAlign::Right);
            break;
        case 2:
            text->SetAlignment(TextAlign::Center);
            break;
        case 3:
            text->SetAlignment(TextAlign::Justified);
            break;
        }
        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }

    return 1;
}


//设置文字描边
static SQInteger Squirrle_Set_Text_Object_Outline(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);

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

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

    //从全局Map中获取文本角色
    if (ActorPtrMapObject.count(ActorUUID)) {
        TextActorPtr text = dynamic_cast<TextActor*>(ActorPtrMapObject[ActorUUID].Get());
        StrokeStylePtr SSP = new StrokeStyle(2);
        switch (OutlineStyle)
        {
        case 0 :
            SSP->SetWidth(3.0);
            //SSP->SetCapStyle(CapStyle::Flat);
            //SSP->SetLineJoinStyle(LineJoinStyle::Miter);
            break;
        default:
            break;
        }
        text->SetOutlineStrokeStyle(SSP);
        text->SetOutlineColor(OutlineColor);
        
        sq_pushbool(v, true);

        
        //TextStyle OldT = text->GetStyle();
        //std::string TString = text->GetText();

        //TextActorPtr LeftUp = new TextActor(TString, OldT);
        //LeftUp->SetPosition(-1.0, -1.0);
        //LeftUp->SetFillColor(0xff000000);
        //LeftUp->SetZOrder(-1);
        //text->AddChild(LeftUp);
        //
        //TextActorPtr Left = new TextActor(TString, OldT);
        //Left->SetPosition(-1.0, 0);
        //Left->SetFillColor(0xff000000);
        //Left->SetZOrder(-1);
        //text->AddChild(Left);

        //TextActorPtr LeftDown = new TextActor(TString, OldT);
        //LeftDown->SetPosition(-1.0, 1.0);
        //LeftDown->SetFillColor(0xff000000);
        //LeftDown->SetZOrder(-1);
        //text->AddChild(LeftDown);

        //TextActorPtr Up = new TextActor(TString, OldT);
        //Up->SetPosition(0.0, -1.0);
        //Up->SetFillColor(0xff000000);
        //Up->SetZOrder(-1);
        //text->AddChild(Up);

        //TextActorPtr Down = new TextActor(TString, OldT);
        //Down->SetPosition(0.0, 1.0);
        //Down->SetFillColor(0xff000000);
        //Down->SetZOrder(-1);
        //text->AddChild(Down);

        //TextActorPtr RightUp = new TextActor(TString, OldT);
        //RightUp->SetPosition(1.0, -1.0);
        //RightUp->SetFillColor(0xff000000);
        //RightUp->SetZOrder(-1);
        //text->AddChild(RightUp);

        //TextActorPtr Right = new TextActor(TString, OldT);
        //Right->SetPosition(1.0, 0.0);
        //Right->SetFillColor(0xff000000);
        //Right->SetZOrder(-1);
        //text->AddChild(Right);

        //TextActorPtr RightDown = new TextActor(TString, OldT);
        //RightDown->SetPosition(1.0, 1.0);
        //RightDown->SetFillColor(0xff000000);
        //RightDown->SetZOrder(-1);
        //text->AddChild(RightDown);

        //sq_pushbool(v, true);



    }
    else {
        sq_pushbool(v, false);
    }

    return 1;
}
























//新建字体样式
static SQInteger Squirrle_Create_Font(HSQUIRRELVM v)
{
    SQInteger Top = sq_gettop(v);
    if (Top <= 0)
    {
        sq_throwerror(v, _SST("Incorrect function argument"));
        return 0;
    }

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

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

    //如果字体不存在就去创建字体
    if (!FontRecObject.count(FontName)) {
        //得到Squirrel字符串 字体路径Key
        const SQChar* OutPutBuffer;
        sq_getstring(v, 3, &OutPutBuffer);

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

        SQFloat gfont_size;
        sq_getfloat(v, 4, &gfont_size);


        FontPtr FontBuf = new Font(FontPath, gfont_size,400U,FontPosture::Normal,FontStretch::Normal);
        
        FontRecObject[FontName] = FontBuf;
        sq_pushbool(v, true);
    }
    else {
        sq_pushbool(v, false);
    }

    return 1;
}







void R_Register_Nut_Text() {
    //文字类
    SquirrelClassEx::RegisterNutApi(_SST("sq_Create_Text_Object"), Squirrle_Create_Text_Object, v);//创建文字对象
    SquirrelClassEx::RegisterNutApi(_SST("sq_Set_Text_Object_Text"), Squirrle_Set_Text_Object_Text, v);//设置文字对象文本
    SquirrelClassEx::RegisterNutApi(_SST("sq_Set_Text_Object_Color"), Squirrle_Set_Text_Object_Color, v);//设置文字对象颜色
    SquirrelClassEx::RegisterNutApi(_SST("sq_Set_Text_Object_TextStyle"), Squirrle_Set_Text_Object_TextStyle, v);//设置文字对象样式
    SquirrelClassEx::RegisterNutApi(_SST("sq_Set_Text_Object_Alignment"), Squirrle_Set_Text_Object_Alignment, v);//设置文字对其方式
    SquirrelClassEx::RegisterNutApi(_SST("sq_Set_Text_Object_Outline"), Squirrle_Set_Text_Object_Outline, v);//设置文字描边




    SquirrelClassEx::RegisterNutApi(_SST("sq_Create_Font"), Squirrle_Create_Font, v);//创建字体
}