//
//  World.cpp
//  Game
//
//  Created by Darvydas on 24/01/2019.
//
#include "Game/GameScene/HUD/StatisticBar.hpp"

using namespace cocos2d;

namespace Game
{
	CStatisticBar* CStatisticBar::create(const rapidjson::Value& statsBar)
    {
        CStatisticBar* statBar = new CStatisticBar;
        
        if (statBar->init())
        {
			statBar->autorelease();
			statBar->scheduleUpdate();
			statBar->createVisualElements(statsBar);

			return statBar;
		}
        CC_SAFE_DELETE(statBar);
        return NULL;
    }
    
    void CStatisticBar::createVisualElements(const rapidjson::Value& statsBar)
    {
		std::string font;
		std::string phrase;
		std::string id;
		std::string bottomTex;
		std::string topTex;
		std::string frameTex;
		int posX;
		int posY;
		int fontOffsetX;
		int fontOffsetY;
		int fontSize = 24;

		if(statsBar.HasMember("font"))
		{
			const rapidjson::Value& name = statsBar["font"];
			font = cocos2d::Value(name.GetString()).asString();
		}

		if (statsBar.HasMember("fontOffsetX"))
		{
			const rapidjson::Value& name = statsBar["fontOffsetX"];
			fontOffsetX = name.GetInt();
		}

		if (statsBar.HasMember("fontOffsetY"))
		{
			const rapidjson::Value& name = statsBar["fontOffsetY"];
			fontOffsetY = name.GetInt();
		}

		if (statsBar.HasMember("fontSize"))
		{
			const rapidjson::Value& name = statsBar["fontSize"];
			fontSize = name.GetInt();
		}

		if (statsBar.HasMember("phrase"))
		{
			const rapidjson::Value& name = statsBar["phrase"];
			phrase = cocos2d::Value(name.GetString()).asString();
			m_Name = phrase;
		}

		if (statsBar.HasMember("id"))
		{
			const rapidjson::Value& name = statsBar["id"];
			id = cocos2d::Value(name.GetString()).asString();
		}

		if (statsBar.HasMember("bottomTexture"))
		{
			const rapidjson::Value& name = statsBar["bottomTexture"];
			bottomTex = cocos2d::Value(name.GetString()).asString();
		}

		if (statsBar.HasMember("frameTexture"))
		{
			const rapidjson::Value& name = statsBar["frameTexture"];
			frameTex = cocos2d::Value(name.GetString()).asString();
		}

		if (statsBar.HasMember("topTexture"))
		{
			const rapidjson::Value& name = statsBar["topTexture"];
			topTex = cocos2d::Value(name.GetString()).asString();
		}

		if (statsBar.HasMember("posX"))
		{
			const rapidjson::Value& name = statsBar["posX"];
			posX = name.GetInt();
		}

		if (statsBar.HasMember("posY"))
		{
			const rapidjson::Value& name = statsBar["posY"];
			posY = name.GetInt();
		}

		auto label = Label::createWithTTF(phrase.c_str(), font.c_str(), fontSize);
		// position the label on the center of the screen
		label->setPosition(Vec2(posX + fontOffsetX, posY + fontOffsetY));
		label->enableOutline(Color4B(0, 0, 0, 255), label->getContentSize().height * 0.05f);

		m_BarRed = cocos2d::Sprite::create(bottomTex);
		m_BarGreen = cocos2d::Sprite::create(topTex);
		m_BarFrame = cocos2d::Sprite::create(frameTex);

		if (statsBar.HasMember("statsHigher"))
		{
			const rapidjson::Value& name = statsBar["statsHigher"];
			std::string tex = cocos2d::Value(name.GetString()).asString();
			m_StatsHigher = cocos2d::Sprite::create(tex);
			m_StatsHigher->setVisible(false);
			m_StatsHigher->setPosition(posX, posY);
		}

		if (statsBar.HasMember("statsLower"))
		{
			const rapidjson::Value& name = statsBar["statsLower"];
			std::string tex = cocos2d::Value(name.GetString()).asString();
			m_StatsLower = cocos2d::Sprite::create(tex);
			m_StatsLower->setVisible(false);
			m_StatsLower->setPosition(posX, posY);
		}


		this->setTextureRect(Rect(0, 0,
								  m_BarFrame->getContentSize().width,
								  m_BarFrame->getContentSize().height));

		m_BarFrame->setPosition(Vec2(posX, posY));


		m_ProgressTimerGreen = ProgressTimer::create(m_BarGreen);
		m_ProgressTimerGreen->setPosition(Vec2(posX, posY));
		m_ProgressTimerGreen->setMidpoint(Vec2(0.0f, 0.1f));
		m_ProgressTimerGreen->setType(ProgressTimer::Type::BAR);
		m_ProgressTimerGreen->setAnchorPoint(Point(0.5f, 0.5f));
		m_ProgressTimerGreen->setBarChangeRate(Point(1, 0));
		m_ProgressTimerGreen->setPercentage(100.0f);

		m_ProgressTimerRed = ProgressTimer::create(m_BarRed);
		m_ProgressTimerRed->setPosition(Vec2(posX, posY));
		m_ProgressTimerRed->setMidpoint(Vec2(0.0f, 0.1f));
		m_ProgressTimerRed->setType(ProgressTimer::Type::BAR);
		m_ProgressTimerRed->setAnchorPoint(Point(0.5f, 0.5f));
		m_ProgressTimerRed->setBarChangeRate(Point(1, 0));
		m_ProgressTimerRed->setPercentage(100.0f);

		if (m_BarFrame != nullptr)
		{
			this->addChild(m_BarFrame);
		}
		if (m_ProgressTimerRed != nullptr)
		{
			this->addChild(m_ProgressTimerRed);
		}
		if (m_ProgressTimerGreen != nullptr)
		{
			this->addChild(m_ProgressTimerGreen);
		}
		if (m_StatsHigher != nullptr)
		{
			this->addChild(m_StatsHigher);
		}
		if (m_StatsLower != nullptr)
		{
			this->addChild(m_StatsLower);
		}

		// add the label as a child to this layer
		this->addChild(label);
	}

