/******************************************************************************
*	File		-	dataManager.cpp
*	Author		-	Joey Pollack
*	Date		-	2021/12/01 (y/m/d)
*	Mod Date	-	2021/12/01 (y/m/d)
*	Description	-	Manage the raw internal asset data, like icons images
*                   UI sounds etc..
******************************************************************************/

#include "data_manager.h"

#include <core/core.h>
#include <utils/logger.h>
#include <renderer/texture.h>
#include <assets/types/image.h>

// ASSETS
#include "data_headers/open_font.h"
#include "data_headers/roboto_font.h"
#include "data_headers/folder16.h"
#include "data_headers/new_folder.h"
#include "data_headers/up_arrow_icon.h"
#include "data_headers/play_arrow.h"
#include "data_headers/pause.h"
#include "data_headers/stop.h"

#include <fstream>

namespace lunarium
{
    // Init asset pointers
    Texture* DataManager::mFolderIcon = nullptr;
    Texture* DataManager::mNewFolderIcon = nullptr;
    Texture* DataManager::mUpArrowIcon = nullptr;
    Texture* DataManager::mPlayArrow = nullptr;
    Texture* DataManager::mPause = nullptr;
    Texture* DataManager::mStop = nullptr;

    void DataManager::Initialize()
    {
        /////////////////////////////////////////
        // Folder16
        TextureFormat format = TextureFormat::RGB;
        if (lunarium_data_folder16::folder16DataNumChannels == 4)
        {
            format = TextureFormat::RGBA;
        }
        unsigned char* pData = new unsigned char[lunarium_data_folder16::folder16DataSize];
        memcpy(pData, lunarium_data_folder16::folder16Data, lunarium_data_folder16::folder16DataSize);
        mFolderIcon = Texture::Create(pData, lunarium_data_folder16::folder16DataWidth, lunarium_data_folder16::folder16DataHeight, format);
        //////////////////////////////////////////

        /////////////////////////////////////////
        // new_dir_icon
        format = TextureFormat::RGB;
        if (lunarium_data_newdir::new_folderDataNumChannels == 4)
        {
            format = TextureFormat::RGBA;
        }
        pData = new unsigned char[lunarium_data_newdir::new_folderDataSize];
        memcpy(pData, lunarium_data_newdir::new_folderData, lunarium_data_newdir::new_folderDataSize);
        mNewFolderIcon = Texture::Create(pData, lunarium_data_newdir::new_folderDataWidth, lunarium_data_newdir::new_folderDataHeight, format);
        //////////////////////////////////////////

        /////////////////////////////////////////
        // up_arrow_icon
        format = TextureFormat::RGB;
        if (lunarium_data_uparrow::up_arrow_iconDataNumChannels == 4)
        {
            format = TextureFormat::RGBA;
        }
        pData = new unsigned char[lunarium_data_uparrow::up_arrow_iconDataSize];
        memcpy(pData, lunarium_data_uparrow::up_arrow_iconData, lunarium_data_uparrow::up_arrow_iconDataSize);
        mUpArrowIcon = Texture::Create(pData, lunarium_data_uparrow::up_arrow_iconDataWidth, lunarium_data_uparrow::up_arrow_iconDataHeight, format);
        //////////////////////////////////////////

        /////////////////////////////////////////
        // play arrow
        format = TextureFormat::RGB;
        if (lunarium_data_play::Play_ArrowDataNumChannels == 4)
        {
            format = TextureFormat::RGBA;
        }
        pData = new unsigned char[lunarium_data_play::Play_ArrowDataSize];
        memcpy(pData, lunarium_data_play::Play_ArrowData, lunarium_data_play::Play_ArrowDataSize);
        mPlayArrow = Texture::Create(pData, lunarium_data_play::Play_ArrowDataWidth, lunarium_data_play::Play_ArrowDataHeight, format);
        //////////////////////////////////////////

        /////////////////////////////////////////
        // pause
        format = TextureFormat::RGB;
        if (lunarium_data_pause::PauseDataNumChannels == 4)
        {
            format = TextureFormat::RGBA;
        }
        pData = new unsigned char[lunarium_data_pause::PauseDataSize];
        memcpy(pData, lunarium_data_pause::PauseData, lunarium_data_pause::PauseDataSize);
        mPause = Texture::Create(pData, lunarium_data_pause::PauseDataWidth, lunarium_data_pause::PauseDataHeight, format);
        //////////////////////////////////////////

        /////////////////////////////////////////
        // stop
        format = TextureFormat::RGB;
        if (lunarium_data_stop::StopDataNumChannels == 4)
        {
            format = TextureFormat::RGBA;
        }
        pData = new unsigned char[lunarium_data_stop::StopDataSize];
        memcpy(pData, lunarium_data_stop::StopData, lunarium_data_stop::StopDataSize);
        mStop = Texture::Create(pData, lunarium_data_stop::StopDataWidth, lunarium_data_stop::StopDataHeight, format);
        //////////////////////////////////////////

    }

    void DataManager::Shutdown()
    {
        Texture::Destroy(&mUpArrowIcon);
        Texture::Destroy(&mNewFolderIcon);
        Texture::Destroy(&mFolderIcon);
    }

    bool DataManager::GenerateFontFileAt(Font font, const char * filename)
	{
		std::ofstream ofs(filename, std::ios_base::binary);

		if (!ofs.is_open())
		{
			Logger::Warn(LogCategory::GRAPHICS, "Could not generate the default font file at: %s", filename);
			return false;
		}


		switch (font)
		{
			case Font::F_OPEN:
				ofs.write((const char*)OpenFontData, OpenDataSize);
			break;

			
			case Font::F_ROBOTO:
				ofs.write((const char*)RobotoFontData, RobotoDataSize);
			break;
		}

		ofs.close();
		ofs.clear();

		return true;
	}
}