﻿#include <array>
#include <cstddef>

#include <DxLib.h>

#include "Shooting/ImageManager.h"

namespace dxgame::shooting
{
    namespace
    {
        using ImageId = ImageManager::Id;

        struct ImageEntry
        {
            ImageId id;
            const wchar_t* path;
        };

        constexpr std::array<ImageEntry, static_cast<std::size_t>(ImageId::Count)> ImageTable =
        {
            ImageEntry { ImageId::Player, L"Assets/Images/Player.png" },
            ImageEntry { ImageId::PlayerShot, L"Assets/Images/Bullet.png" },
            ImageEntry { ImageId::Enemy, L"Assets/Images/Enemy.png" },
            ImageEntry { ImageId::EnemyShot, L"Assets/Images/EnemyShot.png" }
        };

        std::size_t ToIndex(ImageId id)
        {
            return static_cast<std::size_t>(id);
        }
    }

    ImageManager::ImageManager()
    {
        handles.fill(-1);
    }

    ImageManager::~ImageManager()
    {
        for (int handle : handles)
        {
            if (handle != -1)
                DeleteGraph(handle);
        }
    }

    ImageManager::LoadResult ImageManager::LoadAll()
    {
        for (const ImageEntry& entry : ImageTable)
        {
            std::size_t index = ToIndex(entry.id);
            handles[index] = LoadGraph(entry.path);

            if (handles[index] == -1)
                return { false, entry.path };
        }

        return { true, {} };
    }

    void ImageManager::Draw(Id id, const Rect& rect) const
    {
        int handle = handles[ToIndex(id)];
        DrawExtendGraph(
            static_cast<int>(rect.x),
            static_cast<int>(rect.y),
            static_cast<int>(rect.x + rect.width),
            static_cast<int>(rect.y + rect.height),
            handle,
            TRUE);
    }
}
