//
//  World.cpp
//  Game
//
//  Created by Darvydas on 24/01/2019.
//

//#include "cocos2d.h"
#include "Config/Config.hpp"
#include "Game/GameScene/HUD/HUD.hpp"
#include "Game/GameScene/HUD/StatisticBar.hpp"
#include "Game/GameScene/World/Player.hpp"
#include "Utils/json/Manager.hpp"

using namespace cocos2d;

namespace Game
{
    CHUD* CHUD::create()
    {
        CHUD* hud = new CHUD;
        
        if (hud->init())
        {
			hud->setTextureRect(Rect(0, 0, CConfig::m_ResoliutionX, CConfig::m_ResoliutionY));
			hud->autorelease();
            
			hud->createVisualElements();
            
            return hud;
        }
        CC_SAFE_DELETE(hud);
        return NULL;
    }
    
    void CHUD::createVisualElements()
    {
		Director* director = Director::getInstance();
		auto visibleSize = director->getVisibleSize();
			
		// parse config
		json::CManager* manager = json::CManager::getInstance();
		if (manager->openFile("HUDConfig.json"))
		{
			rapidjson::Document& document = *manager->getOpenedDocument();

			if (document.HasMember("StatsBar"))
			{
				const rapidjson::Value& array = document["StatsBar"];  // you are missing this
				if (array.IsArray())
				{
					for (rapidjson::SizeType i = 0; i < array.Size(); i++)
					{
						const rapidjson::Value& statsBar = array[i];

						CStatisticBar* statisticBar = CStatisticBar::create(statsBar);
						this->addChild(statisticBar);

						if (strcmp(statisticBar->m_Name.c_str(), "Vitality") == 0)
						{
							m_VitalityBar = statisticBar;
							//CPlayer::SCharacter* sChar = &player->m_CharacterData;
							//statisticBar->setInitialValue(sChar->vitality);
						}

						if (strcmp(statisticBar->m_Name.c_str(), "Intelligence") == 0)
						{
							m_IntelligenceBar = statisticBar;
							//CPlayer::SCharacter* sChar = &player->m_CharacterData;
							//statisticBar->setInitialValue(sChar->intelligence);
						}

						if (strcmp(statisticBar->m_Name.c_str(), "Sanity") == 0)
						{
							m_SanityBar = statisticBar;
							//CPlayer::SCharacter* sChar = &player->m_CharacterData;
							//statisticBar->setInitialValue(sChar->sanity);
						}

						if (strcmp(statisticBar->m_Name.c_str(), "Endurance") == 0)
						{
							m_EnduranceBar = statisticBar;
							//CPlayer::SCharacter* sChar = &player->m_CharacterData;
							//statisticBar->setInitialValue(sChar->endurance);
						}

						if (strcmp(statisticBar->m_Name.c_str(), "Charisma") == 0)
						{
							m_CharismaBar = statisticBar;
							//CPlayer::SCharacter* sChar = &player->m_CharacterData;
							//statisticBar->setInitialValue(sChar->charisma);
						}

						//StatisticBar* bar = StatisticBar::create();
						//log("Full Name is :---  %s %s", firstName.c_str(), lastName.c_str());
					}
				}
			}
		}

		// Open Config
		// Parse statistic Bars

		//CPlayer* player = CPlayer::create();
		//player->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
		//this->addChild(player);
    }
}





