/*
 * FixBrowser v0.4 - https://www.fixbrowser.org/
 * Copyright (c) 2018-2025 Martin Dvorak <jezek2@advel.cz>
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from
 * the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose, 
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
 *    appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 */

use "image/shaders";
use "classes";

import "io/gzip";
import "image/image";
import "image/rect";
import "image/font";
import "gui/view";
import "gui/event";
import "gui/subview";
import "gui/subview/button";
import "gui/pure/button";

var @instance: AboutWindow;

class AboutWindow: Window
{
	var @background: BackgroundView;
	var @button: PureButton;

	static function show()
	{
		if (!instance) {
			instance = create();
			instance.set_visible(true);
		}
		else {
			instance.focus();
		}
		instance.button.focus();
	}

	static function hide()
	{
		if (instance) {
			instance.destroy();
		}
	}

	constructor @create()
	{
		super::create("About", 576, 324, WIN_CENTER);

		background = BackgroundView::create();
		add(background, Rect::create(0, 0, 576, 324));

		var w = 80;
		var h = 25;
		button = PureButton::create("OK");
		button.set_rect((576-w)/2, 280, w, h);
		button.set_click_action(AboutWindow::button_action#1, this);
		background.add(button);
	}

	function @button_action()
	{
		destroy();
	}

	override function handle_destroy()
	{
		instance = null;
	}
}

class @BackgroundView: SubView
{
	var @img: Image;
	var @font: Font;

	constructor create()
	{
		set_rect(0, 0, 576, 324);
		img = Image::load(resource_get("hpunbg1.png"));
		font = Font::load(gzip_uncompress(resource_get("DejaVuSans.ttf.gz")));
	}

	override function handle_paint(p: Painter)
	{
		var tr = Transform::create();
		p.fill_rect(0, 0, img.get_width(), img.get_height(), Shader {
			replace(sample_nearest(img, tr));
		});
		var title = "FixBrowser";
		var size = 39.8;
		var width = 0.0;
		var shape = Shape::create();
		for (var i=0; i<length(title); i++) {
			font.append_char_shape(shape, title[i], round(width), 0.0, size, size);
			width = width + font.get_char_advance(title[i])*size + 6.0;
		}
		width = width - 6.0;
		p.push();
		//p.set_subpixel_rendering(true);
		p.set_blend_gamma(1.3);
		p.translate(iround((get_width() - width)*0.5), 94);
		p.fill_shape(shape.get_outline(2.0, CAP_ROUND, JOIN_ROUND), 0xFFFFFFFF);
		p.fill_shape(shape, 0xFFFFFFFF);
		p.pop();

		var version = "v0.4";
		size = 12.5;
		var width_ver = font.get_string_advance(version)*size;
		p.push();
		//p.set_subpixel_rendering(true);
		p.set_blend_gamma(1.3);
		p.fill_shape(font.get_string_shape(version, iround((get_width() + width) * 0.5 - width_ver), 116, size, size), 0xFFFFFFFF);

		width = 300;
		shape = Shape::round_rect(iround((get_width() - width) * 0.5), 190, width, 70, 5, 5);
		p.fill_shape(shape, 0x80000000);

		p.translate(-0.5, -0.5);
		shape = Shape::round_rect(iround((get_width() - width) * 0.5), 190, width+1, 70+1, 5, 5);
		p.fill_shape(shape.get_outline(1.0), 0x40202020);
		p.translate(0.5, 0.5);

		var text1 = "Copyright \A9 2018-2025 Martin Dvorak";
		var text2 = "www.fixbrowser.org";
		var size2 = 12.8;
		var width1 = font.get_string_advance(text1) * size2;
		var width2 = font.get_string_advance(text2) * size2;
		var shape1 = font.get_string_shape(text1, iround((get_width() - width1) * 0.5), 220, size2, size2);
		var shape2 = font.get_string_shape(text2, iround((get_width() - width2) * 0.5), 243, size2, size2);
		p.fill_shape(shape1, 0xFFFFFFFF);
		p.fill_shape(shape2, 0xFFFFFFFF);
		p.pop();

		draw_children(p);
	}
}
