#!perl
use Cassandane::Tiny;

#
# Needs to be updated once the mechanism for setting media gets sorted out
#

sub test_card_copy
    :min_version_3_9
    ($self)
{
    my $jmap = $self->{jmap};
    my $carddav = $self->{carddav};
    my $admintalk = $self->{adminstore}->get_client();
    my $service = $self->{instance}->get_service("http");

    xlog $self, "create shared accounts";
    $admintalk->create("user.other");
    $admintalk->create("user.other2");
    $admintalk->create("user.other3");

    my $other_user = $self->{instance}->create_user_without_setup('other');
    my $other2_user = $self->{instance}->create_user_without_setup('other2');
    my $other3_user = $self->{instance}->create_user_without_setup('other3');

    my $other2jmap = $other2_user->jmap;

    $other2jmap->DefaultUsing([
        'urn:ietf:params:jmap:core',
        'https://cyrusimap.org/ns/jmap/contacts',
        'urn:ietf:params:jmap:contacts',
    ]);

#    my $carddav = $self->default_user->carddav;

    my $othercarddav = $other_user->carddav;
    my $other2carddav = $other2_user->carddav;
    my $other3carddav = $other3_user->carddav;

    xlog $self, "share addressbooks";
    $admintalk->setacl("user.other.#addressbooks.Default",
                       "cassandane" => 'lrswipkxtecdn') or die;
    $admintalk->setacl("user.other2.#addressbooks.Default",
                       "cassandane" => 'lrswipkxtecdn') or die;
    $admintalk->setacl("user.other3.#addressbooks.Default",
                       "cassandane" => 'lrswipkxtecdn') or die;

    xlog $self, "create alternate addressbook";
    my $res = $other2jmap->CallMethods([
            ['AddressBook/set', { create => { "1" => {
                              name => "foo"
             }}}, "R1"]
    ]);

    my $abookid = $res->[0][1]{created}{"1"}{id};
    $self->assert_not_null($abookid);

    xlog $self, "share other2 addressbook";
    $admintalk->setacl("user.other2.#addressbooks.$abookid",
                       "cassandane" => 'lrswipkxtecdn') or die;

    # avatar
    xlog $self, "upload avatar";
    my $data = "some photo";
    $res = $jmap->Upload($data, "image/jpeg");
    my $blobid = $res->{blobId};

    my $card =  {
        '@type' => 'Card',
        version => '1.0',
        name => { full => "foo bar" },
        media => {
            avatar => {
                '@type' => 'Media',
                kind => 'photo',
                mediaType => 'image/jpeg',
                blobId => $blobid
            }
        }
    };

    xlog $self, "create card";
    $res = $jmap->CallMethods([['ContactCard/set',{
        create => {"1" => $card}},
    "R1"]]);
    $self->assert_not_null($res->[0][1]{created});
    my $cardId = $res->[0][1]{created}{"1"}{id};

    xlog $self, "copy card $cardId w/o changes";
    $res = $jmap->CallMethods([['ContactCard/copy', {
        fromAccountId => 'cassandane',
        accountId => 'other',
        create => {
            1 => {
                id => $cardId,
                addressBookIds => { Default => JSON::true },
            },
        },
    },
    "R1"]]);
    $self->assert_not_null($res->[0][1]{created});
    my $copiedCardId = $res->[0][1]{created}{"1"}{id};

    $res = $jmap->CallMethods([
        ['ContactCard/get', {
            accountId => 'other',
            ids => [$copiedCardId],
        }, 'R1'],
        ['ContactCard/get', {
            accountId => undef,
            ids => [$cardId],
        }, 'R2'],
    ]);
    $self->assert_str_equals('foo bar', $res->[0][1]{list}[0]{name}{full});
    my $blob = $jmap->Download({ accept => 'image/jpeg' },
                               'other', $res->[0][1]{list}[0]{media}{avatar}{blobId});
    $self->assert_str_equals('image/jpeg',
                             $blob->{headers}->{'content-type'});
    $self->assert_num_not_equals(0, $blob->{headers}->{'content-length'});
    $self->assert_equals($data, $blob->{content});

    $self->assert_str_equals('foo bar', $res->[1][1]{list}[0]{name}{full});
    $blob = $jmap->Download({ accept => 'image/jpeg' },
                            'cassandane', $res->[1][1]{list}[0]{media}{avatar}{blobId});
    $self->assert_str_equals('image/jpeg',
                             $blob->{headers}->{'content-type'});
    $self->assert_num_not_equals(0, $blob->{headers}->{'content-length'});
    $self->assert_equals($data, $blob->{content});

    xlog $self, "move card $cardId with changes";
    $res = $jmap->CallMethods([['ContactCard/copy', {
        fromAccountId => 'cassandane',
        accountId => 'other2',
        create => {
            1 => {
                id => $cardId,
                addressBookIds => { $abookid => JSON::true },
                'media/avatar' => JSON::null,
                nicknames => { n1 => { '@type' => 'Nickname', name => "xxxxx" } }
            },
        }
    },
    "R1"]]);
    $self->assert_not_null($res->[0][1]{created});
    $copiedCardId = $res->[0][1]{created}{"1"}{id};

    $res = $jmap->CallMethods([
        ['ContactCard/get', {
            accountId => 'other2',
            ids => [$copiedCardId],
        }, 'R1'],
        ['ContactCard/get', {
            accountId => undef,
            ids => [$cardId],
        }, 'R2'],
    ]);

    $self->assert_deep_equals({ $abookid => JSON::true }, $res->[0][1]{list}[0]{addressBookIds});

    $self->assert_str_equals('foo bar', $res->[0][1]{list}[0]{name}{full});
    $self->assert_str_equals('xxxxx', $res->[0][1]{list}[0]{nicknames}{n1}{name});
    $self->assert_null($res->[0][1]{list}[0]{media});

    my $other3Jmap = $other3_user->jmap;
    $other3Jmap->DefaultUsing([
        'urn:ietf:params:jmap:core',
        'https://cyrusimap.org/ns/jmap/contacts',
        'urn:ietf:params:jmap:contacts',
    ]);

    # avatar
    xlog $self, "upload avatar for other3";
    $data = "some other photo";
    $res = $other3Jmap->Upload($data, "image/jpeg");
    $blobid = $res->{blobId};

    $admintalk->setacl("user.other3.#jmap",
                       "cassandane" => 'lrswipkxtecdn') or die;

    xlog $self, "move card $cardId with different avatar";
    $res = $jmap->CallMethods([['ContactCard/copy', {
        fromAccountId => 'cassandane',
        accountId => 'other3',
        create => {
            1 => {
                id => $cardId,
                addressBookIds => { Default => JSON::true },
                media => {
                    avatar => {
                        '@type' => 'Media',
                        kind => 'photo',
                        mediaType => 'image/jpeg',
                        blobId => $blobid
                    }
                }
            },
        },
        onSuccessDestroyOriginal => JSON::true,
    },
    "R1"]]);
    $self->assert_not_null($res->[0][1]{created});
    $copiedCardId = $res->[0][1]{created}{"1"}{id};

    $res = $jmap->CallMethods([
        ['ContactCard/get', {
            accountId => 'other3',
            ids => [$copiedCardId],
        }, 'R1'],
        ['ContactCard/get', {
            accountId => undef,
            ids => [$cardId],
        }, 'R2'],
    ]);
    $self->assert_str_equals('foo bar', $res->[0][1]{list}[0]{name}{full});
    $blob = $jmap->Download({ accept => 'image/jpeg' },
                            'other3', $res->[0][1]{list}[0]{media}{avatar}{blobId});
    $self->assert_str_equals('image/jpeg',
                             $blob->{headers}->{'content-type'});
    $self->assert_num_not_equals(0, $blob->{headers}->{'content-length'});
    $self->assert_equals($data, $blob->{content});

    $self->assert_str_equals($cardId, $res->[1][1]{notFound}[0]);
}
