import click
from miio.miioprotocol import MiIOProtocol

from flandre.config import C


def c1():
    return MiIOProtocol(C.switch1_ip, C.switch1_token)


def c2():
    return MiIOProtocol(C.switch2_ip, C.switch2_token)


def c1_set_connect(status: bool):
    c1().send('set_properties', [{'did': 'MYDID', 'siid': 2, 'piid': 1, 'value': status}])


def c1_connect():
    c1_set_connect(True)


def c1_disconnect():
    c1_set_connect(False)


def c1_connected():
    return c1().send('get_properties', [{'did': 'MYDID', 'siid': 2, 'piid': 1}])[0]['value']


def c1_power():
    return c1().send('get_properties', [{'did': '845778715', 'siid': 11, 'piid': 2}])[0]['value']


def c2_set_connect(status: bool):
    c2().send('set_properties', [{'did': 'MYDID', 'siid': 2, 'piid': 1, 'value': status}])


def c2_connect():
    c2_set_connect(True)


def c2_disconnect():
    c2_set_connect(False)


def c2_connected():
    return c2().send('get_properties', [{'did': 'MYDID', 'siid': 2, 'piid': 1}])[0]['value']


@click.group()
def cli():
    pass


@cli.command()
@click.argument('name')
def on(name):
    if name == 'c1':
        c1_connect()
    if name == 'c2':
        c2_connect()


@cli.command()
@click.argument('name')
def off(name):
    if name == 'c1':
        c1_disconnect()
    if name == 'c2':
        c2_disconnect()


@cli.command()
@click.argument('name')
def status(name):
    if name == 'c1':
        print(c1_connected())
        print(c1_power())
    if name == 'c2':
        print(c2_connected())


if __name__ == '__main__':
    cli()
