/****************************************************************************
 Copyright (c) 2013      Zynga Inc.
 Copyright (c) 2013-2016 Chukong Technologies Inc.
 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
 Copyright (c) 2021 Bytedance Inc.

 https://axmolengine.github.io/

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

#include "2d/CCFontCharMap.h"
#include "2d/CCFontAtlas.h"
#include "platform/CCFileUtils.h"
#include "base/CCDirector.h"
#include "renderer/CCTextureCache.h"

NS_AX_BEGIN

FontCharMap* FontCharMap::create(std::string_view plistFile)
{
    std::string pathStr    = FileUtils::getInstance()->fullPathForFilename(plistFile);
    std::string relPathStr = pathStr.substr(0, pathStr.find_last_of('/')) + "/";

    ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(pathStr);

    AXASSERT(dict["version"].asInt() == 1, "Unsupported version. Upgrade cocos2d version");

    std::string textureFilename = relPathStr + dict["textureFilename"].asString();

    unsigned int width     = dict["itemWidth"].asInt();
    unsigned int height    = dict["itemHeight"].asInt();
    unsigned int startChar = dict["firstChar"].asInt();

    Texture2D* tempTexture = Director::getInstance()->getTextureCache()->addImage(textureFilename);
    if (!tempTexture)
    {
        return nullptr;
    }

    FontCharMap* tempFont = new FontCharMap(tempTexture, width, height, startChar);

    if (!tempFont)
    {
        return nullptr;
    }
    tempFont->autorelease();
    return tempFont;
}

FontCharMap* FontCharMap::create(std::string_view charMapFile, int itemWidth, int itemHeight, int startCharMap)
{
    Texture2D* tempTexture = Director::getInstance()->getTextureCache()->addImage(charMapFile);

    if (!tempTexture)
    {
        return nullptr;
    }

    FontCharMap* tempFont = new FontCharMap(tempTexture, itemWidth, itemHeight, startCharMap);

    if (!tempFont)
    {
        return nullptr;
    }
    tempFont->autorelease();
    return tempFont;
}

FontCharMap* FontCharMap::create(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
{
    FontCharMap* tempFont = new FontCharMap(texture, itemWidth, itemHeight, startCharMap);

    if (!tempFont)
    {
        return nullptr;
    }
    tempFont->autorelease();
    return tempFont;
}

FontCharMap::~FontCharMap() {}

int* FontCharMap::getHorizontalKerningForTextUTF32(const std::u32string& /*text*/, int& /*outNumLetters*/) const
{
    return nullptr;
}

FontAtlas* FontCharMap::newFontAtlas()
{
    FontAtlas* tempAtlas = new FontAtlas(this);

    Vec2 s             = _texture->getContentSizeInPixels();
    int itemsPerColumn = (int)(s.height / _itemHeight);
    int itemsPerRow    = (int)(s.width / _itemWidth);

    tempAtlas->setLineHeight((float)_itemHeight);

    auto contentScaleFactor = AX_CONTENT_SCALE_FACTOR();

    FontLetterDefinition tempDefinition;
    tempDefinition.textureID       = 0;
    tempDefinition.offsetX         = 0.0f;
    tempDefinition.offsetY         = 0.0f;
    tempDefinition.validDefinition = true;
    tempDefinition.width           = _itemWidth / contentScaleFactor;
    tempDefinition.height          = _itemHeight / contentScaleFactor;
    tempDefinition.xAdvance        = _itemWidth;
    tempDefinition.rotated         = false;

    int charId = _mapStartChar;
    for (int row = 0; row < itemsPerColumn; ++row)
    {
        for (int col = 0; col < itemsPerRow; ++col)
        {
            tempDefinition.U = _itemWidth * col / contentScaleFactor;
            tempDefinition.V = _itemHeight * row / contentScaleFactor;

            tempAtlas->addLetterDefinition(charId, tempDefinition);
            charId++;
        }
    }

    tempAtlas->setTexture(0, _texture);

    return tempAtlas;
}

NS_AX_END