from .operation import *
from .s0_base import *


def get_pump_schema(name: str) -> dict[str, dict[str, Any]]:
    return { 'id'         : {'type': 'str'   , 'optional': False , 'readonly': True },
             'node1'      : {'type': "str"   , 'optional': False , 'readonly': False},
             'node2'      : {'type': "str"   , 'optional': False , 'readonly': False} }


def get_pump(name: str, id: str) -> dict[str, Any]:
    p = read(name, f"select * from pumps where id = '{id}'")
    d = {}
    d['id'] = str(p['id'])
    d['node1'] = str(p['node1'])
    d['node2'] = str(p['node2'])
    return d


class Pump(object):
    def __init__(self, input: dict[str, Any]) -> None:
        self.type = 'pump'
        self.id = str(input['id'])
        self.node1 = str(input['node1'])
        self.node2 = str(input['node2'])

        self.f_type = f"'{self.type}'"
        self.f_id = f"'{self.id}'"
        self.f_node1 = f"'{self.node1}'"
        self.f_node2 = f"'{self.node2}'"

    def as_dict(self) -> dict[str, Any]:
        return { 'type': self.type, 'id': self.id, 'node1': self.node1, 'node2': self.node2 }

    def as_id_dict(self) -> dict[str, Any]:
        return { 'type': self.type, 'id': self.id }


def set_pump(name: str, cs: ChangeSet) -> ChangeSet:
    old = Pump(get_pump(name, cs.operations[0]['id']))
    raw_new = get_pump(name, cs.operations[0]['id'])

    new_dict = cs.operations[0]
    schema = get_pump_schema(name)
    for key, value in schema.items():
        if key in new_dict and not value['readonly']:
            raw_new[key] = new_dict[key]
    new = Pump(raw_new)

    redo_sql = f"update pumps set node1 = {new.f_node1}, node2 = {new.f_node2} where id = {new.f_id};"
    undo_sql = f"update pumps set node1 = {old.f_node1}, node2 = {old.f_node2} where id = {old.f_id};"

    redo_cs = g_update_prefix | new.as_dict()
    undo_cs = g_update_prefix | old.as_dict()

    return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)


def add_pump(name: str, cs: ChangeSet) -> ChangeSet:
    new = Pump(cs.operations[0])

    redo_sql = f"insert into _link (id, type) values ({new.f_id}, {new.f_type});"
    redo_sql += f"\ninsert into pumps (id, node1, node2) values ({new.f_id}, {new.f_node1}, {new.f_node2});"

    undo_sql = f"delete from pumps where id = {new.f_id};"
    undo_sql += f"\ndelete from _link where id = {new.f_id};"

    redo_cs = g_add_prefix | new.as_dict()
    undo_cs = g_delete_prefix | new.as_id_dict()

    return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)


def delete_pump(name: str, cs: ChangeSet) -> ChangeSet:
    old = Pump(get_pump(name, cs.operations[0]['id']))

    redo_sql = f"delete from pumps where id = {old.f_id};"
    redo_sql += f"\ndelete from _link where id = {old.f_id};"

    undo_sql = f"insert into _link (id, type) values ({old.f_id}, {old.f_type});"
    undo_sql += f"\ninsert into pumps (id, node1, node2) values ({old.f_id}, {old.f_node1}, {old.f_node2});"

    redo_cs = g_delete_prefix | old.as_id_dict()
    undo_cs = g_add_prefix | old.as_dict()

    return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
