from .s1_title import *
from .s2_junctions import *
from .s3_reservoirs import *
from .s4_tanks import *
from .s5_pipes import *
from .s6_pumps import *
from .s7_valves import *
from .s8_tags import *
from .s9_demands import *
from .s10_status import *
from .s11_patterns import *
from .s12_curves import *
from .s13_controls import *
from .s14_rules import *
from .s15_energy import *
from .s16_emitters import *
from .s17_quality import *
from .s18_sources import *
from .s19_reactions import *
from .s20_mixing import *
from .s21_times import *
from .s22_report import *
from .s23_options import *
from .s24_coordinates import *
from .s25_vertices import *
from .s26_labels import *
from .s27_backdrop import *
from .s28_end import *


_s1_title = 'title'
_s2_junction = 'junction'
_s3_reservoir = 'reservoir'
_s4_tank = 'tank'
_s5_pipe = 'pipe'
_s6_pump = 'pump'
_s7_valve = 'valve'
_s8_tag = 'tag'
_s9_demand = 'demand'
_s10_status = 'status'
_s11_pattern = 'patten'
_s12_curve = 'curve'
_s13_control = 'control'
_s14_rule = 'rule'
_s15_energy = 'energy'
_s15_pump_energy = 'pump_energy'
_s16_emitter = 'emitter'
_s17_quality = 'quality'
_s18_source = 'source'
_s19_reaction = 'reaction'
_s19_pipe_reaction = 'pipe_reaction'
_s19_tank_reaction = 'tank_reaction'
_s20_mixing = 'mixing'
_s21_time = 'time'
_s22_report = 'report'
_s23_option = 'option'
_s24_coordinate = 'coordinate'
_s25_vertex = 'vertex'
_s26_label = 'label'
_s27_backdrop = 'backdrop'
_s28_end = 'end'


def execute_add_command(name: str, cs: ChangeSet) -> ChangeSet:
    type = cs.operations[0]['type']

    if type == _s1_title:
        return ChangeSet()
    if type == _s2_junction:
        return add_junction(name, cs)
    elif type == _s3_reservoir:
        return add_reservoir(name, cs)
    elif type == _s4_tank:
        return add_tank(name, cs)
    elif type == _s5_pipe:
        return add_pipe(name, cs)
    elif type == _s6_pump:
        return add_pump(name, cs)
    elif type == _s7_valve:
        return add_valve(name, cs)
    elif type == _s8_tag:
        return ChangeSet()
    elif type == _s9_demand:
        return ChangeSet()
    elif type == _s10_status:
        return ChangeSet()
    elif type == _s11_pattern:
        return add_pattern(name, cs)
    elif type == _s12_curve:
        return add_curve(name, cs)
    elif type == _s13_control:
        return ChangeSet()
    elif type == _s14_rule:
        return ChangeSet()
    elif type == _s15_energy:
        return ChangeSet()
    elif type == _s15_pump_energy:
        return ChangeSet()
    elif type == _s16_emitter:
        return ChangeSet()
    elif type == _s17_quality:
        return ChangeSet()
    elif type == _s18_source:
        return add_source(name, cs)
    elif type == _s19_reaction:
        return ChangeSet()
    elif type == _s19_pipe_reaction:
        return ChangeSet()
    elif type == _s19_tank_reaction:
        return ChangeSet()
    elif type == _s20_mixing:
        return add_mixing(name, cs)
    elif type == _s21_time:
        return ChangeSet()
    elif type == _s22_report:
        return ChangeSet()
    elif type == _s23_option:
        return ChangeSet()
    elif type == _s24_coordinate:
        return ChangeSet()
    elif type == _s25_vertex:
        return add_vertex(name, cs)
    elif type == _s26_label:
        return add_label(name, cs)
    elif type == _s27_backdrop:
        return ChangeSet()
    elif type == _s28_end:
        return ChangeSet()

    return ChangeSet()


