from helix.constants.inverter_type import InverterType
from helix.constants.module_type import ModuleType
from helix.constants.parts import *
from helix.constants.system_type import SystemType

inverter_model_parts = {
    InverterType.SMA.MODEL_12KW: {sma_12kw_inverter: 1},
    InverterType.SMA.MODEL_15KW: {sma_15kw_inverter: 1},
    InverterType.SMA.MODEL_20KW: {sma_20kw_inverter: 1},
    InverterType.SMA.MODEL_24KW: {sma_24kw_inverter: 1},
    InverterType.DELTA.MODEL_36KW: {delta_36kw_inverter: 1},
    InverterType.DELTA.MODEL_42KW: {delta_42kw_inverter: 1},
    InverterType.DELTA.MODEL_60KW: {delta_60kw_inverter: 1},
    InverterType.DELTA.MODEL_80KW: {delta_80kw_inverter: 1},
}

inverter_strings_parts = {
    0: {},
    2: {
        harness_2_string_mf: 1,
        harness_2_string_fm: 1
    },
    3: {
        harness_3_string_mf: 1,
        harness_3_string_fm: 1
    },
    4: {
        harness_2_string_mf: 2,
        harness_2_string_fm: 2
    },
    5: {
        harness_2_string_mf: 1,
        harness_2_string_fm: 1,
        harness_3_string_mf: 1,
        harness_3_string_fm: 1
    },
    6: {
        harness_3_string_mf: 2,
        harness_3_string_fm: 2
    },
    7: {
        harness_3_string_mf: 1,
        harness_3_string_fm: 1,
        harness_4_string_mf: 1,
        harness_4_string_fm: 1
    },
    8: {
        harness_4_string_mf: 2,
        harness_4_string_fm: 2
    },
}


def shared_panel_board_parts(module_type, system_type):
    v1_1_inverter_links = 0
    if system_type == SystemType.singleTilt and (module_type == ModuleType.PSeries or module_type == ModuleType.Cell128):
        v1_1_inverter_links = 1
    return {
        front_legs: 1,
        back_legs: 1,
        inverter_link: 2,
        inverter_rail: 1,
        rubber_foot: 3,
        mounting_back_plate: 1,
        ethernet_plug: 1.8,
        flat_washer: 4,
        hex_nut_three_eighths_16: 2,
        hex_bolt_1_2: 9,
        inverter_link_long: v1_1_inverter_links,
        inverter_link_short: v1_1_inverter_links,
    }

dc_switch_parts = {
    dc_switch_bracket: 1,
    dc_switch_box: 1,
    hex_bolt_3_4: 4,
    hex_bolt_quarter_20: 4,
    hex_nut_three_eighths_16: 4,
    hex_nut_quarter_20: 4,
    flat_washer_quarter_inch: 4,
}


def panel_board_parts(inverter_quantity, with_aux):
    if with_aux:
        parts = {
            1: {
                panel_board_2_aux: 1,
            },
            2: {
                panel_board_2_aux: 1,
            },
            3: {
                panel_board_3_aux: 1,
            },
            4: {
                panel_board_4_aux: 1,
            }
        }
    else:
        parts = {
            1: {
                panel_board_2: 1,
            },
            2: {
                panel_board_2: 1,
            },
            3: {
                panel_board_3: 1,
            },
            4: {
                panel_board_4: 1,
            }
        }
    return parts[inverter_quantity]


def panel_board_parts_with_monitor(inverter_quantity, monitor_controller_type):
    parts = panel_board_parts(inverter_quantity, monitor_controller_type != monitor_controller_240_v)
    parts[monitor_controller_type] = 1
    return parts


def standalone_inverter_parts(inverter, system_type, module_type):
    multiplier = 1
    v1_1_inverter_links = 0

    if inverter['model'] in InverterType.DELTA.all():
        parts = {}
        if system_type == SystemType.singleTilt:
            parts = {
              **parts, 
              delta_kit_inverter_mount: 1,
              delta_kit_inverter_mount_dt: 0
            }
        else:
            parts = {
              **parts, 
              delta_kit_inverter_mount: 0,
              delta_kit_inverter_mount_dt: 1
            }
        if inverter['splice_box']:
            parts = {**parts, delta_splice_box: 1}
        return parts

    if system_type == SystemType.singleTilt:
        multiplier = 2
        v1_1_inverter_links = 1 if module_type == ModuleType.PSeries or module_type == ModuleType.Cell128 else 0
    return {
        ac_switch: 1,
        star_washer: 4,
        phillips_screw: 4 * multiplier,
        ac_inverter_bracket: 1 * multiplier,
        hex_bolt_1_2: 4,
        ethernet_plug: 1.8,
        flat_washer_6: 4,
        inverter_link_short: v1_1_inverter_links,
        inverter_link_long: v1_1_inverter_links,
        whip_tray: 1
    }


