/*
 * Copyright (c) 2015 Ngewi Fet <ngewif@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gnucash.android.test.unit.model;

import org.gnucash.android.model.PeriodType;
import org.gnucash.android.model.Recurrence;
import org.gnucash.android.model.ScheduledAction;
import org.junit.Test;

import java.sql.Timestamp;
import java.util.Calendar;

import static org.assertj.core.api.Assertions.assertThat;
/**
 * Test scheduled actions
 */
public class ScheduledActionTest {

    @Test
    public void settingStartTime_shouldSetRecurrenceStart(){
        ScheduledAction scheduledAction = new ScheduledAction(ScheduledAction.ActionType.TRANSACTION);
        Calendar calendar = Calendar.getInstance();
        calendar.set(2014, 8, 26);
        long startTime = calendar.getTimeInMillis();
        scheduledAction.setStartTime(startTime);
        assertThat(scheduledAction.getRecurrence()).isNull();

        Recurrence recurrence = new Recurrence(PeriodType.MONTH);
        assertThat(recurrence.getPeriodStart().getTime()).isNotEqualTo(startTime);
        scheduledAction.setRecurrence(recurrence);
        assertThat(recurrence.getPeriodStart().getTime()).isEqualTo(startTime);

        calendar.clear();
        calendar.set(2015, 6, 6);
        long newStartTime = calendar.getTimeInMillis();
        scheduledAction.setStartTime(newStartTime);
        assertThat(recurrence.getPeriodStart().getTime()).isEqualTo(newStartTime);
    }

    @Test
    public void settingEndTime_shouldSetRecurrenceEnd(){
        ScheduledAction scheduledAction = new ScheduledAction(ScheduledAction.ActionType.TRANSACTION);
        Calendar calendar = Calendar.getInstance();
        calendar.set(2014, 8, 26);
        long endTime = calendar.getTimeInMillis();
        scheduledAction.setEndTime(endTime);
        assertThat(scheduledAction.getRecurrence()).isNull();

        Recurrence recurrence = new Recurrence(PeriodType.MONTH);
        assertThat(recurrence.getPeriodEnd()).isNull();
        scheduledAction.setRecurrence(recurrence);
        assertThat(recurrence.getPeriodEnd().getTime()).isEqualTo(endTime);

        calendar.clear();
        calendar.set(2015, 6, 6);
        long newEndTime = calendar.getTimeInMillis();
        scheduledAction.setEndTime(newEndTime);
        assertThat(recurrence.getPeriodEnd().getTime()).isEqualTo(newEndTime);
    }

    @Test
    public void settingRecurrence_shouldSetScheduledActionStartTime(){
        ScheduledAction scheduledAction = new ScheduledAction(ScheduledAction.ActionType.BACKUP);
        assertThat(scheduledAction.getStartTime()).isEqualTo(0);

        Calendar calendar = Calendar.getInstance();
        calendar.set(2014, 8, 26);
        Recurrence recurrence = new Recurrence(PeriodType.WEEK);
        recurrence.setPeriodStart(new Timestamp(calendar.getTimeInMillis()));
        scheduledAction.setRecurrence(recurrence);
        assertThat(scheduledAction.getStartTime()).isEqualTo(calendar.getTimeInMillis());
    }

    @Test
    public void settingRecurrence_shouldSetEndTime(){
        ScheduledAction scheduledAction = new ScheduledAction(ScheduledAction.ActionType.BACKUP);
        assertThat(scheduledAction.getStartTime()).isEqualTo(0);

        Calendar calendar = Calendar.getInstance();
        calendar.set(2017, 8, 26);
        Recurrence recurrence = new Recurrence(PeriodType.WEEK);
        recurrence.setPeriodEnd(new Timestamp(calendar.getTimeInMillis()));
        scheduledAction.setRecurrence(recurrence);

        assertThat(scheduledAction.getEndTime()).isEqualTo(calendar.getTimeInMillis());
    }

    //todo add test for computing the scheduledaction endtime from the recurrence count
}

@@ -1181,95 +1181,8 @@
 N);%0A
-        Calendar calendar = Calendar.getInstance();%0A        calendar.set(2014, 8, 26);%0A
     
@@ -1198,33 +1198,24 @@
 startTime = 
-calendar.
 getTimeInMil
@@ -1210,32 +1210,43 @@
 getTimeInMillis(
+2014, 8, 26
 );%0A        sched
@@ -1629,68 +1629,8 @@
 );%0A%0A
-        calendar.clear();%0A        calendar.set(2015, 6, 6);%0A
     
@@ -1649,33 +1649,24 @@
 StartTime = 
-calendar.
 getTimeInMil
@@ -1661,32 +1661,42 @@
 getTimeInMillis(
+2015, 6, 6
 );%0A        sched
@@ -1998,95 +1998,8 @@
 N);%0A
-        Calendar calendar = Calendar.getInstance();%0A        calendar.set(2014, 8, 26);%0A
     
@@ -2013,33 +2013,24 @@
 g endTime = 
-calendar.
 getTimeInMil
@@ -2025,32 +2025,43 @@
 getTimeInMillis(
+2014, 8, 26
 );%0A        sched
@@ -2409,68 +2409,8 @@
 );%0A%0A
-        calendar.clear();%0A        calendar.set(2015, 6, 6);%0A
     
@@ -2431,25 +2431,16 @@
 dTime = 
-calendar.
 getTimeI
@@ -2447,16 +2447,26 @@
 nMillis(
+2015, 6, 6
 );%0A     
@@ -2851,72 +2851,40 @@
     
-Calendar calendar = Calendar.getInstance();%0A        calendar.set
+long startTime = getTimeInMillis
 (201
@@ -3011,34 +3011,17 @@
 amp(
-calendar.getTimeInMillis()
+startTime
 ));%0A
@@ -3136,34 +3136,17 @@
 lTo(
-calendar.getTimeInMillis()
+startTime
 );%0A 
@@ -3391,72 +3391,38 @@
     
-Calendar calendar = Calendar.getInstance();%0A        calendar.set
+long endTime = getTimeInMillis
 (201
@@ -3547,34 +3547,15 @@
 amp(
-calendar.getTimeInMillis()
+endTime
 ));%0A
@@ -3665,16 +3665,205 @@
 EqualTo(
+endTime);%0A    %7D%0A%0A    private long getTimeInMillis(int year, int month, int day) %7B%0A        Calendar calendar = Calendar.getInstance();%0A        calendar.set(year, month, day);%0A        return 
 calendar
@@ -3880,17 +3880,16 @@
 Millis()
-)
 ;%0A    %7D%0A

make getting the time for the tests simpler.