import unittest
from unittest.mock import MagicMock
from nose.tools import eq_
from helix.constants.panel_type import PanelType
from helix.constants.seismic_anchor_validation_error import SeismicAnchorValidationError
from helix.models.panel import Panel
from helix.models.subarray import Subarray
from helix.validators.seismic_anchor_validator import SeismicAnchorValidator


class SeismicAnchorValidatorTest(unittest.TestCase):
    def setUp(self):
        self.calculator = MagicMock()
        self.calculator.subarray_summary.return_value = [
            Subarray(subarray_number=0, required_seismic_anchors=1, weight=1000)
        ]
        self.subject = SeismicAnchorValidator(self.calculator)

    def test_seismic_anchor_below_needed_seismic_anchors_is_invalid(self):
        panels = [
            Panel(panel_type=PanelType.Corner, wind_anchors=0, pressure=11.31, subarray=0, seismic_anchors=0),
            Panel(panel_type=PanelType.Corner, wind_anchors=0, pressure=11.31, subarray=0, seismic_anchors=0),
            Panel(panel_type=PanelType.Corner, wind_anchors=1, pressure=7.37, subarray=0, seismic_anchors=0),
            Panel(panel_type=PanelType.NorthSouth, wind_anchors=0, pressure=10.00, subarray=0, seismic_anchors=0),
            Panel(panel_type=PanelType.Middle, wind_anchors=0, pressure=12.44, subarray=0, seismic_anchors=0),
            Panel(panel_type=PanelType.NorthSouth, wind_anchors=0, pressure=10.00, subarray=0, seismic_anchors=0),
            Panel(panel_type=PanelType.NorthSouth, wind_anchors=1, pressure=5.19, subarray=0, seismic_anchors=0)
        ]
        eq_(self.subject.validate(panels), SeismicAnchorValidationError.TooFewAnchors)

    def test_seismic_anchor_at_needed_seismic_anchors_is_valid(self):
        panels =[
            Panel(panel_type=PanelType.Corner, wind_anchors=0, pressure=11.31, subarray=0, seismic_anchors=1),
            Panel(panel_type=PanelType.Corner, wind_anchors=0, pressure=11.31, subarray=0, seismic_anchors=0),
            Panel(panel_type=PanelType.Corner, wind_anchors=1, pressure=7.37, subarray=0, seismic_anchors=0),
            Panel(panel_type=PanelType.NorthSouth, wind_anchors=0, pressure=10.00, subarray=0, seismic_anchors=0),
            Panel(panel_type=PanelType.Middle, wind_anchors=0, pressure=12.44, subarray=0, seismic_anchors=0),
            Panel(panel_type=PanelType.NorthSouth, wind_anchors=0, pressure=10.00, subarray=0, seismic_anchors=0),
            Panel(panel_type=PanelType.NorthSouth, wind_anchors=1, pressure=5.19, subarray=0, seismic_anchors=0)
        ]
        eq_(self.subject.validate(panels), None)
