#!perl
use Cassandane::Tiny;

sub test_calendar_set_defaultalerts
    :min_version_3_1 :needs_component_jmap
{
    my ($self) = @_;

    my $jmap = $self->{jmap};
    my $CalDAV = $self->{caldav};

    my $defaultAlertsWithTime = {
        alert1 => {
            '@type' => 'Alert',
            trigger => {
                '@type' => 'OffsetTrigger',
                relativeTo => 'start',
                offset => '-PT1H',
            },
            action => 'email',
        },
        alert2 => {
            '@type' => 'Alert',
            trigger => {
                '@type' => 'OffsetTrigger',
                relativeTo => 'start',
                offset => 'PT0S',
            },
            action => 'display',
        },
    };

    my $defaultAlertsWithoutTime = {
        alert3 => {
            '@type' => 'Alert',
            trigger => {
                '@type' => 'OffsetTrigger',
                relativeTo => 'start',
                offset => 'PT0S',
            },
            action => 'display',
        },
    };

    my $res = $jmap->CallMethods([
        ['Calendar/set', {
            create => {
                1 => {
                    name => 'test',
                    color => 'blue',
                    defaultAlertsWithTime => $defaultAlertsWithTime,
                    defaultAlertsWithoutTime => $defaultAlertsWithoutTime,
                }
            }
        }, 'R1'],
        ['Calendar/get', {
            ids => ['#1'],
            properties => ['defaultAlertsWithTime', 'defaultAlertsWithoutTime'],
        }, 'R2']
    ]);
    my $calendarId = $res->[0][1]{created}{1}{id};
    $self->assert_not_null($calendarId);
    $self->assert_deep_equals($defaultAlertsWithTime,
        $res->[1][1]{list}[0]{defaultAlertsWithTime});
    $self->assert_deep_equals($defaultAlertsWithoutTime,
        $res->[1][1]{list}[0]{defaultAlertsWithoutTime});

    my $alert4 = {
        '@type' => 'Alert',
        trigger => {
            '@type' => 'OffsetTrigger',
            relativeTo => 'start',
            offset => '-PT30M',
        },
        action => 'display',
    };

    $res = $jmap->CallMethods([
        ['Calendar/set', {
            update => {
                $calendarId => {
                    'defaultAlertsWithTime/alert1' => undef,
                    'defaultAlertsWithTime/alert4' => $alert4,
                    'defaultAlertsWithoutTime/alert3/trigger/offset' => '-PT5M',
                }
            }
        }, 'R1'],
        ['Calendar/get', {
            ids => [$calendarId],
            properties => ['defaultAlertsWithTime', 'defaultAlertsWithoutTime'],
        }, 'R2']
                              ]);
    $self->assert(exists $res->[0][1]{updated}{$calendarId});
    $self->assert_null($res->[1][1]{list}[0]{defaultAlertsWithTime}{alert1});
    $self->assert_deep_equals($alert4,
        $res->[1][1]{list}[0]{defaultAlertsWithTime}{alert4});
    $self->assert_equals('-PT5M',
                         $res->[1][1]{list}[0]{defaultAlertsWithoutTime}{alert3}{trigger}{offset});

    $res = $jmap->CallMethods([
        ['Calendar/set', {
            update => {
                $calendarId => {
                    defaultAlertsWithTime => undef,
                    defaultAlertsWithoutTime => undef,
                }
            }
        }, 'R1'],
        ['Calendar/get', {
            ids => [$calendarId],
            properties => ['defaultAlertsWithTime', 'defaultAlertsWithoutTime'],
        }, 'R2']
    ]);
    $self->assert(exists $res->[0][1]{updated}{$calendarId});
    $self->assert_null($res->[1][1]{list}[0]{defaultAlertsWithTime});
    $self->assert_null($res->[1][1]{list}[0]{defaultAlertsWithoutTime});
}
