// Copyright (c) 2016-2018 Kiwano - Nomango
//
// 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 <kiwano/utils/Logger.h>
#include <kiwano/render/GifImage.h>
#include <kiwano/render/Renderer.h>
#include <kiwano/render/TextureCache.h>
#include <functional>  // std::hash

namespace kiwano
{

GifImagePtr GifImage::Preload(const String& file_path)
{
    size_t hash_code = std::hash<String>{}(file_path);
    if (GifImagePtr ptr = TextureCache::GetInstance().GetGifImage(hash_code))
    {
        return ptr;
    }
    GifImagePtr ptr = MakePtr<GifImage>();
    if (ptr && ptr->Load(file_path))
    {
        TextureCache::GetInstance().AddGifImage(hash_code, ptr);
    }
    return ptr;
}

GifImagePtr GifImage::Preload(const Resource& res)
{
    size_t hash_code = res.GetId();
    if (GifImagePtr ptr = TextureCache::GetInstance().GetGifImage(hash_code))
    {
        return ptr;
    }
    GifImagePtr ptr = MakePtr<GifImage>();
    if (ptr && ptr->Load(res))
    {
        TextureCache::GetInstance().AddGifImage(hash_code, ptr);
    }
    return ptr;
}

GifImage::GifImage(const String& file_path)
    : GifImage()
{
    Load(file_path);
}

GifImage::GifImage(const Resource& res)
    : GifImage()
{
    Load(res);
}

GifImage::GifImage()
    : frames_count_(0)
{
}

bool GifImage::Load(const String& file_path)
{
    Renderer::GetInstance().CreateGifImage(*this, file_path);

    if (IsValid())
    {
        if (GetGlobalMetadata())
            return true;

        // Clear data
        ResetNative();
        Fail("GifImage::Load failed");
    }
    return false;
}

bool GifImage::Load(const Resource& res)
{
    Renderer::GetInstance().CreateGifImage(*this, res.GetData());

    if (IsValid())
    {
        if (GetGlobalMetadata())
            return true;

        // Clear data
        ResetNative();
        Fail("GifImage::Load failed");
    }
    return false;
}

GifImage::Frame GifImage::GetFrame(uint32_t index)
{
    Frame frame;
    Renderer::GetInstance().CreateGifImageFrame(frame, *this, index);
    return frame;
}

}  // namespace kiwano

#if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
#include <kiwano/render/DirectX/helper.h>

namespace kiwano
{

bool GifImage::GetGlobalMetadata()
{
    ComPtr<IWICBitmapDecoder> decoder = ComPolicy::Get<IWICBitmapDecoder>(this);

    HRESULT hr = decoder ? S_OK : E_FAIL;

    if (SUCCEEDED(hr))
    {
        hr = decoder->GetFrameCount(&frames_count_);
    }

    if (SUCCEEDED(hr))
    {
        ComPtr<IWICMetadataQueryReader> metadata_reader;
        hr = decoder->GetMetadataQueryReader(&metadata_reader);

        if (SUCCEEDED(hr))
        {
            uint32_t width  = 0;
            uint32_t height = 0;

            PROPVARIANT prop_val;
            ::PropVariantInit(&prop_val);

            // 获取全局 frame 大小
            if (SUCCEEDED(hr))
            {
                // 获取宽度
                hr = metadata_reader->GetMetadataByName(L"/logscrdesc/Width", &prop_val);

                if (SUCCEEDED(hr))
                {
                    hr = (prop_val.vt == VT_UI2 ? S_OK : E_FAIL);
                    if (SUCCEEDED(hr))
                    {
                        width = prop_val.uiVal;
                    }
                    ::PropVariantClear(&prop_val);
                }
            }

            if (SUCCEEDED(hr))
            {
                // 获取高度
                hr = metadata_reader->GetMetadataByName(L"/logscrdesc/Height", &prop_val);

                if (SUCCEEDED(hr))
                {
                    hr = (prop_val.vt == VT_UI2 ? S_OK : E_FAIL);
                    if (SUCCEEDED(hr))
                    {
                        height = prop_val.uiVal;
                    }
                    ::PropVariantClear(&prop_val);
                }
            }

            if (SUCCEEDED(hr))
            {
                // 获得像素纵横比
                hr = metadata_reader->GetMetadataByName(L"/logscrdesc/PixelAspectRatio", &prop_val);

                if (SUCCEEDED(hr))
                {
                    hr = (prop_val.vt == VT_UI1 ? S_OK : E_FAIL);
                    if (SUCCEEDED(hr))
                    {
                        if (prop_val.bVal != 0)
                        {
                            // 需要计算比率
                            // 最高像素 1：4，最宽像素 4：1，增量为 1/64
                            float pixel_asp_ratio = (prop_val.bVal + 15.f) / 64.f;

                            // 根据像素长宽比计算像素中的图像宽度和高度，只缩小图像
                            if (pixel_asp_ratio > 1.f)
                            {
                                size_in_pixels_ = { width, static_cast<uint32_t>(height / pixel_asp_ratio) };
                            }
                            else
                            {
                                size_in_pixels_ = { static_cast<uint32_t>(width * pixel_asp_ratio), height };
                            }
                        }
                        else
                        {
                            // 值为 0, 所以像素比为 1
                            size_in_pixels_ = { width, height };
                        }
                    }
                    ::PropVariantClear(&prop_val);
                }
            }
            ::PropVariantClear(&prop_val);
        }
    }
    return SUCCEEDED(hr);
}

#else

bool GifImage::GetGlobalMetadata()
{
    return false;  // not supported
}

#endif

}  // namespace kiwano
