import unittest
from nose.tools import eq_
from helix.Services.dxf_helper import DXFHelper
from helix.constants.system_type import SystemType
from helix.models.coordinate import Coordinate
from helix.models.dxf.dxf_error import DXFError
from helix.models.panel import Panel
from helix.validators.subarray_validator import SubarrayValidator


class SubarrayValidatorTest(unittest.TestCase):
    def test_subarrays_taller_than_150_feet_are_invalid(self):
        panels = []
        for i in range(33):
            panels.append(Panel(coordinate=Coordinate(0, i * 60.0, rotation=0), subarray=1))
            panels.append(Panel(coordinate=Coordinate(62.15, i * 60.0, rotation=0), subarray=1))
        node_graph = DXFHelper.build_node_graph(panels, (62.15, 60.0))

        try:
            SubarrayValidator.validate_subarray(node_graph, 1, SystemType.singleTilt)
            assert False
        except DXFError as error:
            eq_(error.message, "Array size is too big. Max is 150' by 150'.")

    def test_subarrays_wider_than_150_feet_are_invalid(self):
        panels = []
        for i in range(30):
            panels.append(Panel(coordinate=Coordinate(i * 62.15, 0, rotation=0), subarray=1))
            panels.append(Panel(coordinate=Coordinate(i * 62.15, 60.0, rotation=0), subarray=1))
        node_graph = DXFHelper.build_node_graph(panels, (62.15, 60))

        try:
            SubarrayValidator.validate_subarray(node_graph, 1, None)
            assert False
        except DXFError as error:
            eq_(error.message, "Array size is too big. Max is 150' by 150'.")

    def test_single_file_horizontal_line_is_invalid_single_tilt(self):
        panels = [
            Panel(coordinate=Coordinate(10, 0, rotation=0), subarray=1),
            Panel(coordinate=Coordinate(20, 0, rotation=0), subarray=1),
            Panel(coordinate=Coordinate(30, 0, rotation=0), subarray=1),
            Panel(coordinate=Coordinate(30, 10, rotation=0), subarray=1),
            Panel(coordinate=Coordinate(40, 0, rotation=0), subarray=1),
            Panel(coordinate=Coordinate(40, 10, rotation=0), subarray=1)
        ]
        node_graph = DXFHelper.build_node_graph(panels, (10, 10))

        try:
            SubarrayValidator.validate_subarray(node_graph, 1, SystemType.singleTilt)
            assert False
        except DXFError as error:
            eq_(error.message, "Error - Unsupported panel configuration in subarray 1.")

    def test_single_file_vertical_line_is_invalid_single_tilt(self):
        panels = [
            Panel(coordinate=Coordinate(0, 10, rotation=0), subarray=2),
            Panel(coordinate=Coordinate(0, 20, rotation=0), subarray=2),
            Panel(coordinate=Coordinate(0, 30, rotation=0), subarray=2),
            Panel(coordinate=Coordinate(10, 30, rotation=0), subarray=2),
            Panel(coordinate=Coordinate(0, 40, rotation=0), subarray=2),
            Panel(coordinate=Coordinate(10, 40, rotation=0), subarray=2)
        ]
        node_graph = DXFHelper.build_node_graph(panels, (10, 10))

        try:
            SubarrayValidator.validate_subarray(node_graph, 2, SystemType.singleTilt)
            assert False
        except DXFError as error:
            eq_(error.message, "Error - Unsupported panel configuration in subarray 2.")

    def test_other_single_tilt_subarrays_are_valid(self):
        panels = [
            Panel(coordinate=Coordinate(0, 0, rotation=0), subarray=1),
            Panel(coordinate=Coordinate(0, 1, rotation=0), subarray=1),
            Panel(coordinate=Coordinate(1, 0, rotation=0), subarray=1),
            Panel(coordinate=Coordinate(1, 1, rotation=0), subarray=1)
        ]
        node_graph = DXFHelper.build_node_graph(panels, (1, 1))

        SubarrayValidator.validate_subarray(node_graph, 1, SystemType.singleTilt)

    def test_single_file_horizontal_line_is_invalid_dual_tilt(self):
        panels = [
            Panel(coordinate=Coordinate(10, 0, rotation=0), subarray=1),
            Panel(coordinate=Coordinate(20, 0, rotation=0), subarray=1),
            Panel(coordinate=Coordinate(30, 0, rotation=0), subarray=1),
            Panel(coordinate=Coordinate(30, 10, rotation=0), subarray=1),
            Panel(coordinate=Coordinate(40, 0, rotation=0), subarray=1),
            Panel(coordinate=Coordinate(40, 10, rotation=0), subarray=1)
        ]
        node_graph = DXFHelper.build_node_graph(panels, (10, 10))

        try:
            SubarrayValidator.validate_subarray(node_graph, 1, SystemType.dualTilt)
            assert False
        except DXFError as error:
            eq_(error.message, "Error - Unsupported panel configuration in subarray 1.")

    def test_single_file_vertical_line_is_valid_dual_tilt(self):
        panels = [
            Panel(coordinate=Coordinate(0, 1, rotation=0), subarray=1),
            Panel(coordinate=Coordinate(0, 2, rotation=0), subarray=1),
            Panel(coordinate=Coordinate(0, 3, rotation=0), subarray=1),
            Panel(coordinate=Coordinate(1, 3, rotation=0), subarray=1),
            Panel(coordinate=Coordinate(0, 4, rotation=0), subarray=1),
            Panel(coordinate=Coordinate(1, 4, rotation=0), subarray=1)
        ]
        node_graph = DXFHelper.build_node_graph(panels, (1, 1))

        SubarrayValidator.validate_subarray(node_graph, 1, SystemType.dualTilt)

    def test_other_dual_tilt_subarrays_are_valid(self):
        panels = [
            Panel(coordinate=Coordinate(0, 0, rotation=0), subarray=1),
            Panel(coordinate=Coordinate(0, 1, rotation=0), subarray=1)
        ]
        node_graph = DXFHelper.build_node_graph(panels, (1, 1))

        SubarrayValidator.validate_subarray(node_graph, 1, SystemType.dualTilt)