def execute_update_command(name: str, cs: ChangeSet) -> ChangeSet:
    type = cs.operations[0]['type']

    if type == _s1_title:
        return set_title(name, cs)
    if type == _s2_junction:
        return set_junction(name, cs)
    elif type == _s3_reservoir:
        return set_reservoir(name, cs)
    elif type == _s4_tank:
        return set_tank(name, cs)
    elif type == _s5_pipe:
        return set_pipe(name, cs)
    elif type == _s6_pump:
        return set_pump(name, cs)
    elif type == _s7_valve:
        return set_valve(name, cs)
    elif type == _s8_tag:
        return set_tag(name, cs)
    elif type == _s9_demand: # exception, batch command ...
        return ChangeSet()
    elif type == _s10_status:
        return set_status(name, cs)
    elif type == _s11_pattern:
        return set_pattern(name, cs)
    elif type == _s12_curve:
        return set_curve(name, cs)
    elif type == _s13_control:
        return set_control(name, cs)
    elif type == _s14_rule:
        return set_rule(name, cs)
    elif type == _s15_energy:
        return set_energy(name, cs)
    elif type == _s15_pump_energy:
        return set_pump_energy(name, cs)
    elif type == _s16_emitter:
        return set_emitter(name, cs)
    elif type == _s17_quality:
        return set_quality(name, cs)
    elif type == _s18_source:
        return set_source(name, cs)
    elif type == _s19_reaction:
        return set_reaction(name, cs)
    elif type == _s19_pipe_reaction:
        return set_pipe_reaction(name, cs)
    elif type == _s19_tank_reaction:
        return set_tank_reaction(name, cs)
    elif type == _s20_mixing:
        return set_mixing(name, cs)
    elif type == _s21_time:
        return set_time(name, cs)
    elif type == _s22_report: # no api now
        return ChangeSet()
    elif type == _s23_option:
        return set_option(name, cs)
    elif type == _s24_coordinate: # do not support update here
        return ChangeSet()
    elif type == _s25_vertex:
        return set_vertex(name, cs)
    elif type == _s26_label:
        return set_label(name, cs)
    elif type == _s27_backdrop:
        return set_backdrop(name, cs)
    elif type == _s28_end: # end
        return ChangeSet()

    return ChangeSet()


def execute_delete_command(name: str, cs: ChangeSet) -> ChangeSet:
    type = cs.operations[0]['type']

    if type == _s1_title:
        return ChangeSet()
    if type == _s2_junction:
        return delete_junction(name, cs)
    elif type == _s3_reservoir:
        return delete_reservoir(name, cs)
    elif type == _s4_tank:
        return delete_tank(name, cs)
    elif type == _s5_pipe:
        return delete_pipe(name, cs)
    elif type == _s6_pump:
        return delete_pump(name, cs)
    elif type == _s7_valve:
        return delete_valve(name, cs)
    elif type == _s8_tag:
        return ChangeSet()
    elif type == _s9_demand:
        return ChangeSet()
    elif type == _s10_status:
        return ChangeSet()
    elif type == _s11_pattern:
        return delete_pattern(name, cs)
    elif type == _s12_curve:
        return delete_curve(name, cs)
    elif type == _s13_control:
        return ChangeSet()
    elif type == _s14_rule:
        return ChangeSet()
    elif type == _s15_energy:
        return ChangeSet()
    elif type == _s15_pump_energy:
        return ChangeSet()
    elif type == _s16_emitter:
        return ChangeSet()
    elif type == _s17_quality:
        return ChangeSet()
    elif type == _s18_source:
        return delete_source(name, cs)
    elif type == _s19_reaction:
        return ChangeSet()
    elif type == _s19_pipe_reaction:
        return ChangeSet()
    elif type == _s19_tank_reaction:
        return ChangeSet()
    elif type == _s20_mixing:
        return delete_mixing(name, cs)
    elif type == _s21_time:
        return ChangeSet()
    elif type == _s22_report:
        return ChangeSet()
    elif type == _s23_option:
        return ChangeSet()
    elif type == _s24_coordinate:
        return ChangeSet()
    elif type == _s25_vertex:
        return delete_vertex(name, cs)
    elif type == _s26_label:
        return delete_label(name, cs)
    elif type == _s27_backdrop:
        return ChangeSet()
    elif type == _s28_end:
        return ChangeSet()

    return ChangeSet()


