import unittest
from unittest.mock import MagicMock

from nose.tools import assert_almost_equal, eq_
from numpy import array
from numpy.testing import assert_array_equal

from helix.calculators.ballast_calculator import BallastCalculator
from helix.constants.anchor_type import AnchorType
from helix.constants.module_type import ModuleType
from helix.constants.panel_type import PanelType
from helix.constants.system_type import SystemType
from helix.models.panel import Panel, PanelWarnings
from test.test_helpers import assert_array_is_close


class BallastCalculatorUpliftWarningsTest(unittest.TestCase):
    def setUp(self):
        self.values = MagicMock()
        """
        Generated using site parameters:
        height: 100
        length: 1000
        width: 1500
        parapet: 0
        wind_speed: 150
        exposure_category: D
        """

        self.c_p_matrix = array([[1.506920, 1.347220, 1.122684, 0.948795],
                                 [1.186590, 1.057265, 0.881054, 0.740239],
                                 [0.676809, 0.615810, 0.439865, 0.382935],
                                 [0.685883, 0.602310, 0.430222, 0.352224],
                                 [0.460258, 0.272456, 0.408684, 0.227532],
                                 [0.438829, 0.388352, 0.298733, 0.247999],
                                 [0.206406, 0.186191, 0.186191, 0.159779],
                                 [0.560763, 0.491939, 0.491939, 0.402013],
                                 [1.548721, 1.320138, 1.320138, 1.021469],
                                 [0.921232, 0.799827, 0.399914, 0.320599],
                                 [0.168333, 0.142587, 0.101848, 0.078692]])
        self.system_type = SystemType.singleTilt
        self.module_type = ModuleType.Cell96
        self.values.max_system_pressure.return_value = 1000
        self.values.system_type.return_value = self.system_type
        self.values.anchor_type.return_value = AnchorType.OMG_PowerGrip
        self.values.module_system_constants.return_value = self.system_type.module_constants(self.module_type)
        self.q_z = 70.155851
        self.subject = BallastCalculator(self.values)

    def test_exceeding_maximal_uplift(self):
        expected = [PanelWarnings.MaxPsf]
        for i in range(0,1):
            actual = self.subject.ballast_tray_and_anchor_count(i, PanelType.Middle, 14, 100, self.c_p_matrix, self.q_z)
            assert_array_equal(expected, actual.warnings)

    def test_not_exceeding_maximal_uplift(self):
        expected = []
        for i in range(2,7):
            actual = self.subject.ballast_tray_and_anchor_count(i, PanelType.Middle, 14, 100, self.c_p_matrix, self.q_z)
            assert_array_equal(expected, actual.warnings)
