from util import DeviceInfo, add_to_group
from ruamel.yaml import YAML
import knx_conf as knx


yaml = YAML()


def add_knx_devices(devices, groups):
    lights = [
        # Dimmers
        DeviceInfo('Wohnzimmerlampe', 'Wohnzimmer Deckenlampe', 'living_area'),
        DeviceInfo('EsszimmerlampeWest', 'Esszimmer Deckenlampe West', 'living_area'),
        DeviceInfo('EsszimmerlampeMitte', 'Esszimmer Deckenlampe Mitte', 'living_area'),
        DeviceInfo("EsszimmerWandlampe", 'Esszimmer Schrankleuchte', 'living_area'),
        DeviceInfo("Küchenlampe", "Küche Deckenlampe", 'living_area'),
        DeviceInfo("AussenleuchteUntenSO", "Aussen Terassenlicht", 'outside'),
        DeviceInfo("Gang", "Gang Licht", 'hallway'),
        DeviceInfo("Bad", "Bad Licht", 'bathroom'),
        DeviceInfo("GangWindfang", "Gang Einganglicht", 'hallway'),
        DeviceInfo("LichtWaschküche", "Waschküche Licht", 'hallway'),
        # Normal lights
        DeviceInfo('AussenleuchteHaustüren', 'Haustür Licht', 'outside'),
        DeviceInfo('AussenleuchteObenNW', 'Haustür Licht NW', 'outside'),
        DeviceInfo('TreppenhausLicht', "Treppenhaus Licht", 'first_floor'),
        DeviceInfo('WCLicht', "WC Licht", 'other'),
        DeviceInfo('LampeVorratsraum', "Vorratsraum Licht", 'other'),
    ]

    shutters = [
        DeviceInfo('Wohnzimmer Fenster Rollo', 'Wohnzimmer Fenster Rollo', 'living_area'),
        DeviceInfo('Terassentür Rollo', 'Wohnzimmer Terrassentür Rollo', 'living_area'),
        DeviceInfo('Küchenfenster Rollo', 'Küche Fenster Rollo', 'living_area'),
        DeviceInfo('Esszimmerfenster Rollo', 'Esszimmer Fenster Rollo', 'living_area'),
    ]

    switches = [
        # Bells
        DeviceInfo("KlingelOben", "Klingel Oben", 'first_floor'),
        DeviceInfo("Klingel Innen", "Klingel Innentür", 'other'),
        DeviceInfo("Klingel Aussen", "Klingen Außentür", 'other'),
        # Bewegungsmelder LEDs
        DeviceInfo("BewegungsmelderMitte LED", "Bewegungsmelder Mitte LED", 'hallway'),
        DeviceInfo("BewegungsmelderWest LED", "Bewegungsmelder West LED", 'hallway'),
        DeviceInfo("BewegungsmelderOst LED", "Bewegungsmelder Ost LED", 'hallway'),
    ]
    scene_button_names = ['ObenLinks', 'ObenRechts', 'MitteLinks', 'MitteRechts', 'UntenLinks', 'UntenRechts']
    scene_button_names = [(i, e) for i, e in enumerate(scene_button_names)]
    switches += [DeviceInfo(f"SzeneEsszimmer{n}", f"Esszimmer Szene {i}") for i, n in scene_button_names]
    switches += [DeviceInfo(f"SzeneWohnzimmer{n}", f"Wohnzimmer Szene {i}") for i, n in scene_button_names]
    switches += [DeviceInfo(f"SzeneEingang{n}", f"Eingang Szene {i}") for i, n in scene_button_names[2:]]
    switches += [DeviceInfo(f"SzeneTerrassentuer{n}", f"Wohnzimmer Terrassentür Szene {i}")
                 for i, n in scene_button_names[2: 4]]

    power_plugs = [
        # Vorratsraum
        DeviceInfo("VorratsraumSteckdose1", "Vorratsraum Steckdose", 'other'),
        DeviceInfo("VorratsraumSteckdose2", "Vorratsraum Steckdose", 'other'),
        DeviceInfo("VorratsraumSteckdose3", "Gefrierschrank", 'other'),
        # Waschraum
        DeviceInfo("Trockner", "Trockner", 'other'),
        DeviceInfo("Waschmaschine", "Waschmaschine", 'other'),
        # Küche
        DeviceInfo("KücheSteckdose1", "Küche Steckdose 1"),
        DeviceInfo("KücheSteckdose2", "Küche Steckdose 2"),
        DeviceInfo("Spülmaschine", "Spülmaschine", 'living_area'),
        DeviceInfo("Backofen", "Backofen", 'living_area'),
        DeviceInfo("HerdP1", "Herd Phase 1", 'living_area'),
        DeviceInfo("HerdP2", "Herd Phase 2", 'living_area'),
        DeviceInfo("HerdP3", "Herd Phase 3", 'living_area'),
        # Rest
        DeviceInfo("ArbeitszimmerSteckdose", "Arbeitszimmer Steckdose", 'office_martin'),
        DeviceInfo("WohnzimmerSteckdose1", "Wohnzimmer Steckdose 1", 'living_area'),
        DeviceInfo("WohnzimmerSteckdose2", "Wohnzimmer Steckdose 2", 'living_area'),
    ]

    imported_csv = knx.import_ets5_csv_file('knx_data/export_project1.csv')
    imported_csv.update(knx.import_ets5_csv_file('knx_data/export_project2.csv'))

    knx.extent(devices, knx.create_lights(lights, imported_csv))
    knx.extent(devices, knx.create_shutters(shutters, imported_csv))
    knx.extent(devices, knx.create_switches(switches, imported_csv))
    knx.extent(devices, knx.create_power_plug(power_plugs, imported_csv))

    for device_type, devices in [('light', lights), ('cover', shutters), ('switch', switches)]:
        for device in devices:
            add_to_group(groups, device.groups, device.display_name, device_type)


def add_fhem_devices(devices, groups):
    fhem_yaml = yaml.load(open('fhem.yaml'))
    for device_type, device_list in fhem_yaml.items():
        if device_type not in devices:
            devices[device_type] = []
        for device in device_list:
            device['platform'] = 'fhem'
            if 'groups' in device:
                add_to_group(groups, device['groups'], device['name'], device_type)
                del device['groups']
            devices[device_type].append(device)


def main():
    group_dict = yaml.load(open('groups.yaml'))

    all_devices = {}
    add_knx_devices(all_devices, group_dict)
    add_fhem_devices(all_devices, group_dict)

    with open('output/configuration.yaml', 'w') as output:
        output.write(open('manual_config.yaml', 'r').read())
        yaml.dump(all_devices, output)
    yaml.dump(group_dict, open('output/groups.yaml', 'w'))


if __name__ == '__main__':
    main()
