//
//  InteractiveObject.cpp
//  Game
//
//  Created by Darvydas on 26/01/2019.
//

#include "InteractiveObject.hpp"

namespace Game
{
    namespace Objects
    {
        CInteractiveObject *CInteractiveObject::create(const rapidjson::Value& krValue) {
            
            CInteractiveObject *sp = new CInteractiveObject;
                        
            sp->initData(krValue);
            
            if (sp->initWithFile(sp->m_strPngPath)) {
                
                sp->autorelease();
                
                return sp;
            }
            CC_SAFE_DELETE(sp);
            return NULL;
        }
        
        void CInteractiveObject::initData(const rapidjson::Value& krValue)
        {
            if (krValue.HasMember("FileName"))
            {
                const rapidjson::Value& name = krValue["FileName"];
                m_strPngPath = name.GetString();
            }
            if (krValue.HasMember("Type"))
            {
                const rapidjson::Value& name = krValue["Type"];
                m_strType = name.GetString();
            }
            if (krValue.HasMember("SizeX"))
            {
                const rapidjson::Value& name = krValue["SizeX"];
                m_width = name.GetInt();
            }
            if (krValue.HasMember("SizeY"))
            {
                const rapidjson::Value& name = krValue["SizeY"];
                m_height = name.GetInt();
            }
            if (krValue.HasMember("Action"))
            {
                const rapidjson::Value& array = krValue["Action"];
                
                for (rapidjson::SizeType i = 0; i < array.Size(); i++)
                {
                    const rapidjson::Value& object = array[i];
                    
                    const rapidjson::Value& name = object["Name"];
                    
                    if (object.HasMember("Sounds"))
                    {
                        const rapidjson::Value& kvSound = object["Sounds"];
                        m_strSound = kvSound.GetString();
                    }
                    
                    int ivitality = 0;
                    int iintelligence = 0;
                    int isanity = 0;
                    int iendurance = 0;
                    int icharisma = 0;
                    
                    if (object.HasMember("Stats"))
                    {
                        const rapidjson::Value& stats = object["Stats"];
                        
                        for (rapidjson::SizeType i = 0; i < stats.Size(); i++)
                        {
                            const rapidjson::Value& obj = stats[i];
                            
                            if (obj.HasMember("Vitality"))
                            {
                                const rapidjson::Value& vitality = obj["Vitality"];
                                ivitality = vitality.GetInt();
                            }
                            if (obj.HasMember("Intelligence"))
                            {
                                const rapidjson::Value& intelligence = obj["Intelligence"];
                                iintelligence = intelligence.GetInt();
                                
                            }
                            if (obj.HasMember("Sanity"))
                            {
                                const rapidjson::Value& sanity = obj["Sanity"];
                                isanity = sanity.GetInt();
                            }
                            if (obj.HasMember("Endurance"))
                            {
                                const rapidjson::Value& endurance = obj["Endurance"];
                                iendurance = endurance.GetInt();
                            }
                            if (obj.HasMember("Charisma"))
                            {
                                const rapidjson::Value& charisma = obj["Charisma"];
                                icharisma = charisma.GetInt();
                            }
                        }
                    }
                    
                    m_vActions.push_back(new CAction(name.GetString(), m_strSound, ivitality, iintelligence, isanity, iendurance, icharisma));
                }
            }
        }
    }
}
