import json
from datetime import datetime, timedelta

from django.test import TestCase
from oauth.hep_client import HepClient
from users.licenses import MYSKILLBOX_LICENSES

ISBNS = list(MYSKILLBOX_LICENSES.keys())

STUDENT_ISBN = ISBNS[0]
STUDENT_LICENSE = MYSKILLBOX_LICENSES[STUDENT_ISBN]

TEACHER_ISBN = ISBNS[-1]
TEACHER_LICENSE = MYSKILLBOX_LICENSES[TEACHER_ISBN]


class HepClientTestCases(TestCase):
    def setUp(self):
        self.hep_client = HepClient()
        self.now = datetime.now()

    def test_has_no_valid_product(self):
        products = [
            {
                'license': TEACHER_LICENSE,
                'isbn': TEACHER_ISBN,
                'raw': {},
                'activated': self.now - timedelta(2*TEACHER_LICENSE['duration']),
                'status': 'complete'
            },
            {
                'license': TEACHER_LICENSE,
                'isbn': TEACHER_ISBN,
                'raw': {},
                'activated': self.now - timedelta(3 * TEACHER_LICENSE['duration']),
                'status': 'complete'
            },
            {
                'license': TEACHER_LICENSE,
                'isbn': TEACHER_ISBN,
                'raw': {},
                'activated': self.now - timedelta(4 * TEACHER_LICENSE['duration']),
                'status': 'complete'
            }
        ]

        relevant_product = self.hep_client._get_relevant_product(products)
        self.assertIsNone(relevant_product)

    def test_has_no_not_completed_product(self):
        products = [
            {
                'license': TEACHER_LICENSE,
                'isbn': TEACHER_ISBN,
                'raw': {},
                'activated': self.now - timedelta(7),
                'status': 'not'
            }
        ]

        relevant_product = self.hep_client._get_relevant_product(products)
        self.assertIsNone(relevant_product)

    def test_has_valid_product(self):
        products = [
            {
                'license': TEACHER_LICENSE,
                'isbn': TEACHER_ISBN,
                'raw': {
                    'id': 0
                },
                'activated': self.now - timedelta(7),
                'status': 'complete'
            },
            {
                'license': TEACHER_LICENSE,
                'isbn': TEACHER_ISBN,
                'raw': {
                    'id': 1
                },
                'activated': self.now - timedelta(3 * TEACHER_LICENSE['duration']),
                'status': 'complete'
            },
            {
                'license': TEACHER_LICENSE,
                'isbn': TEACHER_ISBN,
                'raw': {
                    'id': 2
                },
                'activated': self.now - timedelta(4 * TEACHER_LICENSE['duration']),
                'status': 'complete'
            }
        ]

        relevant_product = self.hep_client._get_relevant_product(products)
        self.assertEqual(relevant_product['raw']['id'], 0)

    def test_has_multiple_valid_teacher_products_but_only_one_active(self):
        products = [
            {
                'license': TEACHER_LICENSE,
                'isbn': TEACHER_ISBN,
                'raw': {
                    'id': 0
                },
                'activated': self.now - timedelta(7),
                'status': 'complete'
            },
            {
                'license': TEACHER_LICENSE,
                'isbn': TEACHER_ISBN,
                'raw': {
                    'id': 1
                },
                'activated': self.now - timedelta(3 * TEACHER_LICENSE['duration']),
                'status': 'complete'
            },
            {
                'license': TEACHER_LICENSE,
                'isbn': TEACHER_ISBN,
                'raw': {
                    'id': 2
                },
                'activated': self.now - timedelta(4 * TEACHER_LICENSE['duration']),
                'status': 'complete'
            }
        ]

        relevant_product = self.hep_client._get_relevant_product(products)
        self.assertEqual(relevant_product['raw']['id'], 0)

    def test_has_valid_student_and_teacher_edition(self):
        products = [
            {
                'license': STUDENT_LICENSE,
                'isbn': STUDENT_ISBN,
                'raw': {
                    'id': 0
                },
                'activated': self.now - timedelta(7),
                'status': 'complete'
            },
            {
                'license': TEACHER_LICENSE,
                'isbn': TEACHER_ISBN,
                'raw': {
                    'id': 1
                },
                'activated': self.now - timedelta(7),
                'status': 'complete'
            }
        ]

        relevant_product = self.hep_client._select_from_teacher_products(products)
        self.assertEqual(relevant_product['raw']['id'], 1)

    def test_product_is_active(self):

        expiry_date = self.now + timedelta(3)

        is_active = HepClient.is_product_active(expiry_date, TEACHER_ISBN)
        self.assertTrue(is_active)

    def test_product_is_not_active(self):

        expiry_date = self.now - timedelta(3 * TEACHER_LICENSE['duration'])

        is_active = HepClient.is_product_active(expiry_date, TEACHER_ISBN)
        self.assertFalse(is_active)