def execute_batch_commands(name: str, cs: ChangeSet) -> ChangeSet:
    result = ChangeSet()

    try:
        for op in cs.operations:
            operation = op['operation']
            if operation == API_ADD:
                result.merge(execute_add_command(name, ChangeSet(op)))
            elif operation == API_UPDATE:
                result.merge(execute_update_command(name, ChangeSet(op)))
            elif operation == API_DELETE:
                result.merge(execute_delete_command(name, ChangeSet(op)))
    except:
        pass

    return result


def cache_add_command(name: str, cs: ChangeSet) -> SqlChangeSet | None:
    type = cs.operations[0]['type']

    if type == _s1_title:
        return None
    if type == _s2_junction:
        return add_junction_cache(name, cs)
    elif type == _s3_reservoir:
        return add_reservoir_cache(name, cs)
    elif type == _s4_tank:
        return add_tank_cache(name, cs)
    elif type == _s5_pipe:
        return add_pipe_cache(name, cs)
    elif type == _s6_pump:
        return add_pump_cache(name, cs)
    elif type == _s7_valve:
        return add_valve_cache(name, cs)
    elif type == _s8_tag:
        return None
    elif type == _s9_demand:
        return None
    elif type == _s10_status:
        return None
    elif type == _s11_pattern:
        return add_pattern_cache(name, cs)
    elif type == _s12_curve:
        return add_curve_cache(name, cs)
    elif type == _s13_control:
        return None
    elif type == _s14_rule:
        return None
    elif type == _s15_energy:
        return None
    elif type == _s15_pump_energy:
        return None
    elif type == _s16_emitter:
        return None
    elif type == _s17_quality:
        return None
    elif type == _s18_source:
        return add_source_cache(name, cs)
    elif type == _s19_reaction:
        return None
    elif type == _s19_pipe_reaction:
        return None
    elif type == _s19_tank_reaction:
        return None
    elif type == _s20_mixing:
        return add_mixing_cache(name, cs)
    elif type == _s21_time:
        return None
    elif type == _s22_report:
        return None
    elif type == _s23_option:
        return None
    elif type == _s24_coordinate:
        return None
    elif type == _s25_vertex:
        return add_vertex_cache(name, cs)
    elif type == _s26_label:
        return add_label_cache(name, cs)
    elif type == _s27_backdrop:
        return None
    elif type == _s28_end:
        return None

    return None


def cache_update_command(name: str, cs: ChangeSet) -> SqlChangeSet | None:
    type = cs.operations[0]['type']

    if type == _s1_title:
        return set_title_cache(name, cs)
    if type == _s2_junction:
        return set_junction_cache(name, cs)
    elif type == _s3_reservoir:
        return set_reservoir_cache(name, cs)
    elif type == _s4_tank:
        return set_tank_cache(name, cs)
    elif type == _s5_pipe:
        return set_pipe_cache(name, cs)
    elif type == _s6_pump:
        return set_pump_cache(name, cs)
    elif type == _s7_valve:
        return set_valve_cache(name, cs)
    elif type == _s8_tag:
        return set_tag_cache(name, cs)
    elif type == _s9_demand: # exception, batch command ...
        return None
    elif type == _s10_status:
        return set_status_cache(name, cs)
    elif type == _s11_pattern:
        return set_pattern_cache(name, cs)
    elif type == _s12_curve:
        return set_curve_cache(name, cs)
    elif type == _s13_control:
        return set_control_cache(name, cs)
    elif type == _s14_rule:
        return set_rule_cache(name, cs)
    elif type == _s15_energy:
        return set_energy_cache(name, cs)
    elif type == _s15_pump_energy:
        return set_pump_energy_cache(name, cs)
    elif type == _s16_emitter:
        return set_emitter_cache(name, cs)
    elif type == _s17_quality:
        return set_quality_cache(name, cs)
    elif type == _s18_source:
        return set_source_cache(name, cs)
    elif type == _s19_reaction:
        return set_reaction_cache(name, cs)
    elif type == _s19_pipe_reaction:
        return set_pipe_reaction_cache(name, cs)
    elif type == _s19_tank_reaction:
        return set_tank_reaction_cache(name, cs)
    elif type == _s20_mixing:
        return set_mixing_cache(name, cs)
    elif type == _s21_time:
        return set_time_cache(name, cs)
    elif type == _s22_report: # no api now
        return None
    elif type == _s23_option:
        return set_option_cache(name, cs)
    elif type == _s24_coordinate: # do not support update here
        return None
    elif type == _s25_vertex:
        return set_vertex_cache(name, cs)
    elif type == _s26_label:
        return set_label_cache(name, cs)
    elif type == _s27_backdrop:
        return set_backdrop_cache(name, cs)
    elif type == _s28_end: # end
        return None

    return None


