#include "pw/io/image_io.hpp"

#include <pw/core/debug.hpp>

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

#define STB_IMAGE_WRITE_IMPLEMENTATION
 #include "stb_image_write.h"

#include <future>

namespace pw {

struct image_io::impl
{

    image read_impl(const std::string& uri,uint32_t flags)
    {
        int x,y,n;
		auto data = stbi_load(uri.c_str(), &x, &y, &n, 4);

        if (data) {

			image r;

            r.create(sizei(x,y),image::pixel_layout::RGBA8,data);

			stbi_image_free(data);

            return r;
		} else {

			debug::e() << stbi_failure_reason();
		}

        return image();
    }

    bool write(const std::string& uri, uint32_t flags)
    {

        return false;
    }

};


image_io &image_io::get()
{
    static image_io instance;
    return instance;
}

image image_io::read(const std::string &uri, uint32_t flags)
{
    return _impl->read_impl(uri,flags);
}

bool image_io::write(const std::string &uri, uint32_t flags)
{
    return _impl->write(uri,flags);
}

//
//
//

image_io::image_io()
{
    _impl = make_unique<impl>();
}

image_io::~image_io()
{
}

}
