import unittest
from nose.tools import eq_, assert_not_equal
from helix.constants.panel_type import PanelType
from helix.models.coordinate import Coordinate
from helix.models.panel import Panel, PanelData


class PanelTest(unittest.TestCase):
    def setUp(self):
        self.subject = Panel(
            handle="'14DAB3",
            blockname="*U13608",
            subarray=1,
            panel_type=PanelType.Corner,
            wind_zone=0,
            ballast=5,
            link_tray=None,
            cross_tray=None,
            wind_anchors=0,
            seismic_anchors=1,
            coordinate=Coordinate(0, 1),
            original_coordinate=Coordinate(0, 1),
            pressure=3.58,
            id=1,
            presented_link_tray=2
        )

    def test_almost_equal_pressure_almost_equal(self):
        almost_equal_panel = Panel(
            handle="'14DAB3",
            blockname="*U13608",
            subarray=1,
            panel_type=PanelType.Corner,
            wind_zone=0,
            ballast=5,
            link_tray=None,
            cross_tray=None,
            wind_anchors=0,
            seismic_anchors=1,
            coordinate=Coordinate(0, 1),
            original_coordinate=Coordinate(0, 1),
            pressure=3.5805,
            id=1,
            presented_link_tray=2
        )

        assert self.subject.almost_equal(almost_equal_panel, decimal=3)

    def test_almost_equal_pressure_not_equal(self):

        not_equal_panel = Panel(
            handle="'14DAB3",
            blockname="*U13608",
            subarray=1,
            panel_type=PanelType.Corner,
            wind_zone=0,
            ballast=5,
            link_tray=None,
            cross_tray=None,
            wind_anchors=0,
            seismic_anchors=1,
            coordinate=Coordinate(0, 1),
            original_coordinate=Coordinate(0, 1),
            pressure=3.585,
            id=1,
            presented_link_tray=2
        )
        assert not self.subject.almost_equal(not_equal_panel, decimal=3)

    def test_almost_equal_other_attribute_not_equal(self):

        not_even_close_panel = Panel(
            handle="FOO",
            blockname="*U13608",
            subarray=1,
            panel_type=PanelType.Corner,
            wind_zone=0,
            ballast=5,
            link_tray=None,
            cross_tray=None,
            wind_anchors=0,
            seismic_anchors=1,
            coordinate=Coordinate(0, 1),
            pressure=3.58,
            id=1,
            presented_link_tray=2
        )
        assert not self.subject.almost_equal(not_even_close_panel, decimal=3)

    def test_equality_identity(self):
        eq_(self.subject, self.subject)

    def test_equality_property(self):
        same_panel = Panel(
            handle="'14DAB3",
            blockname="*U13608",
            subarray=1,
            panel_type=PanelType.Corner,
            wind_zone=0,
            ballast=5,
            link_tray=None,
            cross_tray=None,
            wind_anchors=0,
            seismic_anchors=1,
            coordinate=Coordinate(0, 1),
            original_coordinate=Coordinate(0, 1),
            pressure=3.58,
            id=1,
            presented_link_tray=2
        )
        eq_(self.subject, same_panel)

    def test_equality_subobject(self):
        a = Panel(
            handle="'14DAB3",
            blockname="*U13608",
            id=1,
            presented_link_tray=2
        )

        b = Panel(
            handle="'14DAB3",
            blockname="*U13608",
            id=1,
            presented_link_tray=2
        )

        eq_(a, b)

        assert_not_equal(self.subject, a)

    def test_is_subset(self):
        a = Panel(
            handle="'14DAB3",
            blockname="*U13608",
            id=1,
            presented_link_tray=2
        )

        b = Panel(
            handle="'14DAB3",
            blockname="*U13608",
            id=2,
            presented_link_tray=2
        )

        assert a.is_subset(self.subject)

        assert not b.is_subset(self.subject)

    def test_repr(self):
        expected = '{' \
            '"ballast": 5, ' \
            '"blockname": "*U13608", ' \
            '"coordinate": {"rotation": 0.0, "x": 0, "y": 1}, ' \
            '"fuzzy_wind_zone": false, '\
            '"handle": "\'14DAB3", ' \
            '"id": 1, ' \
            '"original_coordinate": {"rotation": 0.0, "x": 0, "y": 1}, ' \
            '"panel_type": 0, ' \
            '"presented_link_tray": 2, ' \
            '"pressure": 3.58, ' \
            '"seismic_anchors": 1, ' \
            '"subarray": 1, ' \
            '"warnings": [], ' \
            '"wind_anchors": 0, ' \
            '"wind_zone": 0}'

        eq_(str(self.subject), expected)

    def test_merge_two_panels(self):
        a = Panel(
            handle="'14DAB3",
            blockname="*U13608",
            id=1,
            presented_link_tray=2
        )

        b = Panel(
            handle="'14DAB3",
            blockname="*U13608",
            id=2,
            presented_link_tray=2,
            ballast=10
        )

        merged = a.merge(b)

        eq_(merged, Panel(
            handle="'14DAB3",
            blockname="*U13608",
            id=1,
            presented_link_tray=2,
            ballast=10
        ))

