# Album Plugin: media/html5player
# For info:     'album -plugin_info media/html5player'
# For usage:    'album -plugin_usage media/html5player'
# For license:  'album -media/html5player:show_license'
#
# Uses html video or audio player viewer (tag <video...> or <audio...>)
use strict;

my $LICENSE = << 'LICENSE';
Album's html5player plugin is copyright (c) 2018 Antoine Emerit under the GPL 3.0 licence 
LICENSE

my $DESCRIPTION = << 'DESCRIPTION';
Uses the <video...> or <audio...> HTML tags to include videos and audios
with an embedded player in albums.

  % album -media/html5player:show_license
DESCRIPTION

my $PATH;
sub start_plugin {
  (my $opt,my $plugin,$PATH) = @_;

  # Setup the options
  album::add_option(1, 'show_license', \&show_license, one_time=>1,
                    usage=>'Show the license for this plugin.');

  # Setup the hooks
  album::hook($opt,'is_movie', \&is_html5player_type);
  album::hook($opt,'is_image', \&is_html5player_type);
  album::hook($opt,'end_handle_file', \&embed_html5player);

  return {
    author => 'Antoine Emerit',
    href => 'http://MarginalHacks.com/',
    version => '1.0',
    description => $DESCRIPTION,
  };
}


sub is_html5player_type {
	my ($opt,$hook,$video) = @_;
	$video =~ /\.(mp4|mpg|mpeg|webm|mov|avi|aac|m4a|f4a|mp3|ogg|oga|wav)$/i ? 1 : undef;
}

sub embed_html5player {
	my ($opt,$data,$hook,$dir,$pic,$obj,$path) = @_;
	return 0 unless is_html5player_type($opt,$hook,$pic);

	album::get_size($opt,'medium',$obj);

	# Player parameters
	my $x = $obj->{medium}{x};
	my $y = $obj->{medium}{y};
	#my $snapshot = $obj->{medium}{file};
	my $video = $obj->{URL}{image_page}{image};
	$video =~ s/^['"](.+)['"]$/$1/;

	if ($video =~ /\.(mp4|mpg|mpeg|webm|mov|avi)/i) {
		$obj->{medium}{img_tag} = "<video src=\"$video\" width=\"$x\" height=\"$y\" controls />";
	} else {
		$obj->{medium}{img_tag} = "<audio src=\"$video\" controls />";
        }

	album::get_size($opt,'full',$obj);

	# Player parameters
	my $x = $obj->{full}{x};
	my $y = $obj->{full}{y};
	#my $snapshot = $obj->{full}{file};
	my $video = $obj->{URL}{image_page}{image};
	$video =~ s/^['"](.+)['"]$/$1/;

	if ($video =~ /\.(mp4|mpg|webm|mov|avi)/i) {
		$obj->{full}{img_tag} = "<video src=\"$video\" width=\"$x\" height=\"$y\" controls />";
	} else {
		$obj->{full}{img_tag} = "<audio src=\"$video\" controls />";
        }

	0;	# Don't skip
}


sub show_license {
  my ($opt) = @_;
  
  my $me = album::curr_plugin($opt);
  print "The following license applies ONLY to the ",
        "$me plugin, and should not be construed ",
        "to apply to album, or any other plugin.\n\n", $LICENSE;
  exit;
}

# Plugins always end with:
1;
