package Catmandu::Fix::lido_actor;

use Catmandu::Sane;
use Moo;
use Catmandu::Fix::Has;
use Catmandu::Fix::LIDO::Term qw(emit_term);
use Catmandu::Fix::LIDO::ID qw(emit_base_id);
use Catmandu::Fix::LIDO::Nameset qw(emit_nameset);
use Catmandu::Fix::LIDO::Value qw(emit_base_value emit_simple_value);

use strict;

our $VERSION = '0.10';

with 'Catmandu::Fix::Base';

has path            => (fix_arg => 1);
has id              => (fix_arg => 1);
has name            => (fix_arg => 1);
has name_lang       => (fix_opt => 1);
has name_pref       => (fix_opt => 1);
has id_label        => (fix_opt => 1);
has id_source       => (fix_opt => 1);
has id_type         => (fix_opt => 1);
has nationality     => (fix_opt => 1); # Path
has birthdate       => (fix_opt => 1);
has deathdate       => (fix_opt => 1);
has role            => (fix_opt => 1);
has role_id         => (fix_opt => 1);
has role_id_type    => (fix_opt => 1);
has role_id_source  => (fix_opt => 1);
has qualifier       => (fix_opt => 1);

sub emit {
    my ($self, $fixer) = @_;
    my $perl = '';
    my $new_path = $fixer->split_path($self->path);

    my $paths = {};
    $paths->{'id'} = ['actor', 'actorID'];
    $paths->{'name'} = ['actor', 'nameActorSet', '$append'];
    $paths->{'nationality'} = ['actor', 'nationalityActor'];
    $paths->{'dates'} = ['actor', 'vitalDatesActor'];
    $paths->{'role'} = ['roleActor'];
    $paths->{'attribution'} = ['attributionQualifierActor'];

    push @$new_path, 'actorInRole';

    $perl .= $fixer->emit_create_path(
                sub {
                    my $r_root = shift;
                    my $r_code = '';

                    # actorID
                    # $fixer, $root, $path, $id, $source, $label, $type
                    if (defined($self->id)) {
                        $r_code .= emit_base_id($fixer, $r_root, join('.', @{$paths->{'id'}}), $self->id, $self->id_source, $self->id_label, $self->id_type);

                    # nameActorSet
                    # $fixer, $root, $path, $appellation_value, $appellation_value_lang, $appellation_value_type,
                    # $appellation_value_pref, $source_appellation, $source_appellation_lang
                    if (defined($self->name)) {
                        $r_code .= emit_nameset($fixer, $r_root, join('.', @{$paths->{'name'}}), $self->name, $self->name_lang, undef, $self->name_pref);

                    # nationalityActor
                    # $fixer, $root, $path, $term, $conceptid, $lang, $pref, $source, $type
                    if (defined($self->nationality)) {
                        $r_code .= emit_term($fixer, $r_root, join('.', @{$paths->{'nationality'}}), $self->nationality);

                    # vitalDatesActor
                    $r_code .= $fixer->emit_create_path(
                        sub {
                            my $d_root = shift;
                            my $d_code = '';

                            # earliestDate
                            if (defined($self->birthdate)) {
                                $d_code .= emit_simple_value($fixer, $d_root, 'earliestDate', $self->birthdate);

                            # latestDate
                            if (defined($self->deathdate)) {
                                $d_code .= emit_simple_value($fixer, $d_root, 'latestDate', $self->deathdate);

                            return $d_code;

                    # roleActor
                    if (defined($self->role)) {
                        $r_code .= emit_term($fixer, $r_root, join('.', @{$paths->{'role'}}), $self->role, $self->role_id, undef, undef, $self->role_id_source, $self->role_id_type);

                    # attributionQualifierActor
                    # $fixer, $root, $path, $value, $lang, $pref, $label, $type
                    if (defined($self->qualifier)) {
                        $r_code .= emit_base_value($fixer, $r_root, join('.', @{$paths->{'attribution'}}), $self->qualifier);

                    return $r_code;

    return $perl;



=head1 NAME

Catmandu::Fix::lido_actor - create a LIDO actorInRole node at a specified path


        -name_lang:      name.lang,
        -name_pref:      name.pref,
        -id_label:       actorID.label,
        -id_source:      actorID.source,
        -id_type:        actorID.type,
        -nationality:    nationalityActor,
        -birthdate:      vitalDatesActor.earliestDate,
        -deathdate:      vitalDatesActor.latestDate,
        -role:           roleActor.term,
        -role_id:        roleActor.conceptID,
        -role_id_type:   roleActor.conceptID.type,
        -role_id_source: roleActor.conceptID.source,
        -qualifier:      attributionQualifierActor


C<lido_actor()> will create an actorInRole node in the path specified by the C<path> parameter.


=head3 Required parameters

It requires the parameters C<path>, C<id> and C<name> to be present as paths.


=item C<path>

=item C<id>

=item C<name>


=head3 Optional parameters

The following parameters are optional, but must be paths:


=item C<nationality>

=item C<birthdate>

=item C<deathdate>

=item C<role>

=item C<role_id>

=item C<qualifier>


All other optional parameters are strings:


=item C<id_label>

=item C<id_source>

=item C<id_type> (Required if id is set.)

=item C<role_id_type>

=item C<role_id_source>



Multiple instances can be created by appending C<$append> to the path. This will create a new C<actorInRole> tag for every instance. While it is possible to create a single C<actorInRole> for multiple actors, this is not permitted by the LIDO standard.

=head1 EXAMPLE

=head2 Fix

        -id_label: 'priref',
        -id_type: 'local',
        -id_source: 'Adlib'
        -nationality: recordList.record.creator.nationality,
        -birthdate: recordList.record.creator.date_of_birth,
        -deathdate: recordList.record.creator.date_of_death,
        -role_id_type: 'global',
        -role_id_source: 'AAT',

=head2 Result

                                <lido:actorID lido:label="priref" lido:type="local" lido:source="Adlib">123</lido:actorID>
                                    <lido:appellationValue>Jonghe, Jan Baptiste De</lido:appellationValue>
                                <lido:conceptID lido:type="global" lido:source="AAT">123</lido:conceptID>

=head1 SEE ALSO

L<Catmandu::LIDO> and L<Catmandu>

=head1 AUTHORS


=item Pieter De Praetere, C<< pieter at >>




=item Pieter De Praetere, C<< pieter at >>

=item Matthias Vandermaesen, C<< matthias.vandermaesen at >>



The Perl software is copyright (c) 2016 by PACKED vzw and VKC vzw.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

=encoding utf8