def cache_delete_command(name: str, cs: ChangeSet) -> SqlChangeSet | None:
    type = cs.operations[0]['type']

    if type == _s1_title:
        return None
    if type == _s2_junction:
        return delete_junction_cache(name, cs)
    elif type == _s3_reservoir:
        return delete_reservoir_cache(name, cs)
    elif type == _s4_tank:
        return delete_tank_cache(name, cs)
    elif type == _s5_pipe:
        return delete_pipe_cache(name, cs)
    elif type == _s6_pump:
        return delete_pump_cache(name, cs)
    elif type == _s7_valve:
        return delete_valve_cache(name, cs)
    elif type == _s8_tag:
        return None
    elif type == _s9_demand:
        return None
    elif type == _s10_status:
        return None
    elif type == _s11_pattern:
        return delete_pattern_cache(name, cs)
    elif type == _s12_curve:
        return delete_curve_cache(name, cs)
    elif type == _s13_control:
        return None
    elif type == _s14_rule:
        return None
    elif type == _s15_energy:
        return None
    elif type == _s15_pump_energy:
        return None
    elif type == _s16_emitter:
        return None
    elif type == _s17_quality:
        return None
    elif type == _s18_source:
        return delete_source_cache(name, cs)
    elif type == _s19_reaction:
        return None
    elif type == _s19_pipe_reaction:
        return None
    elif type == _s19_tank_reaction:
        return None
    elif type == _s20_mixing:
        return delete_mixing_cache(name, cs)
    elif type == _s21_time:
        return None
    elif type == _s22_report:
        return None
    elif type == _s23_option:
        return None
    elif type == _s24_coordinate:
        return None
    elif type == _s25_vertex:
        return delete_vertex_cache(name, cs)
    elif type == _s26_label:
        return delete_label_cache(name, cs)
    elif type == _s27_backdrop:
        return None
    elif type == _s28_end:
        return None

    return None


def execute_batch_command(name: str, cs: ChangeSet) -> ChangeSet:
    redo_sql_s = []
    undo_sql_s = []
    redo_cs_s = []
    undo_cs_s = []

    try:
        for op in cs.operations:
            operation = op['operation']

            r = None

            if operation == API_ADD:
                r = cache_add_command(name, ChangeSet(op))
            elif operation == API_UPDATE:
                r = cache_update_command(name, ChangeSet(op))
                if op['type'] == 'demand':
                    r = set_demand_cache(name, ChangeSet(op))
            elif operation == API_DELETE:
                r = cache_delete_command(name, ChangeSet(op))

            if r == None:
                return ChangeSet()

            if op['type'] == 'demand':
                redo_sql_s.append(r.redo_sql)
                undo_sql_s.append(r.undo_sql)
                for d in r.redo_cs:
                    redo_cs_s.append(d)
                for d in r.undo_cs.reverse(): # need reverse again...
                    undo_cs_s.append(r.undo_cs)
            else:
                redo_sql_s.append(r.redo_sql)
                undo_sql_s.append(r.undo_sql)
                redo_cs_s.append(r.redo_cs)
                undo_cs_s.append(r.undo_cs)

    except:
        pass

    redo_sql = '\n'.join(redo_sql_s)

    undo_sql_s.reverse()
    undo_sql = '\n'.join(undo_sql_s)

    undo_cs_s.reverse()

    try:
        return execute_batch(name, redo_sql, undo_sql, redo_cs_s, undo_cs_s)
    except:
        return ChangeSet()
