import os
from .project import *
from .database import ChangeSet
from .sections import *
from .s1_title import inp_out_title
from .s2_junctions import inp_out_junction
from .s3_reservoirs import inp_out_reservoir
from .s4_tanks import inp_out_tank
from .s5_pipes import inp_out_pipe
from .s6_pumps import inp_out_pump
from .s7_valves import inp_out_valve
from .s8_tags import inp_out_tag
from .s9_demands import inp_out_demand
from .s10_status import inp_out_status
from .s11_patterns import inp_out_pattern, inp_out_pattern_v3
from .s12_curves import inp_out_curve, inp_out_curve_v3
from .s13_controls import inp_out_control
from .s14_rules import inp_out_rule
from .s15_energy import inp_out_energy
from .s16_emitters import inp_out_emitter
from .s17_quality import inp_out_quality
from .s18_sources import inp_out_source
from .s19_reactions import inp_out_reaction
from .s20_mixing import inp_out_mixing
from .s21_times import inp_out_time
from .s22_report import inp_out_report
from .s23_options import inp_out_option
from .s23_options_v3 import inp_out_option_v3
from .s24_coordinates import inp_out_coord
from .s25_vertices import inp_out_vertex
from .s26_labels import inp_out_label
from .s27_backdrop import inp_out_backdrop
#from .s28_end import *


def dump_inp(project: str, inp: str, version: str = '3'):
    if version != '3' and version != '2':
        version = '2'

    if not have_project(project):
       return

    project_open = is_project_open(project)

    if not project_open:
       open_project(project)

    dir = os.getcwd()
    path = os.path.join(dir, inp)

    if os.path.exists(path):
        os.remove(path)

    file = open(path, mode='w',encoding="UTF-8")

    # REGION, BOUND, REGION_NODES 在 epanet v2 中没有，是我们自己定制的
    # v2 需要去掉我们自己定制的 section
    sections = section_names_for_epanetv2
    if version == '3':
        sections = section_name

    for name in sections:
        if name == TITLE:
            file.write(f'[{name}]\n')
        else:
            file.write(f'\n[{name}]\n')

        if name == TITLE:
            file.write('\n'.join(inp_out_title(project)))

        elif name == JUNCTIONS: # + coords
            file.write('\n'.join(inp_out_junction(project)))

        elif name == RESERVOIRS: # + coords
            file.write('\n'.join(inp_out_reservoir(project)))

        elif name == TANKS: # + coords
            file.write('\n'.join(inp_out_tank(project)))

        elif name == PIPES:
            file.write('\n'.join(inp_out_pipe(project)))

        elif name == PUMPS:
            file.write('\n'.join(inp_out_pump(project)))

        elif name == VALVES:
            file.write('\n'.join(inp_out_valve(project)))

        elif name == TAGS:
            file.write('\n'.join(inp_out_tag(project)))

        elif name == DEMANDS:
            file.write('\n'.join(inp_out_demand(project)))

        elif name == STATUS:
            file.write('\n'.join(inp_out_status(project)))

        elif name == PATTERNS:
            if version == '3':
                file.write('\n'.join(inp_out_pattern_v3(project)))
            else:
                file.write('\n'.join(inp_out_pattern(project)))

        elif name == CURVES:
            if version == '3':
                file.write('\n'.join(inp_out_curve_v3(project)))
            else:
                file.write('\n'.join(inp_out_curve(project)))

        elif name == CONTROLS:
            file.write('\n'.join(inp_out_control(project)))

        elif name == RULES:
            file.write('\n'.join(inp_out_rule(project)))

        elif name == ENERGY:
            file.write('\n'.join(inp_out_energy(project)))

        elif name == EMITTERS:
            file.write('\n'.join(inp_out_emitter(project)))

        elif name == QUALITY:
            file.write('\n'.join(inp_out_quality(project)))

        elif name == SOURCES:
            file.write('\n'.join(inp_out_source(project)))

        elif name == REACTIONS:
            file.write('\n'.join(inp_out_reaction(project)))

        elif name == MIXING:
            file.write('\n'.join(inp_out_mixing(project)))

        elif name == TIMES:
            file.write('\n'.join(inp_out_time(project)))

        elif name == REPORT:
            file.write('\n'.join(inp_out_report(project)))

        elif name == OPTIONS:
            if version == '3':
                file.write('\n'.join(inp_out_option_v3(project)))
            else:
                file.write('\n'.join(inp_out_option(project)))

        elif name == COORDINATES:
            file.write('\n'.join(inp_out_coord(project)))

        elif name == VERTICES:
            file.write('\n'.join(inp_out_vertex(project)))

        elif name == LABELS:
            file.write('\n'.join(inp_out_label(project)))

        elif name == BACKDROP:
            file.write('\n'.join(inp_out_backdrop(project)))

        elif name == END:
            pass # :)

        file.write('\n')

    file.close()

    if not project_open:
        close_project(project)