standalone_inverter_attached_to_panel_board_parts = {
    ac_splice_box: 1,
}


def inverter_parts(inverter, module_type):
    if inverter['model'] in InverterType.DELTA.all():
        return {
            delta_inverter_leg: 3,
            rubber_foot: 3,
        }
    else:
        if module_type == ModuleType.PSeries:
            return {
                flat_washer: 8,
                hex_nut_three_eighths_16: 6,
                hex_bolt_3_4: 5,
                hex_bolt_1_2: 18,
                front_legs: 1,
                back_legs: 1,
                mounting_back_plate: 1,
                rubber_foot: 3,
                inverter_link: 2,
                inverter_rail: 1,
                stump: 6,
                fuseshade: 1,
                screw_12_24x1_25: 2,
                fuseshade_brace: 1,
            }
        else:
            return {
                flat_washer: 4,
                hex_nut_three_eighths_16: 4,
                hex_bolt_3_4: 2,
                hex_bolt_1_2: 18,
                front_legs: 1,
                back_legs: 1,
                mounting_back_plate: 1,
                rubber_foot: 3,
                inverter_link: 2,
                inverter_rail: 1,
                stump: 6,
            }

def dependent_parts(module_type, system_type, is_delta):
    v1_1_inverter_links = 0
    if system_type == SystemType.singleTilt and (module_type == ModuleType.Cell128 or module_type == ModuleType.PSeries):
        v1_1_inverter_links = 1

    if is_delta:
        monitor_controller_480_v_={
            monitor_power_plug: 1,
        }
    else:
        monitor_controller_480_v_={
            monitor_power_plug: 1,
            flat_washer: 4,
            channel_nut: 4,
            hex_nut_three_eighths_16: 2,
            front_legs: 1,
            back_legs: 1,
            inverter_link: 2,
            inverter_rail: 1,
            rubber_foot: 3,
            hex_bolt_1_2: 9,
            mounting_back_plate: 1,
        }

    return {
        panel_board_2: {
            harness_ac_inner: 2,
            whip_tray: 2,
            comm_cable: 1
        },
        panel_board_2_aux: {
            harness_ac_inner: 2,
            whip_tray: 2,
            comm_cable: 2
        },
        panel_board_3: {
            harness_ac_inner: 2,
            harness_ac_outer: 1,
            whip_tray: 3,
            comm_cable: 2,
            inverter_link_short: v1_1_inverter_links,
            inverter_link_long: v1_1_inverter_links,
            inverter_link: -2 * v1_1_inverter_links,
        },
        panel_board_3_aux: {
            harness_ac_inner: 2,
            harness_ac_outer: 1,
            whip_tray: 3,
            comm_cable: 3,
            inverter_link_short: v1_1_inverter_links,
            inverter_link_long: v1_1_inverter_links,
            inverter_link: -2 * v1_1_inverter_links,
        },
        panel_board_4: {
            harness_ac_inner: 2,
            harness_ac_outer: 2,
            whip_tray: 4,
            comm_cable: 3,
            inverter_link_short: v1_1_inverter_links,
            inverter_link_long: v1_1_inverter_links,
            inverter_link: -2 * v1_1_inverter_links,
        },
        panel_board_4_aux: {
            harness_ac_inner: 2,
            harness_ac_outer: 2,
            whip_tray: 4,
            comm_cable: 4,
            inverter_link_short: v1_1_inverter_links,
            inverter_link_long: v1_1_inverter_links,
            inverter_link: -2 * v1_1_inverter_links,
        },
        monitor_controller_480_v: monitor_controller_480_v_,
        monitor_controller_240_v: {},
        ac_splice_box: {
            hex_bolt_1_2: 4,
            hex_bolt_3_4: 4,
            hex_nut_three_eighths_16: 8,
            flat_washer: 8,
            back_legs: 1,
            front_legs: 1,
            rubber_foot: 3,
            inverter_rail: 1,
            inverter_link: 2,
            mounting_back_plate: 1,
        },
        inverter_link_long: {
            inverter_link: -1,
        },
        inverter_link_short: {
            inverter_link: -1,
        }
    }
