/*
PolyGun

Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>

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.
*/

#ifndef POLYGUN_RENDERER_SURFACE_HPP
#define POLYGUN_RENDERER_SURFACE_HPP

#include <glm/glm.hpp>
#include <string>

#include "common/glm_ext.hpp"

namespace polygun::renderer {
	class Surface final {
	public:
		Surface(unsigned width, unsigned height, const glm::vec4ub& color = glm::vec4ub(0, 0, 0, 255));
		Surface(unsigned width, unsigned height, const uint8_t* pixels);
		Surface();
		~Surface();

		void fill(const glm::vec4ub& color);
		void blit_surface(int x, int y, const Surface& other);
		void blit_surface(int x, int y, const Surface& other, unsigned dw, unsigned dh);

		bool load_from_file(const std::string& path);

		const uint8_t* get_pixels() const { return m_pixels; }
		unsigned get_width() const { return m_width; }
		unsigned get_height() const { return m_height; }

	private:
		unsigned m_width, m_height;
		uint8_t* m_pixels;

	private:
		inline void put_pixel(int x, int y, const glm::vec4ub& color) {
			if(x<0 || static_cast<unsigned>(x)>=m_width || y<0 || static_cast<unsigned>(y)>=m_height)
				return;
			const int offset = (y*m_width+x)*4;
			const float bg_alpha_factor = static_cast<float>(m_pixels[offset+3])/255.0f;
			const float fg_alpha_factor = static_cast<float>(color.a)/255.0f;
			uint16_t temp_r = (color.r*fg_alpha_factor)+(m_pixels[offset]*bg_alpha_factor);
			uint16_t temp_g = (color.g*fg_alpha_factor)+(m_pixels[offset+1]*bg_alpha_factor);
			uint16_t temp_b = (color.b*fg_alpha_factor)+(m_pixels[offset+2]*bg_alpha_factor);
			uint16_t temp_a = color.a+m_pixels[offset+3];
			temp_r = temp_r>255?255:temp_r;
			temp_g = temp_g>255?255:temp_g;
			temp_b = temp_b>255?255:temp_b;
			temp_a = temp_a>255?255:temp_a;
			m_pixels[offset] = static_cast<uint8_t>(temp_r);
			m_pixels[offset+1] = static_cast<uint8_t>(temp_g);
			m_pixels[offset+2] = static_cast<uint8_t>(temp_b);
			m_pixels[offset+3] = static_cast<uint8_t>(temp_a);
		}
		bool load_png(FILE* file);
	};
}

#endif // POLYGUN_RENDERER_SURFACE_HPP
