/******************************************************************************
*	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 <graphics/graphics.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_dir_icon.h"
#include "data_headers/up_arrow_icon.h"

#include <fstream>

namespace lunarium
{
    // Init asset pointers
    Image* DataManager::mFolderIcon = nullptr;
    Image* DataManager::mNewFolderIcon = nullptr;
    Image* DataManager::mUpArrowIcon = nullptr;

    void DataManager::Initialize()
    {
        /////////////////////////////////////////
        // Folder16
        ImageFormat format = ImageFormat::RGB;
        if (lunarium_data_folder16::folder16DataNumChannels == 4)
        {
            format = ImageFormat::RGBA;
        }
        unsigned char* pData = new unsigned char[lunarium_data_folder16::folder16DataSize];
        memcpy(pData, lunarium_data_folder16::folder16Data, lunarium_data_folder16::folder16DataSize);
        mFolderIcon = new Image(pData, lunarium_data_folder16::folder16DataWidth, lunarium_data_folder16::folder16DataHeight, format);
        Core::Graphics().RegisterImage(*mFolderIcon);
        mFolderIcon->FreeRawData();
        //////////////////////////////////////////

        /////////////////////////////////////////
        // new_dir_icon
        format = ImageFormat::RGB;
        if (lunarium_data_newdir::new_dir_iconDataNumChannels == 4)
        {
            format = ImageFormat::RGBA;
        }
        pData = new unsigned char[lunarium_data_newdir::new_dir_iconDataSize];
        memcpy(pData, lunarium_data_newdir::new_dir_iconData, lunarium_data_newdir::new_dir_iconDataSize);
        mNewFolderIcon = new Image(pData, lunarium_data_newdir::new_dir_iconDataWidth, lunarium_data_newdir::new_dir_iconDataHeight, format);
        Core::Graphics().RegisterImage(*mNewFolderIcon);
        mNewFolderIcon->FreeRawData();
        //////////////////////////////////////////

        /////////////////////////////////////////
        // up_arrow_icon
        format = ImageFormat::RGB;
        if (lunarium_data_uparrow::up_arrow_iconDataNumChannels == 4)
        {
            format = ImageFormat::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 = new Image(pData, lunarium_data_uparrow::up_arrow_iconDataWidth, lunarium_data_uparrow::up_arrow_iconDataHeight, format);
        Core::Graphics().RegisterImage(*mUpArrowIcon);
        mUpArrowIcon->FreeRawData();
        //////////////////////////////////////////

    }

    void DataManager::Shutdown()
    {
        delete mUpArrowIcon;
        delete mNewFolderIcon;
        delete 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;
	}
}