#include "ImageBuffer.h"

#include "Texture2D.h"
#include "Core/Thread/ScopeLock.h"

ImageBuffer* Singleton<ImageBuffer>::instance_ = nullptr;

bool ImageBuffer::Initialize() {
    return true;
}

void ImageBuffer::Uninitialize() {

}

void ImageBuffer::PushImage(ImageBuffer::IBType type, std::vector<unsigned char> data,
    uint32 width, uint32 height, uint32 comp) {
	switch (type) {
	case ImageBuffer::IBType::Background:
        PushBackgroundImage(std::move(data), width, height, comp);
		break;
	default:
		break;
	}
}

void ImageBuffer::Update(IBType type, Texture2D* texture) {
    switch (type) {
    case ImageBuffer::IBType::Background:
        UpdateBackground(texture);
        break;
    default:
        break;
    }
}

void ImageBuffer::PushBackgroundImage(std::vector<uint8> data, uint32 width, uint32 height, uint32 comp) {
    ScopeLock<CriticalSection> lock(&cs_);
 
    background_.data = std::move(data);
    background_.width = width;
    background_.height = height;
    background_.comp = comp;
}

void ImageBuffer::UpdateBackground(Texture2D* texture) {
    if (background_.data.empty()) {
        return;
    }

    ScopeLock<CriticalSection> lock(&cs_);
    texture->Update(background_.width, background_.height, background_.comp, background_.data.data());
}
