# Copyright (C) 2017 Google Inc.
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>

import datetime
import freezegun
import os
import random

from ggrc import app  # noqa
from ggrc import db
from ggrc_workflows import models
from ggrc_workflows import start_recurring_cycles
from ggrc_workflows.services.workflow_cycle_calculator import \
    get_cycle_calculator

from integration.ggrc_workflows.workflow_cycle_calculator import \
    base_workflow_test_case

if os.environ.get('TRAVIS', False):
  random.seed(1)  # so we can reproduce the tests if needed


class TestAnnuallyWorkflow(base_workflow_test_case.BaseWorkflowTestCase):

  def test_annually_workflow(self):
    """Basic annual workflow test.

    """
    annually_wf = {
        "title": "annually thingy",
        "description": "start this many a time",
        "frequency": "annually",
        "task_groups": [{
            "title": "task group",
            "task_group_tasks": [
                {
                    'title': 'annual task 1',
                    "relative_start_day": 10,  # 6/10/2015  Wed
                    "relative_start_month": 6,
                    "relative_end_day": 25,  # 6/25/2015 Thu
                    "relative_end_month": 6,
                },
                {
                    'title': 'annual task 2',
                    "relative_start_day": 15,  # 6/15/2015 Mon
                    "relative_start_month": 6,
                    "relative_end_day": 9,  # 8/9/2015 Sun
                    "relative_end_month": 8,
                }],
            "task_group_objects": self.random_objects
        },
        ]
    }
    with freezegun.freeze_time("2015-6-8 13:00:00"):  # Mon, 6/8/2015
      _, wf = self.generator.generate_workflow(annually_wf)
      _, awf = self.generator.activate_workflow(wf)

      active_wf = db.session.query(models.Workflow).filter(
          models.Workflow.id == wf.id).one()
      self.assertEqual(active_wf.status, "Active")
      self.assertEqual(active_wf.next_cycle_start_date,
                       datetime.date(2015, 6, 10))

    with freezegun.freeze_time("2015-6-10 13:00:00"):  # Mon, 6/8/2015
      start_recurring_cycles()

      cycle = db.session.query(models.Cycle).filter(
          models.Cycle.workflow_id == wf.id).one()

      self.assertEqual(cycle.start_date, datetime.date(2015, 6, 10))
      # Because end date is on Sunday, relative start day will have to be
      # adjusted
      self.assertEqual(cycle.end_date, datetime.date(2015, 8, 7))

      _, cycle = self.generator.generate_cycle(wf)  # 2016
      _, cycle = self.generator.generate_cycle(wf)  # 2017
      _, cycle = self.generator.generate_cycle(wf)  # 2018

      active_wf = db.session.query(models.Workflow).filter(
          models.Workflow.id == wf.id).one()
      self.assertEqual(active_wf.next_cycle_start_date,
                       datetime.date(2019, 6, 10))
      self.assertEqual(cycle.start_date, datetime.date(2018, 6, 8))
      self.assertEqual(cycle.end_date, datetime.date(2018, 8, 9))

  def test_type_casting(self):
    """Verify type casting for string input

    Test if string values get converted correctly to integers
    and arithmetic works"""

    annually_wf = {
        "title": "annually thingy",
        "description": "start this many a time",