#include "Asset/AnimationStruct.h"
#include "Asset/ScriptData.h"

namespace AniScriptParser
{

    std::string Get_Ani_Flag(int data)
    {
        switch (data)
        {
        case 0:
            return "LOOP";
        case 1:
            return "SHADOW";
        case 3:
            return "COORD";
        case 7:
            return "IMAGE_RATE";
        case 8:
            return "IMAGE_ROTATE";
        case 9:
            return "RGBA";
        case 10:
            return "INTERPOLATION";
        case 11:
            return "GRAPHIC_EFFECT";
        case 12:
            return "DELAY";
        case 13:
            return "DAMAGE_TYPE";
        case 14:
            return "DAMAGE_BOX";
        case 15:
            return "ATTACK_BOX";
        case 16:
            return "PLAY_SOUND";
        case 17:
            return "PRELOAD";
        case 18:
            return "SPECTRUM";
        case 23:
            return "SET_FLAG";
        case 24:
            return "FLIP_TYPE";
        case 25:
            return "LOOP_START";
        case 26:
            return "LOOP_END";
        case 27:
            return "CLIP";
        case 28:
            return "OPERATION";
        default:
            return "";
        }
    }

    std::string Get_Ani_Effect_Type(int data)
    {
        switch (data)
        {
        case 0:
            return "NONE";
        case 1:
            return "DODGE";
        case 2:
            return "LINEARDODGE";
        case 3:
            return "DARK";
        case 4:
            return "XOR";
        case 5:
            return "MONOCHROME";
        case 6:
            return "SPACEDISTORT";
        default:
            return "";
        }
    };

    std::string Get_Ani_Flip_Type(int data)
    {
        switch (data)
        {
        case 1:
            return "HORIZON";
        case 2:
            return "VERTICAL";
        case 3:
            return "ALL";
        default:
            return "";
        }
    };

    std::string Get_Ani_Damage_Type(int data)
    {
        switch (data)
        {
        case 0:
            return "NORMAL";
        case 1:
            return "SUPERARMOR";
        case 2:
            return "UNBREAKABLE";
        default:
            return "";
        }
    };

    AlsInfo StructAlsInfo(ScriptData &Data)
    {
        AlsInfo Info;
        while (!Data.IsEnd())
        {
            std::string Segment = Data.Get();
            if (Segment == "[use animation]")
            {
                std::string AniPath = Data.Get();
                std::string AniKey = Data.Get();
                Info.AniList[AniKey].path = Tool_toLowerCase(AniPath);
            }
            else if (Segment == "[none effect add]")
            {
                std::vector<int> AniLayer = {std::stoi(Data.Get()), std::stoi(Data.Get())};
                std::string AniKey = Data.Get();
                Info.AniList[AniKey].layer = AniLayer;
            }
            else if (Segment == "[add]")
            {
                std::vector<int> AniLayer = {std::stoi(Data.Get()), std::stoi(Data.Get())};
                std::string AniKey = Data.Get();
                Info.AniList[AniKey].layer = AniLayer;
            }
        }
        return Info;
    };

    AniInfo StructAniInfo(ScriptData &Data)
    {
        AniInfo Info;
        while (!Data.IsEnd())
        {
            std::string Segment = Data.Get();
            if (Segment == "[image]")
            {
            }
        }
        return Info;
    };

