#!perl
use Cassandane::Tiny;

sub test_calendarevent_copy_state
{
    my ($self) = @_;
    my $jmap = $self->{jmap};
    my $caldav = $self->{caldav};
    my $admintalk = $self->{adminstore}->get_client();
    my $service = $self->{instance}->get_service("http");

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

    my $othercaldav = Net::CalDAVTalk->new(
        user => "other",
        password => 'pass',
        host => $service->host(),
        port => $service->port(),
        scheme => 'http',
        url => '/',
        expandurl => 1,
    );

    $admintalk->setacl('user.other', admin => 'lrswipkxtecdan');
    $admintalk->setacl('user.other', other => 'lrswipkxtecdn');

    xlog $self, "create source calendar";
    my $srcCalendarId = $caldav->NewCalendar({name => 'Source Calendar'});
    $self->assert_not_null($srcCalendarId);

    xlog $self, "create destination calendar";
    my $dstCalendarId = $othercaldav->NewCalendar({name => 'Destination Calendar'});
    $self->assert_not_null($dstCalendarId);

    xlog $self, "share calendar";
    $admintalk->setacl("user.other.#calendars.$dstCalendarId", "cassandane" => 'lrswipkxtecdn') or die;

    my $event =  {
        calendarIds => {
            $srcCalendarId => JSON::true,
        },
        "uid" => "58ADE31-custom-UID",
        "title"=> "foo",
        "start"=> "2015-11-07T09:00:00",
        "duration"=> "PT5M",
        "sequence"=> 42,
        "timeZone"=> "Etc/UTC",
        "showWithoutTime"=> JSON::false,
        "locale" => "en",
        "status" => "tentative",
        "description"=> "",
        "freeBusyStatus"=> "busy",
        "participants" => undef,
        "alerts"=> undef,
    };

    xlog $self, "create event";
    my $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            create => {"1" => $event}
         }, "R1"],
        ['CalendarEvent/get', {
            accountId => 'other',
            ids => ['foo'],  # Just fetching current state for 'other'
        }, 'R2'],
    ]);
    $self->assert_not_null($res->[0][1]{created});
    my $eventId = $res->[0][1]{created}{"1"}{id};
    my $fromState = $res->[0][1]->{newState};
    $self->assert_not_null($fromState);
    my $state = $res->[1][1]->{state};
    $self->assert_not_null($state);

    xlog $self, "move event";
    $res = $jmap->CallMethods([
        ['CalendarEvent/copy', {
            fromAccountId => 'cassandane',
            accountId => 'other',
            ifFromInState => $fromState,
            ifInState => $state,
            create => {
                1 => {
                    id => $eventId,
                    calendarIds => {
                        $dstCalendarId => JSON::true,
                    },
                },
            },
            onSuccessDestroyOriginal => JSON::true,
            destroyFromIfInState => $fromState,
         }, "R1"],
        ['CalendarEvent/get', {
            accountId => 'other',
            ids => ['#1'],
            properties => ['title'],
        }, 'R2'],
    ]);
    $self->assert_not_null($res->[0][1]{created});
    my $oldState = $res->[0][1]->{oldState};
    $self->assert_str_equals($oldState, $state);
    my $newState = $res->[0][1]->{newState};
    $self->assert_not_null($newState);
    $self->assert_str_equals('CalendarEvent/set', $res->[1][0]);
    $self->assert_str_equals($eventId, $res->[1][1]{destroyed}[0]);
    $self->assert_str_equals('foo', $res->[2][1]{list}[0]{title});

    # Is the blobId downloadable?
    my $blob = $jmap->Download({ accept => 'text/calendar' },
                               'other',
                               $res->[0][1]{created}{"1"}{blobId});
    $self->assert_str_equals('text/calendar; component=VEVENT',
                             $blob->{headers}->{'content-type'});
    $self->assert_num_not_equals(0, $blob->{headers}->{'content-length'});
    $self->assert_matches(qr/\r\nSUMMARY;LANGUAGE=en:foo\r\n/, $blob->{content});
}
