#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# Copyright (c) 2020, Sandflow Consulting LLC
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

'''Unit tests for the style properties defined in the canonical model'''

# pylint: disable=R0201,C0115,C0116

from fractions import Fraction
import unittest
from ttconv import model
from ttconv.isd import ISD
from ttconv.style_properties import ExtentType, FontVariantType, LengthType, NamedColors, StyleProperties, TextShadowType

class TestModelStyleProperties(unittest.TestCase):

  def setUp(self):
    self.doc = model.ContentDocument()

    self.r1 = model.Region("r1", self.doc)
    self.doc.put_region(self.r1)

    self.b = model.Body(self.doc)
    self.doc.set_body(self.b)

    self.div1 = model.Div(self.doc)
    self.div1.set_region(self.r1)
    self.b.push_child(self.div1)

    self.p1 = model.P(self.doc)
    self.div1.push_child(self.p1)

    self.span1 = model.Span(self.doc)
    self.p1.push_child(self.span1)

    self.text1 = model.Text(self.doc, "span1")
    self.span1.push_child(self.text1)

  def test_fontVariant(self):
    self.b.set_style(StyleProperties.FontVariant, FontVariantType.subscript)
    self.assertFalse(self.b.is_style_applicable(StyleProperties.FontVariant))

    a = model.DiscreteAnimationStep(StyleProperties.FontVariant, 1, None, FontVariantType.superscript)
    self.p1.add_animation_step(a)

    isd = ISD.from_model(self.doc, 0)
    r = list(isd)[0]
    self.assertIsNone(r[0][0].get_style(StyleProperties.FontVariant))
    self.assertEqual(r[0][0][0][0].get_style(StyleProperties.FontVariant), FontVariantType.subscript)

    isd = ISD.from_model(self.doc, 1)
    r = list(isd)[0]
    self.assertIsNone(r[0][0].get_style(StyleProperties.FontVariant))
    self.assertEqual(r[0][0][0][0].get_style(StyleProperties.FontVariant), FontVariantType.superscript)

  def test_make_initial(self):
    for style in StyleProperties.ALL:
      with self.subTest(style.__name__):
        style.validate(style.make_initial_value())

  def test_text_shadow_5_shadows_raises(self):
    span = model.Span()
    with self.assertRaises(ValueError):
      shadow = TextShadowType.Shadow(
        x_offset=LengthType(1, LengthType.Units.pct),
        y_offset=LengthType(1, LengthType.Units.pct),
      )
      span.set_style(
        StyleProperties.TextShadow,
        TextShadowType(shadows=(shadow, shadow, shadow, shadow, shadow)),
      )

  def test_extent_c_fail(self):
    doc = model.ContentDocument()

    r1 = model.Region("r1", doc)
    with self.assertRaises(ValueError):
      r1.set_style(
        StyleProperties.Extent,
        ExtentType(
          width=LengthType(10, LengthType.Units.c),
          height=LengthType(20, LengthType.Units.c)
        )
      )

if __name__ == '__main__':
  unittest.main()