    /////////以下是读取逻辑
    AniInfo StructAniInfo(Blob blob)
    {
        AniInfo Info;
        int Frame_Max = blob.getUShort();
        int Img_Count = blob.getUShort();

        // Img的路径读取 存入数组
        for (int i = 0; i < Img_Count; i++)
        {
            int Buf = blob.getInt();
            std::string ImgPath = "sprite/" + blob.getString(Buf);
            Info.Img_List.push_back(ImgPath);
        }

        // Ani头部标签数量
        int Ani_H_Item_Count = blob.getUShort();
        // 处理标签
        for (int i = 0; i < Ani_H_Item_Count; i++)
        {
            int Type = blob.getUShort();
            switch (Type)
            {
            case 0:
            case 1:
            {
                std::string Key = AniScriptParser::Get_Ani_Flag(Type);
                int Value = blob.getByte();
                Info.Flag.emplace(Key, Value);
                break;
            }
            case 3:
            case 28:
            {
                std::string Key = AniScriptParser::Get_Ani_Flag(Type);
                int Value = blob.getUShort();
                Info.Flag.emplace(Key, Value);
                break;
            }
            case 18:
            {
                blob.getByte();
                blob.getInt();
                blob.getInt();
                blob.getInt();
                blob.get256();
                blob.get256();
                blob.get256();
                blob.get256();
                blob.getUShort();
            }
            default:
                break;
            }
        }

        // 读取每一个Img
        for (int i = 0; i < Frame_Max; i++)
        {
            AniFrame FrameObject;

            // 碰撞框项目数量
            int Ani_Box_Item_Count = blob.getUShort();
            for (int j = 0; j < Ani_Box_Item_Count; j++)
            {
                int Box_Type = blob.getUShort();
                std::vector<int> D_Box_b;
                for (int k = 0; k < 6; k++)
                {
                    D_Box_b.push_back(blob.getInt());
                }
                if (Box_Type == 15)
                    FrameObject.AttackBox.push_back(D_Box_b);
                else
                    FrameObject.DamageBox.push_back(D_Box_b);
            }

            // 调用的第几个Img
            int Index_Buf = blob.getShort();
            if (Index_Buf != -1)
            {
                FrameObject.Img_Path = Tool_toLowerCase(Info.Img_List[Index_Buf]);
                FrameObject.Img_Index = blob.getUShort();
            }
            else
            {
                FrameObject.Img_Path = "";
                FrameObject.Img_Index = 0;
            }

            // 坐标
            FrameObject.Img_Pos = VecPos{blob.getInt(), blob.getInt()};

            // Img中的项目数量
            int Img_Flag_Count = blob.getUShort();
            for (int j = 0; j < Img_Flag_Count; j++)
            {
                int Img_Flag_Type = blob.getUShort();
                std::string Key;
                int Value;
                switch (Img_Flag_Type)
                {
                case 0:
                case 1:
                case 10:
                {
                    Key = AniScriptParser::Get_Ani_Flag(Img_Flag_Type);
                    Value = blob.getByte();
                    FrameObject.Flag.emplace(Key, Value);
                    break;
                }
                case 3:
                {
                    Key = "COORD";
                    Value = blob.getUShort();
                    FrameObject.Flag.emplace(Key, Value);
                    break;
                }
                case 17:
                {
                    Key = "PRELOAD";
                    Value = 1;
                    FrameObject.Flag.emplace(Key, Value);
                    break;
                }
                case 7:
                {
                    Key = "IMAGE_RATE";
                    Vec2 pos{
                        blob.getFloat(),
                        blob.getFloat()};
                    FrameObject.Flag.emplace(Key, pos);
                    break;
                }
                case 8:
                {
                    Key = "IMAGE_ROTATE";
                    float RateBuffer = blob.getFloat();
                    FrameObject.Flag.emplace(Key, RateBuffer);
                    break;
                }
                case 9:
                {
                    Key = "RGBA";
                    std::vector<int> RGBA = {
                        (int)blob.get256(),
                        (int)blob.get256(),
                        (int)blob.get256(),
                        (int)blob.get256()};
                    FrameObject.Flag.emplace(Key, RGBA);
                    break;
                }
                case 11:
                {
                    int Effect_Type = blob.getUShort();
                    Key = "GRAPHIC_EFFECT_" + AniScriptParser::Get_Ani_Effect_Type(Effect_Type);
                    std::vector<float> effect;
                    switch (Effect_Type)
                    {
                    case 5:
                    {
                        effect.push_back(blob.get256());
                        effect.push_back(blob.get256());
                        effect.push_back(blob.get256());
                        break;
                    }
                    case 6:
                    {
                        effect.push_back(blob.get256());
                        effect.push_back(blob.get256());
                        break;
                    }
                    }
                    FrameObject.Flag.emplace(Key, effect);
                    break;
                }
                case 12:
                {
                    Value = blob.getInt();
                    FrameObject.Delay = Value;
                    break;
                }
                case 13:
                {
                    Key = "DAMAGE_TYPE";
                    std::string DTYPE = AniScriptParser::Get_Ani_Damage_Type(blob.getUShort());
                    FrameObject.Flag.emplace(Key, DTYPE);
                    break;
                }
                case 16:
                {
                    int SoundTempSize = blob.getInt();
                    Key = "PLAY_SOUND";
                    std::string sound = blob.getString(SoundTempSize);
                    FrameObject.Flag.emplace(Key, sound);
                    break;
                }
                case 23:
                {
                    Key = "SET_FLAG";
                    Value = blob.getInt();
                    FrameObject.Flag.emplace(Key, Value);
                    break;
                }
                case 24:
                {
                    Key = "FLIP_TYPE";
                    std::string FTYPEValue = AniScriptParser::Get_Ani_Flip_Type(blob.getUShort());
                    FrameObject.Flag.emplace(Key, FTYPEValue);
                    break;
                }
                case 25:
                {
                    Key = "LOOP_START";
                    FrameObject.Flag.emplace(Key, 1);
                    break;
                }
                case 26:
                {
                    Key = "LOOP_END";
                    Value = blob.getInt();
                    FrameObject.Flag.emplace(Key, Value);
                    break;
                }
                case 27:
                {
                    Key = "CLIP";
                    std::vector<int> ClipArr{
                        blob.getShort(),
                        blob.getShort(),
                        blob.getShort(),
                        blob.getShort(),
                    };
                    FrameObject.Flag.emplace(Key, ClipArr);
                    break;
                }
                default:
                    break;
                }
            }

            Info.Frame.push_back(FrameObject);
        }
        return Info;
    }
};