#include "Equipment.h"
#include "Global/Global_Game.h"
#include "Asset/AssetManager.h"

int Equipment::GetJobIndex(std::string JobName)
{
    if (JobName == "[swordman]")
        return 0;
    else if (JobName == "[fighter]")
        return 1;
    else if (JobName == "[gunner]")
        return 2;
    else if (JobName == "[mage]")
        return 3;
    else if (JobName == "[priest]")
        return 4;
    else if (JobName == "[atgunner]")
        return 5;
    else if (JobName == "[thief]")
        return 6;
    else if (JobName == "[atfighter]")
        return 7;
    else if (JobName == "[atmage]")
        return 8;
    else if (JobName == "[demonic swordman]")
        return 9;
    else if (JobName == "[creatormage]")
        return 10;
    return -1;
}

ATTACH_TYPE Equipment::GetTradeType(std::string TradeType)
{
    if (TradeType == "[free]")
        return ATTACH_TYPE::FREE;
    else if (TradeType == "[sealing]")
        return ATTACH_TYPE::SEALING;
    else if (TradeType == "[trade]")
        return ATTACH_TYPE::TRADE;
    else if (TradeType == "[account]")
        return ATTACH_TYPE::ACCOUNT;
    else if (TradeType == "[trade delete]")
        return ATTACH_TYPE::TRADE_DELETE;
    else if (TradeType == "[sealing trade]")
        return ATTACH_TYPE::SEALING_TRADE;
    return ATTACH_TYPE::FREE;
}

bool Equipment::IsEmpty()
{
    if (m_EquipmentPath.empty())
        return false;
    return true;
}

void Equipment::Init(int Index)
{
    ConstructionByIndex(Index);
}

void Equipment::Init(int Index, int Quality)
{
    this->m_Quality = Quality;
    ConstructionByIndex(Index);
}

void Equipment::ConstructionByIndex(int Index)
{
    // 检查装备list有没有这件装备
    if (Global_Game::GetInstance().EquipmentPathMap.count(Index))
    {
        m_EquipmentPath = Global_Game::GetInstance().EquipmentPathMap[Index];
        // 读取装备信息
        ScriptData Data = AssetManager::GetInstance().GetScriptInfo(m_EquipmentPath);
        while (!Data.IsEnd())
        {
            std::string Segment = Data.Get();
            if (Segment == "[name]")
            {
                this->Name = Data.Get();
            }
            else if (Segment == "[name2]")
            {
                this->Name2 = Data.Get();
            }
            else if (Segment == "[grade]")
            {
                this->Grade = std::stoi(Data.Get());
            }
            else if (Segment == "[rarity]")
            {
                this->Rarity = std::stoi(Data.Get());
            }
            else if (Segment == "[usable job]")
            {
                while (true)
                {
                    std::string Buf = Data.Get();
                    if (Buf == "[/usable job]")
                        break;
                    this->UsableJob[GetJobIndex(Buf)] = true;
                }
            }
            else if (Segment == "[attach type]")
            {
                this->TradeType = GetTradeType(Data.Get());
            }
            else if (Segment == "[minimum level]")
            {
                this->MinimumLevel = std::stoi(Data.Get());
            }
            else if (Segment == "[price]")
            {
                this->Price = std::stoi(Data.Get());
            }
            else if (Segment == "[repair price]")
            {
                this->RepairPrice = std::stoi(Data.Get());
            }
            else if (Segment == "[animation job]")
            {
                std::string JobString = Data.Get();
                int JobIndex = GetJobIndex(JobString);
                this->JobAni[JobIndex].clear();
                if (Data.Get() == "[variation]")
                {
                    int Parm1 = std::stoi(Data.Get());
                    int Parm2 = std::stoi(Data.Get());

                    while (true)
                    {
                        JobAnimation JobAniBuffer;
                        JobAniBuffer.ImgFormat.clear();
                        JobAniBuffer.ImgFormat.push_back(Parm1);
                        JobAniBuffer.ImgFormat.push_back(Parm2);

                        std::string buf = Data.Get();
                        if (buf == "[layer variation]")
                        {
                            JobAniBuffer.Layer = std::stoi(Data.Get());
                            JobAniBuffer.AnimationGroup = Data.Get();
                            this->JobAni[JobIndex].push_back(JobAniBuffer);
                            // 调用两次 跳过原来的 [equipment ani script] 这个没有用
                            std::string B1 = Data.Get();
                            std::string B2 = Data.Get();
                        }
                        // 皮肤没有图层也没有动画组名称
                        else if (m_EquipmentPath.find("skin") != std::string::npos)
                        {
                            JobAniBuffer.Layer = 0;
                            JobAniBuffer.AnimationGroup = "null";
                            this->JobAni[JobIndex].push_back(JobAniBuffer);
                            break;
                        }
                        else
                        {
                            Data.Back();
                            break;
                        }
                    }
                }
            }
        }
    }
    else
    {
        SDL_LogError(0, "没有编号为: %d 这件装备", Index);
    }
}