	void CStatisticBar::setInitialProgress(Type type, float greenProgress, float redProgress)
	{
		m_Type = type;
		if (m_Type == MORE_THEN)
		{
			m_InitialProgress = greenProgress;
			m_TargetProgress = redProgress;

			const cocos2d::Vec2& pos = m_StatsHigher->getPosition();
			const cocos2d::Size& size = m_BarGreen->getContentSize();

			cocos2d::Vec2 newPos(pos.x + (size.width * 0.01f * redProgress - size.width * 0.5f), pos.y);
			m_StatsHigher->setPosition(newPos);
			m_StatsHigher->setVisible(true);

			m_ProgressTimerGreen->setPercentage(50.0f);
			m_ProgressTimerRed->setPercentage(m_TargetProgress);
		}
		else if (m_Type == LESS_THEN)
		{
			m_InitialProgress = redProgress;
			m_TargetProgress = greenProgress;

			const cocos2d::Vec2& pos = m_StatsHigher->getPosition();
			const cocos2d::Size& size = m_BarGreen->getContentSize();

			//cocos2d::Vec2 newPos(pos.x + (size.width * 0.01f * greenProgress - size.width * 0.5f), pos.y);
			//m_StatsLower->setPosition(newPos);
			m_StatsLower->setVisible(true);

			m_ProgressTimerGreen->setPercentage(greenProgress);
			m_ProgressTimerRed->setPercentage(redProgress);
		}
		else // dont care
		{
			m_InitialProgress = greenProgress;
			m_TargetProgress = 0;

			m_ProgressTimerGreen->setPercentage(greenProgress);
			m_ProgressTimerRed->setPercentage(0.0f);
		}
	}

	void CStatisticBar::addValue(int value)
	{
		m_CurrentValue += value;
		if (m_CurrentValue < 0)
		{
			m_CurrentValue = 0;
		}
		if (m_CurrentValue > 20)
		{
			m_CurrentValue = 20;
		}
		float percent = m_CurrentValue * 100.f / 20.0f;
		setProgress(percent);
	}

	void CStatisticBar::setProgress(float percent)
	{
		if (m_Type == MORE_THEN)
		{
			m_ProgressTimerGreen->setPercentage(percent);
			m_ProgressTimerRed->setPercentage(m_TargetProgress);
		}
		else if (m_Type == LESS_THEN)
		{
 			m_ProgressTimerGreen->setPercentage(percent > m_TargetProgress ? m_TargetProgress : percent);
			m_ProgressTimerRed->setPercentage(percent);
		}
		else
		{
			m_ProgressTimerGreen->setPercentage(percent);
			m_ProgressTimerRed->setPercentage(0);
		}
	}

	void CStatisticBar::update(float deltaTime)
	{
		/*if (m_Name == "Endurance")
		{
			log("%d\n", deltaTime);

			if (m_Type == MORE_THEN)
			{
				float percent = m_ProgressTimerGreen->getPercentage();

				percent += deltaTime * 1.f;

				setProgress(percent);
			}
			else if (m_Type == LESS_THEN)
			{
				float percent = m_ProgressTimerRed->getPercentage();

				percent -= deltaTime * 1.f;

				setProgress(percent);
			}

		}*/
	}

	void CStatisticBar::setInitialValue(int value)
	{
		m_CurrentValue = value;
	}

	void CStatisticBar::calcInitialProgress(int value, bool forceDontCare)
	{
		if (value == 10 || forceDontCare == true)
		{
			setInitialProgress(CStatisticBar::DONT_CARE, value * 100.0f / 20.f, 0.0f);
		}
		else
		{
			if (value < 10)
			{
				setInitialProgress(CStatisticBar::LESS_THEN, value * 100.0f / 20.f, 50.0f);
			}
			else
			{
				setInitialProgress(CStatisticBar::MORE_THEN, 50.0f, value * 100.0f / 20.f);
			}
		}
	}
}





