/*
文件名:AdMsg.nut
路径:Dps_A/BaseClass/AdMsg/AdMsg.nut
创建日期:2024-10-23	20:46
文件用途:高级消息类
*/
class AdMsg {

    RarityColor = [
        [255, 255, 255, 255], // 普通
        [104, 213, 237, 255], // 高级
        [179, 107, 255, 255], // 稀有
        [255, 0, 255, 255], // 神器
        [255, 180, 0, 255], // 史诗
        [255, 102, 102, 255], // 勇者
        [255, 20, 147, 255], // 深粉红色
        [255, 215, 0, 255] // 金色
    ];

    SendBinary = null;
    SendInfoArr = null;
    SendStrArr = null;

    //写入分隔
    function PutSeparate() {
        SendBinary.writen(0xc2, 'c');
        SendBinary.writen(0x80, 'c');
    }
    //写入普通字符串
    function WriteStr(Str) {
        local Point = Memory.allocUtf8String(Str);
        local Blob = Sq_Point2Blob(Point.C_Object, Str.len());
        SendBinary.writeblob(Blob);
    }
    //写入颜色字符串
    function WriteColorStr(Str, Color) {
        //写入分隔
        PutSeparate();
        WriteStr(Str);
        PutSeparate();

        local ColorBlob = blob(104);
        for (local i = 0; i< 3; i++) {
            ColorBlob.writen(Color[i], 'c');
        }
        for (local i = 0; i< 101; i++) {
            ColorBlob.writen(0xff, 'c');
        }
        SendInfoArr.push(ColorBlob);
    }
    //写入表情
    function WriteImotIcon(Var) {
        //写入分隔
        SendBinary.writen(0x1e, 'c');
        SendBinary.writen(0x25, 'c');
        SendBinary.writen(Var, 'c');
        SendBinary.writen(0x1f, 'c');
    }

    function JumpWrite(Blob, Pos, Value, Type) {
        Blob.seek(Pos, 'b');
        Blob.writen(Value, Type);
    }

    //写入装备
    function WriteEquipment(Str, Var, Color) {
        local ItemObj = Var;
        //写入分隔
        PutSeparate();
        WriteStr(Str);
        PutSeparate();

        local InfoBlob = blob(104);
        for (local i = 0; i< 3; i++) {
            InfoBlob.writen(Color[i], 'c');
        }
        //装备代码
        JumpWrite(InfoBlob, 0x4, ItemObj.GetIndex(), 'i');
        //品级
        JumpWrite(InfoBlob, 0x8, ItemObj.GetAdd_Info(), 'i');
        //强化等级
        JumpWrite(InfoBlob, 0xc, ItemObj.GetUpgrade(), 'c');
        //装备耐久
        JumpWrite(InfoBlob, 0xe, ItemObj.GetDurable(), 's');
        //是否封装 //TODO
        JumpWrite(InfoBlob, 0x10, ItemObj.GetAttachType(), 'c');
        //封装次数 //TODO
        JumpWrite(InfoBlob, 0x12, ItemObj.GetDurable(), 'c');
        //附魔卡片
        JumpWrite(InfoBlob, 0x14, ItemObj.GetEnchanting(), 'i');
        //红字类型
        JumpWrite(InfoBlob, 0x18, ItemObj.GetAmplification(), 'c');
        //红字数值
        JumpWrite(InfoBlob, 0x1a, 112, 'c');


        SendInfoArr.push(InfoBlob);
    }


    constructor() {
        SendStrArr = [];
        SendInfoArr = [];
        SendBinary = blob(0);
        //写入分隔
        PutSeparate();

    }

    //构造信息段结构体
    function MakeInfo() {
        return {
            Str = "",
            Flag = 0,
            Var = 0,
            Color = 0
        }
    }

    Type = 0;
    //put 类型
    function PutType(gType) {
        Type = gType;
    }

    //put 普通信息
    function PutString(Str) {
        local Info = MakeInfo();
        Info.Str = Str;
        SendStrArr.push(Info);
    }

    //put 颜色信息
    function PutColorString(Str, Color) {
        local Info = MakeInfo();
        Info.Str = Str;
        Info.Flag = 1;
        Info.Color = Color;
        SendStrArr.push(Info);
    }

    //put 表情
    function PutImoticon(Index) {
        local Info = MakeInfo();
        Info.Flag = 2;
        Info.Var = Index;
        SendStrArr.push(Info);
    }

    //put 装备
    function PutEquipment(...) {
        local Info = MakeInfo();
        if (vargv.len() > 1) {
            Info.Str = vargv[0];
            Info.Var = vargv[1];
            Info.Color = vargv[2];
        } else {
            Info.Var = vargv[0];
            Info.Str = PvfItem.GetNameById(vargv[0].GetIndex());
            Info.Color = RarityColor[PvfItem.GetPvfItemById(vargv[0].GetIndex()).GetRarity()];
        }


        Info.Flag = 3;
        SendStrArr.push(Info);
    }

    function Finalize() {
        //写入字符串
        foreach(Info in SendStrArr) {
            //普通字符串
            if (Info.Flag == 0) WriteStr(Info.Str);
            //写入颜色字符串
            else if (Info.Flag == 1) WriteColorStr(Info.Str, Info.Color);
            //写入表情
            else if (Info.Flag == 2) WriteImotIcon(Info.Var);
            //写入装备
            else if (Info.Flag == 3) WriteEquipment(Info.Str, Info.Var, Info.Color);
        }
    }

    Pack = null;

    function MakePack() {
        //文字信息长度
        local SendBinaryLen = SendBinary.len();
        //申请内存
        local SendStrPoint = Memory.alloc(SendBinaryLen);
        Sq_WriteBlobToAddress(SendStrPoint.C_Object, SendBinary);

        Pack = Packet();
        Pack.Put_Header(0, 370);
        Pack.Put_Byte(Type);
        Pack.Put_Short(0);
        Pack.Put_Byte(3);
        Pack.Put_Int(SendBinaryLen);
        Pack.Put_BinaryEx(SendStrPoint.C_Object, SendBinaryLen);
        Pack.Put_Byte(SendInfoArr.len());
        for (local i = 0; i< SendInfoArr.len(); i++) {
            local Point = Memory.alloc(104);
            Sq_WriteBlobToAddress(Point.C_Object, SendInfoArr[i]);
            Pack.Put_BinaryEx(Point.C_Object, 104);
        }
        Pack.Finalize(true);
        return Pack;
    }

    function Delete() {
        Pack.Delete();
    }
}