def export_inp(project: str, version: str = '3') -> ChangeSet:
    if version != '3' and version != '2':
        version = '2'

    if not have_project(project):
       return ChangeSet()

    project_open = is_project_open(project)

    if not project_open:
       open_project(project)

    inp = ''

    for name in section_name:
        if name == TITLE:
            inp += f'[{name}]\n'
        else:
            inp += f'\n[{name}]\n'

        if name == TITLE:
            inp += '\n'.join(inp_out_title(project))

        elif name == JUNCTIONS: # + coords
            inp += '\n'.join(inp_out_junction(project))

        elif name == RESERVOIRS: # + coords
            inp += '\n'.join(inp_out_reservoir(project))

        elif name == TANKS: # + coords
            inp += '\n'.join(inp_out_tank(project))

        elif name == PIPES:
            inp += '\n'.join(inp_out_pipe(project))

        elif name == PUMPS:
            inp += '\n'.join(inp_out_pump(project))

        elif name == VALVES:
            inp += '\n'.join(inp_out_valve(project))

        elif name == TAGS:
            inp += '\n'.join(inp_out_tag(project))

        elif name == DEMANDS:
            inp += '\n'.join(inp_out_demand(project))

        elif name == STATUS:
            inp += '\n'.join(inp_out_status(project))

        elif name == PATTERNS:
            if version == '3':
                inp += '\n'.join(inp_out_pattern_v3(project))
            else:
                inp += '\n'.join(inp_out_pattern(project))

        elif name == CURVES:
            if version == '3':
                inp += '\n'.join(inp_out_curve_v3(project))
            else:
                inp += '\n'.join(inp_out_curve(project))

        elif name == CONTROLS:
            inp += '\n'.join(inp_out_control(project))

        elif name == RULES:
            inp += '\n'.join(inp_out_rule(project))

        elif name == ENERGY:
            inp += '\n'.join(inp_out_energy(project))

        elif name == EMITTERS:
            inp += '\n'.join(inp_out_emitter(project))

        elif name == QUALITY:
            inp += '\n'.join(inp_out_quality(project))

        elif name == SOURCES:
            inp += '\n'.join(inp_out_source(project))

        elif name == REACTIONS:
            inp += '\n'.join(inp_out_reaction(project))

        elif name == MIXING:
            inp += '\n'.join(inp_out_mixing(project))

        elif name == TIMES:
            inp += '\n'.join(inp_out_time(project))

        elif name == REPORT:
            inp += '\n'.join(inp_out_report(project))

        elif name == OPTIONS:
            if version == '3':
                inp += '\n'.join(inp_out_option_v3(project))
            else:
                inp += '\n'.join(inp_out_option(project))

        elif name == COORDINATES:
            inp += '\n'.join(inp_out_coord(project))

        elif name == VERTICES:
            inp += '\n'.join(inp_out_vertex(project))

        elif name == LABELS:
            inp += '\n'.join(inp_out_label(project))

        elif name == BACKDROP:
            inp += '\n'.join(inp_out_backdrop(project))

        elif name == END:
            pass # :)

        inp += '\n'

    if not project_open:
        close_project(project)

    return ChangeSet({'operation': 'export', 'inp': inp})
