//
//  Dialogue.cpp
//  Game
//
//  Created by Darvydas on 27/01/2019.
//

#include "Config/Config.hpp"
#include "Game/GameScene/World/Dialogue.hpp"
#include "Utils/Audio/Manager.hpp"

using namespace cocos2d;

namespace Game
{
    Dialogue *Dialogue::create(CPlayer* pcPlayer, Game::Objects::CInteractiveObject* obj) {
        
        Dialogue *sp = new Dialogue;
        
        sp->m_pcPlayer = pcPlayer;
        sp->m_pcObj = obj;
        
        if (sp->init()) {
            
            sp->setTextureRect(Rect(0, 0, CConfig::m_fTileDimension * 4.0f, CConfig::m_DialogueHeight * obj->m_vActions.size()));
            sp->setPosition(Point(obj->getPosition().x, obj->getPosition().y + obj->getContentSize().height * 0.5f + CConfig::m_fTileDimension * 0.25f));
            sp->setColor(Color3B::WHITE);
            sp->setOpacity(255 * 0.3f);
            sp->autorelease();
            
            s_dialogueIsShowing = true;
            
            sp->setTag(100);
            
            sp->createVisualElements();
                        
            return sp;
        }
        CC_SAFE_DELETE(sp);
        return NULL;
    }
    
    void Dialogue::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event)
    {
        auto locationInDialogue = this->convertToNodeSpace(touch->getLocation());
        
        for (auto* button : m_vButtons)
        {
            if (button->getBoundingBox().containsPoint(locationInDialogue))
            {
                if (!button->m_pcAction->m_strSoundName.empty())
                {
                    Audio::CManager::getInstance()->playSoundEffect(button->m_pcAction->m_strSoundName);
                }
                m_pcPlayer->AddCharisma(button->m_pcAction->m_Charisma);
                m_pcPlayer->AddEndurance(button->m_pcAction->m_Endurance);
                m_pcPlayer->AddIntelligence(button->m_pcAction->m_Intelligence);
                m_pcPlayer->AddVitality(button->m_pcAction->m_Vitality);
                m_pcPlayer->AddSanity(button->m_pcAction->m_Sanity);
                
                this->runAction(Sequence::create(
                                                 DelayTime::create(0.1f),
                                                 RemoveSelf::create(),
                                                 nullptr
                                ));
            }
        }
    }
    
    void Dialogue::createVisualElements()
    {
        for (int i = 0; i < m_pcObj->m_vActions.size(); i++)
        {
            auto button = CButton::create(m_pcObj->m_vActions[i]);
            
            button->setTextureRect(Rect(0.0f, 0.0f, this->getContentSize().width * 0.95, CConfig::m_DialogueHeight - this->getContentSize().width * 0.05));
            button->setPosition(Point(this->getContentSize().width * 0.5f, CConfig::m_DialogueHeight * 0.5f + CConfig::m_DialogueHeight * i));
            
            button->m_pcLabel->setString(m_pcObj->m_vActions[i]->m_strName);
            button->m_pcLabel->setSystemFontSize(24);
            button->m_pcLabel->setPosition(Point(this->getContentSize().width * 0.5f, button->getContentSize().height * 0.5f));
            
            this->addChild(button, 200);
            
            m_vButtons.push_back(button);
        }
    }
}





