#!perl
use Cassandane::Tiny;

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

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

    # For JMAP calendars, we refactored CalDAV default alarm property
    # handling from a regular dead DAV property to a structured value.
    # This test asserts that CalDAV clients won't notice the difference.

    my $rawAlarmDateTime = <<EOF;
BEGIN:VALARM
TRIGGER:-PT5M
ACTION:DISPLAY
DESCRIPTION:alarmTime1
END:VALARM
BEGIN:VALARM
TRIGGER:PT0M
ACTION:DISPLAY
DESCRIPTION:alarmTime2
END:VALARM
EOF

    my $rawAlarmDate = <<EOF;
BEGIN:VALARM
TRIGGER:PT0S
ACTION:DISPLAY
DESCRIPTION:alarmDate1
END:VALARM
EOF

    xlog "Set alarms";
    my $proppatchXml = <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<D:propertyupdate xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
  <D:set>
    <D:prop>
<C:default-alarm-vevent-datetime>
$rawAlarmDateTime
</C:default-alarm-vevent-datetime>
    </D:prop>
  </D:set>
  <D:set>
    <D:prop>
<C:default-alarm-vevent-date>
$rawAlarmDate
</C:default-alarm-vevent-date>
    </D:prop>
  </D:set>
</D:propertyupdate>
EOF
    $CalDAV->Request('PROPPATCH', "/dav/calendars/user/cassandane/Default",
        $proppatchXml, 'Content-Type' => 'text/xml');

    xlog "Get alarms";
    my $propfindXml = <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<D:propfind xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
  <D:prop>
     <C:default-alarm-vevent-datetime/>
     <C:default-alarm-vevent-date/>
  </D:prop>
</D:propfind>
EOF
    my $Response = $CalDAV->Request('PROPFIND', "/dav/calendars/user/cassandane/Default",
        $propfindXml, 'Content-Type' => 'text/xml');

    xlog "Assert alarm values";
    my $assert_propval = sub {
        my ($Response, $propname, $wantVal, $wantStatus) = @_;
        my $propStat = $Response->{'{DAV:}response'}[0]->{'{DAV:}propstat'}[0];
        my $prop = $propStat->{'{DAV:}prop'};
        $wantVal =~ s/^\s+|\s+$//g;
        my $got = $prop->{'{urn:ietf:params:xml:ns:caldav}'. $propname}->{content};
        $got =~ s/^\s+|\s+$//g;
        $self->assert_str_equals($wantVal, $got);
        my $status = $propStat->{'{DAV:}status'};
        $self->assert_str_equals($wantStatus, $status->{content});
    };
    $assert_propval->($Response, 'default-alarm-vevent-datetime',
                      $rawAlarmDateTime, 'HTTP/1.1 200 OK');
    $assert_propval->($Response, 'default-alarm-vevent-date',
                      $rawAlarmDate, 'HTTP/1.1 200 OK');

    xlog "Remove alarms";
    $proppatchXml = <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<D:propertyupdate xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
  <D:remove>
    <D:prop>
        <C:default-alarm-vevent-datetime/>
        <C:default-alarm-vevent-date/>
    </D:prop>
  </D:remove>
</D:propertyupdate>
EOF
    $CalDAV->Request('PROPPATCH', "/dav/calendars/user/cassandane/Default",
        $proppatchXml, 'Content-Type' => 'text/xml');

    xlog "Get alarms";
    $propfindXml = <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<D:propfind xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
  <D:prop>
     <C:default-alarm-vevent-datetime/>
     <C:default-alarm-vevent-date/>
  </D:prop>
</D:propfind>
EOF
    $Response = $CalDAV->Request('PROPFIND', "/dav/calendars/user/cassandane/Default",
        $propfindXml, 'Content-Type' => 'text/xml');

    xlog "Assert alarm values do not exist";
    $assert_propval->($Response, 'default-alarm-vevent-datetime',
                      '', 'HTTP/1.1 404 Not Found');
    $assert_propval->($Response, 'default-alarm-vevent-date',
                      '', 'HTTP/1.1 404 Not Found');
}
