from numpy import array

from helix.constants.global_constants import minimum_racking_capacity
from helix.constants.panel_type import PanelType


class DualTilt96CellConstants(object):
    tolerance = 0.1 #require 10% additional space for panel spacing
    panel_spacing = (88.24, 62.0)  # inches
    spacing_size_inches = 5.12  # This is inches
    presenter_spacing = (1.5, 1)
    panel_area = 17.58
    surface_area = 35.14  # aka tent area
    tributary_area = array([3.32, 6.58, 7.29, 21.74])  # for each panel type
    ground_coverage_ratio = 0.91
    friction_coefficient = 0.59
    seismic_anchor_interval_constants = (10.1598, 2.3706)
    max_psf = 48

    def c_p_lower_bound(self):
        return array([0.06, 0.05, 0.052, 0.039])

    def edge_factor(self, wind_zone, panel_type):
        return 1

    def racking_capacity(self, panel_type):
        if panel_type == PanelType.Corner or panel_type == PanelType.NorthSouth:
            return minimum_racking_capacity
        else:
            return 308

    def c_p_constants(self, A_n, wind_zone):
        if wind_zone == 'A':
            if A_n < 825:
                return -0.144, 1.1451
            else:
                return -0.058, 0.5651
        elif wind_zone == 'B':
            if A_n < 465:
                return -0.117, 0.8824
            elif A_n < 825:
                return -0.087, 0.6981
            else:
                return -0.033, 0.3336
        elif wind_zone == 'C':
            if A_n < 465:
                return -0.073, 0.5479
            elif A_n < 825:
                return -0.035, 0.3143
            else:
                return -0.015, 0.184
        elif wind_zone == 'D':
            if A_n < 465:
                return -0.039, 0.303
            elif A_n < 825:
                return -0.023, 0.2023
            else:
                return -0.008, 0.102
        else:
            return 1, 1

    def base_weight(self, panel_type, tray_count):
        if panel_type == PanelType.Corner or panel_type == PanelType.NorthSouth:
            return [92.58,
                    94.31,
                    96.03,
                    98.33,
                    100.63,
                    102.93,
                    105.23][tray_count]
        else:
            return [87.11,
                    88.84,
                    89.41,
                    91.71,
                    94.01,
                    96.31,
                    98.61][tray_count]

    def link_tray_thresholds(self, panel_type):
        if panel_type == PanelType.Corner or panel_type == PanelType.NorthSouth:
            return [7.5, 10, 15]
        else:
            return [5, 7.5, 10]

    def cross_tray_thresholds(self, panel_type):
        if panel_type == PanelType.Corner or panel_type == PanelType.NorthSouth:
            return [15, 24, 33, 42, 51]
        else:
            return [10, 19, 28, 37, 46]

    def weighted_average_c_p(self, corner, north_south, east_west, middle):
        return (corner * 2. + east_west * 26. + middle * 104. + north_south * 8.) / 140.

    def minimum_a_n_coefficients(self, uplift_c_p, wind_zone):
        if wind_zone == 'A':
            if uplift_c_p > 0.275:
                return 1.5305, 0.187
            else:
                return 0.9252, 0.097
        elif wind_zone == 'B':
            if uplift_c_p > 0.2292:
                return 1.2058, 0.159
            elif uplift_c_p > 0.1537:
                return 1.0334, 0.131
            else:
                return 0.4411, 0.043
        elif wind_zone == 'C':
            if uplift_c_p > 0.151:
                return 0.7899, 0.104
            elif uplift_c_p > 0.108:
                return 0.5785, 0.07
            else:
                return 0.2921, 0.027
        elif wind_zone == 'D':
            if uplift_c_p > 0.093:
                return 0.3939, 0.049
            elif uplift_c_p > 0.069:
                return 0.3043, 0.035
            else:
                return 0.122, 0.008
        return None
