#include "Game/GameScene/Scene.hpp"
#include "Game/GameScene/HUD/HUD.hpp"
#include "Utils/Audio/Manager.hpp"
#include "CCVideoManager.h"

int Game::CScene::m_StartingLevel = 1;
int Game::CScene::m_StartingCharacter = 1;
int Game::CScene::m_CharacterCount = 6;
int Game::CScene::m_LevelCount = 3;
int Game::CScene::m_SceneDisposed = 0;

using namespace cocos2d;

namespace Game
{
    cocos2d::Scene* CScene::createScene(int startingLevel, int startCharacter)
    {
		m_StartingLevel = startingLevel;
		m_StartingCharacter = startCharacter;

		if (m_StartingCharacter > m_CharacterCount)
		{
			m_StartingCharacter = 1;
		}
		if (m_StartingLevel > m_LevelCount)
		{
			m_StartingLevel = 1;
		}
        return CScene::create();
    }
    
    bool CScene::init()
    {
        if (!Scene::init())
        {
            return false;
        }

		m_IsVideoIsPlaying = false;
		m_VideoStartTime = 20.f;
		this->scheduleUpdate();
        
        createVisualElements();

		if (CConfig::m_VideoHasPlayed == false)
		{
			CCVideoManager::Instance()->PlayVideo("Intro.mp4");
			CConfig::m_VideoHasPlayed = true;
			m_IsVideoIsPlaying = true;
		}
        
        auto listener = EventListenerTouchOneByOne::create();
        listener->setSwallowTouches(true);
            
        listener->onTouchBegan = CC_CALLBACK_2(::Game::CScene::TouchBegan, this);
        
        Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
                
        /* Keyboard */
        
        auto keyboardListener = EventListenerKeyboard::create();
        keyboardListener->onKeyPressed = CC_CALLBACK_2(CScene::onKeyPressed, this);
        keyboardListener->onKeyReleased = CC_CALLBACK_2(CScene::onKeyReleased, this);
        
        _eventDispatcher->addEventListenerWithSceneGraphPriority(keyboardListener, this);
        
        return true;
    }

	void CScene::update(const float kfDeltaTime)
	{
		if (m_IsVideoIsPlaying == true)
		{
			m_VideoStartTime -= kfDeltaTime;
			if (m_VideoStartTime < 0.0f)
			{
				Audio::CManager::getInstance()->playMusicEffect("THEME.mp3");
				m_IsVideoIsPlaying = false;
			}

		}
	}
    
    void CScene::onKeyPressed(EventKeyboard::KeyCode keyCode, cocos2d::Event *event)
    {
        auto pcChild = m_pcWorld->getChildByTag(100);
        
        if (pcChild != nullptr)
        {
            pcChild->removeFromParent();
        }
        
        if (keyCode == EventKeyboard::KeyCode::KEY_E)
        {
			m_SceneDisposed = 0;
            m_pcWorld->interact();
        }
        else if (m_pcWorld != nullptr)
        {
            m_pcWorld->getPlayer()->keyPressed(keyCode);
        }
    }

    bool CScene::TouchBegan(cocos2d::Touch* touch, cocos2d::Event* event)
    {
        if (m_pcWorld != nullptr)
        {
            if (m_pcWorld->m_pcDialogue != nullptr)
            {
                m_pcWorld->m_pcDialogue->onTouchBegan(touch, event);
            }
        }
		return true;
    }
    
    void CScene::onKeyReleased(EventKeyboard::KeyCode keyCode, cocos2d::Event *event)
    {
        if (m_pcWorld != nullptr)
        {
            m_pcWorld->getPlayer()->keyReleased(keyCode);
        }
    }
    
    void CScene::createVisualElements()
    {
		Director* director = Director::getInstance();
		auto visibleSize = director->getVisibleSize();
		Vec2 screenCenter(visibleSize.width / 2, visibleSize.height / 2);
		
		CHUD* hud = CHUD::create();
		hud->setOpacity(0);
		hud->setPosition(screenCenter);
		this->addChild(hud, 1000);

        m_pcWorld = CWorld::create(this, hud);
		m_pcWorld->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
		m_pcWorld->setOpacity(50);
		this->addChild(m_pcWorld);
    }
}
