#!perl
use Cassandane::Tiny;
use utf8;

sub test_card_get_localizations
    :min_version_3_9 :needs_component_jmap :needs_dependency_icalvcard
{
    my ($self) = @_;
    my $jmap = $self->{jmap};

    my $service = $self->{instance}->get_service("http");
    $ENV{DEBUGDAV} = 1;
    my $carddav = Net::CardDAVTalk->new(
        user => 'cassandane',
        password => 'pass',
        host => $service->host(),
        port => $service->port(),
        scheme => 'http',
        url => '/',
        expandurl => 1,
    );

    # Sample card from RFC 6350
    # Second N suffix removed due to vparse bug
    # PROP-IDs added so we can easily compare the results
    my $id = 'ae2640cc-234a-4dd9-95cc-3106258445b9';
    my $href = "Default/$id.vcf";
    my $card = <<EOF;
BEGIN:VCARD
VERSION:4.0
UID:$id
LANGUAGE:es
FN:Gabriel García Márquez
FN;LANGUAGE=jp:ガブリエル・ガルシア・マルケス
N;ALTID=n1:Márquez;Gabriel;García;;
N;ALTID=n1;LANGUAGE=jp:マルケス;ガブリエル;ガルシア;;
TITLE;ALTID=t1;LANGUAGE=fr:Écrivain
TITLE;ALTID=t1:Novelista
TITLE;ALTID=t1;LANGUAGE=en:Novelist
GRAMGENDER;LANGUAGE=en:masculine
GRAMGENDER;LANGUAGE=es:neuter
PRONOUNS;ALTID=k19;LANGUAGE=de:er
PRONOUNS;ALTID=k19;LANGUAGE=it:lui
PRONOUNS;ALTID=k19:él
PRONOUNS;ALTID=k19;LANGUAGE=fr:il
ADR;ALTID=addr1;LANGUAGE=en:;;;Tokyo;;;
ADR;ALTID=addr1;LANGUAGE=jp:;;;東京;;;
ADR;ALTID=addr1:;;;Tokio;;;
END:VCARD
EOF
 
    $card =~ s/\r?\n/\r\n/gs;
    $carddav->Request('PUT', $href, $card, 'Content-Type' => 'text/vcard');

    my $res = $jmap->CallMethods([
        ['ContactCard/get', {
        }, 'R1']
    ]);

    my $want_jscard = {
        '@type' => 'Card',
        version => '1.0',
        addressBookId => 'Default',
        'cyrusimap.org:href' => $carddav->fullpath() . $href,
        id => $id,
        uid => $id,
        kind => 'individual',
        language => 'es',    
        vCardProps => [
            [ 'version', {}, 'text', '4.0' ]
        ],
        name => {
            full => 'Gabriel García Márquez',
            components => [
                { 'kind' => 'surname', 'value' => 'Márquez' },
                { 'kind' => 'given', 'value' => 'Gabriel' },
                { 'kind' => 'given2', 'value' => 'García' },
            ],
        },
        titles => {
            t1 => {
                'name' => 'Novelista'
            }
        },
        speakToAs => {
            grammaticalGender => 'neuter',
            pronouns => {
                k19 => {
                    pronouns => 'él'
                }
            }
        },
        addresses => {
            addr1 => {
                components => [
                    { kind => 'locality', value =>'Tokio' }
                ]
            }
        },
        localizations => {
            en => {
                'titles/t1/name' => 'Novelist',
                'speakToAs/grammaticalGender' => 'masculine',
                'addresses/addr1/components/0/value' => 'Tokyo'
            },
            fr => {
                'titles/t1/name' => 'Écrivain',
                'speakToAs/pronouns/k19/pronouns' => 'il'
            },
            de => {
                'speakToAs/pronouns/k19/pronouns' => 'er'
            },
            it => {
                'speakToAs/pronouns/k19/pronouns' => 'lui'
            },
            jp => {
                'name/full' => 'ガブリエル・ガルシア・マルケス',
                'name/components/0/value' => 'マルケス',
                'name/components/1/value' => 'ガブリエル',
                'name/components/2/value' => 'ガルシア',
                'addresses/addr1/components/0/value' => '東京'
            }
        }
    };

    
    my $have_jscard = $res->[0][1]{list}[0];

    # Delete generated fields
    delete $have_jscard->{blobId};
    delete $have_jscard->{'cyrusimap.org:blobId'};
    delete $have_jscard->{'cyrusimap.org:size'};

    # Normalize and compare cards
    normalize_jscard($want_jscard);
    normalize_jscard($have_jscard);

    $self->assert_deep_equals($want_jscard, $have_jscard);
}
