package HexGrid::PopUp;

use v5.30;

use Moo;
use Text::Wrap::OO;
use Data::Dumper;

use feature "signatures";
no warnings "experimental::signatures";

has width => (is => 'rw', default => 600);
has height => (is => 'rw', default => 750);
has origin_x => (is => 'rw', default => 0);
has origin_y => (is => 'rw', default => 100);
has corner_radius => (is => 'rw', default => 20);
has gap_start => (is => 'rw', default => 40);
has gap_width => (is => 'rw', default => 40);
has name => (is => 'rw');
has link => (is => 'rw', alias => 'href');
has description => (is => 'rw', alias => 'desc');
has 'font-size' => (is => 'rw', default => '2.5em');
has margin => (is => 'rw', default => 20);
has 'stroke-width' => (is => 'rw', default => 2);

sub render($this, $scaler)
{
	my %t = %$this;
	my $gap_end = $t{gap_start} + $t{gap_width};

	$scaler->{width} = $t{width};
	$scaler->{height} = $t{height};
	$scaler->{viewBox} = "0 0 $t{width} $t{height}";
	my $box_height = $t{height} - $t{origin_y};

	my $path_string = _M($t{gap_start},$box_height);
	$path_string .= _L($t{origin_x},$t{height});
	$path_string .= _L($gap_end,$box_height);
	$path_string .= _l($t{width} - $gap_end - $t{corner_radius}, 0);
	$path_string .= _a($t{corner_radius}, $t{corner_radius}, 0, 0, 0, $t{corner_radius}, -$t{corner_radius});
	$path_string .= _l(0, -($box_height - 2*$t{corner_radius}));
	$path_string .= _a($t{corner_radius}, $t{corner_radius}, 0, 0, 0, -$t{corner_radius}, -$t{corner_radius});
	$path_string .= _l(-($t{width} - 2*$t{corner_radius}),0);
	$path_string .= _a($t{corner_radius}, $t{corner_radius}, 0, 0, 0, -$t{corner_radius}, $t{corner_radius});
	$path_string .= _l(0, ($box_height - 2*$t{corner_radius}));
	$path_string .= _a($t{corner_radius}, $t{corner_radius}, 0, 0, 0, $t{corner_radius}, $t{corner_radius});
	$path_string .= " Z";
	my $path = $scaler->path(d => $path_string, stroke => 'black', fill => 'white', 'stroke-width' => $t{'stroke-width'});
	my $content_container = $scaler->g(transform => "translate($t{margin},$t{margin})");
	my $obj = $content_container->foreignObject(width => ($t{width} - 2*$t{margin}), height => $box_height - 2*$t{margin});
	basic_popup($obj, $t{name}, $t{description}, $t{link}, $t{'font-size'});
	return $scaler;
}


sub _M { " M " . join(",", @_); }
sub _L { " L " . join(",", @_); }
sub _l { " l " . join(",", @_); }
sub _a { " a $_[0],$_[1] $_[2] $_[3] $_[4] $_[5],$_[6]"; }


# Split this out
sub basic_popup($obj, $name, $description, $link, $font_size)
{
	my $div = $obj->tag('div', style => "font-size: $font_size;", xmlns => "http://www.w3.org/1999/xhtml");
	$div->tag('h3', style => 'margin-top: 0; margin-left: 0;')->tag('a', href => $link)->cdata($name);
	foreach (split /\n/, $description)
	{
		/^\s*$/
		? $div->tag('br')->cdata
		: $div->tag('span')->cdata($_);
	}
}

1;