import unittest

from helix.csv_builder import CsvBuilder
from nose.tools import eq_
from numpy import array
from helix.constants.panel_type import PanelType
from helix.models.coordinate import Coordinate
from helix.models.panel import Panel


class CsvBuilderTest(unittest.TestCase):
    def setUp(self):
        self.subject = CsvBuilder()

    def test_build_cad_output_correct_csv(self):
        panels = [
            Panel(handle='FOO', blockname='BAR', wind_zone=0, panel_type=PanelType.Corner, subarray=1, pressure=1.026,
                  ballast=10, presented_link_tray=1, cross_tray=0, wind_anchors=0, id=10, original_coordinate=Coordinate(2, 3.0, rotation=4.00), seismic_anchors=0),
            Panel(handle='BAZ', blockname='QUX', wind_zone=2, panel_type=PanelType.Middle, subarray=1, pressure=3.033,
                  ballast=3, presented_link_tray=0, cross_tray=1, wind_anchors=1, id=11, original_coordinate=Coordinate(1, 0.5, rotation=.25), seismic_anchors=1),
            Panel(handle='EGGS', blockname='SPAM', wind_zone=0, panel_type=PanelType.Corner, subarray=1, pressure=1.026,
                  ballast=10, presented_link_tray=1, cross_tray=0, wind_anchors=0, id=12, original_coordinate=Coordinate(2, 3.0, rotation=4.00), seismic_anchors=2)
        ]

        expected_csv = "HANDLE\tBLOCKNAME\tWIND\tPOS\tSUBARRAY\tPSF\tBAL\tLTRAY\tXTRAY\tANC\tID\tXCOORD\tYCOORD\tANGLE\r\n" \
                       "FOO\tBAR\tA\t1\t1\t1.03\t10\t1\t-\t-\t10\t2\t3.0\t4.0\r\n" \
                       "BAZ\tQUX\tC\t4\t1\t3.03\t3\t-\t1\t1S\t11\t1\t0.5\t0.25\r\n" \
                       "EGGS\tSPAM\tA\t1\t1\t1.03\t10\t1\t-\tSS\t12\t2\t3.0\t4.0\r\n"

        eq_(self.subject.build_cad_output(panels), expected_csv)

    def test_build_cad_output_correct_csv_with_fuzzy_wind_zone_if_exists(self):
        panels = [
            Panel(handle='FOO', blockname='BAR', wind_zone=0, panel_type=PanelType.Corner, subarray=1, pressure=1.026,
                  ballast=10, presented_link_tray=1, cross_tray=0, wind_anchors=0, id=10, original_coordinate=Coordinate(2, 3.0, rotation=4.00), seismic_anchors=0),
            Panel(handle='BAZ', blockname='QUX', wind_zone=2, panel_type=PanelType.Middle, subarray=1, pressure=3.033,
                  ballast=3, presented_link_tray=0, cross_tray=1, wind_anchors=1, id=11, original_coordinate=Coordinate(1, 0.5, rotation=.25), seismic_anchors=1,
                  fuzzy_wind_zone=True),
            Panel(handle='EGGS', blockname='SPAM', wind_zone=0, panel_type=PanelType.Corner, subarray=1, pressure=1.026,
                  ballast=10, presented_link_tray=1, cross_tray=0, wind_anchors=0, id=12, original_coordinate=Coordinate(2, 3.0, rotation=4.00), seismic_anchors=2,
                  fuzzy_wind_zone=True)
        ]

        expected_csv = "HANDLE\tBLOCKNAME\tWIND\tPOS\tSUBARRAY\tPSF\tBAL\tLTRAY\tXTRAY\tANC\tID\tXCOORD\tYCOORD\tANGLE\tFUZZYWINDZONE\r\n" \
                       "FOO\tBAR\tA\t1\t1\t1.03\t10\t1\t-\t-\t10\t2\t3.0\t4.0\t0\r\n" \
                       "BAZ\tQUX\tC\t4\t1\t3.03\t3\t-\t1\t1S\t11\t1\t0.5\t0.25\t1\r\n" \
                       "EGGS\tSPAM\tA\t1\t1\t1.03\t10\t1\t-\tSS\t12\t2\t3.0\t4.0\t1\r\n"

        eq_(self.subject.build_cad_output(panels), expected_csv)

    def test_build_bom_output_outputs_bom(self):
        expected_csv = "Part #\tDescription\tTotal\r\n6\tFoo\t404\r\n5\tBar\"\t503\r\n7\tBaz\t1337\r\n"

        calculated_columns = array([[6, 'Foo', 404],
                                    [5, 'Bar"', 503],
                                    [7, 'Baz', 1337]])
        eq_(self.subject.build_bom_output(calculated_columns), expected_csv)

    def test_build_cad_output_with_incomplete_input(self):
        panels = [
            Panel(subarray=1, id=1, original_coordinate=Coordinate(2, 3.0, rotation=4.00))
        ]

        expected_csv = "HANDLE\tBLOCKNAME\tWIND\tPOS\tSUBARRAY\tPSF\tBAL\tLTRAY\tXTRAY\tANC\tID\tXCOORD\tYCOORD\tANGLE\r\n" \
                       "\t\t\t\t1\t\t\t-\t-\t-\t1\t2\t3.0\t4.0\r\n"

        eq_(self.subject.build_cad_output(panels), expected_csv)
