from lxml import etree
from pathlib import Path
import shutil
import os
import sys

sys.path.append(os.path.abspath("ogsworkflowhelper/toy.py"))
from ogsworkflowhelper.arehs.reference_points import reference_points as ref_points
from ogsworkflowhelper.csv2prj import csv2pandas

configfile: "config/config.yaml"
container: config["container_serial"]

MESH_PARAMS="{mesh_type}-id_{layer_set_id}-xres_{x_res}"
TOY_PARAMS = "toy-id_{layer_set_id}-xres_{x_res}"
PRISM_PARAMS="prism-id_{layer_set_id}-xres_{x_res}"
VOXEL_PARAMS="voxel-id_{layer_set_id}-xres_{x_res}-yres_{y_res}-zres_{z_res}"
RANK = "{rank}D"
MODEL = "{mesh_name}"
MESH="{mesh_type}-id_{layer_set_id}-xres_{x_res}"
OUT_DIR = config['result_out_dir']
MSH_DIR = OUT_DIR + "mesh/"
SIM_DIR = OUT_DIR + "sim/"
MODEL_DIR = OUT_DIR + "model/"
PRJ_PATH = OUT_DIR + "prj/{rank}D/{mesh_name}_{mesh_type}-id_{layer_set_id}/{project}/{stage}"
PLOT_DIR = OUT_DIR + "plot/"
PARAM_TABLE="input/parameters/{mesh_name}.csv"
SET_TABLE="input/layer_sets/{mesh_name}.csv"

wildcard_constraints:
    rank="[2-3]",
    layer_set_id="[0-9]+",
    x_res="\d+\.?\d*",
    mesh_name="(Ton-Nord|Ton-Sued|Ton-Kristallin|Salz-Kissen|Salz-flach)",
    project="(simT|simT-phaseless|simH|simM" +
            "|simTH|simTH-freezing|simTH-phaseless|simHM|simHM-nopy" +
            "|simTHM|simTHM-freezing|simTHM-nopy|simTHM-sig0)",
    stage="(front|back|boundary|init|glaciation|glacialcycle)",
    boundary_stage="(front|back)",
    param_func="(temperature|pressure(_hydraulic-head|_dynamic)?" +
               "|velocity(_0|_1|_2|_log)?" +
               "|displacement(_0|_1|_2)?" +
               "|epsilon(_0|_1|_2|_3|_4|_5|_trace)?" +
               "|sigma(_0|_1|_2|_3|_4|_5|_von-Mises-stress|_effective-pressure|_qp-ratio)?)",
    model_folder="[2,3]D\/[^/]+\/[^/]+\/[^/]+",
    optional_parts="[\/parts_\d+]*",
    partition="[1-9]{1}\d{0,3}" #1-9999,  ToDo special treatment for part_1


def stage_type(wcs):
    if wcs.stage in ["front", "back"]:
        return "boundary"
    return wcs.stage


include: "rules/misc/checks.smk"
include: "rules/generic/common.smk"
include: "rules/generic/mesh.smk"
include: "rules/generic/sim.smk"
include: "rules/arehs/prj.smk"
include: "rules/arehs/mesh.smk"
include: "rules/arehs/model.smk"
include: "rules/arehs/sim.smk"
include: "rules/post_processing.smk"
include: "rules/arehs/final_report.smk"

# TODO: reduce iteration numbers in prj files

# empty line for fileending necessary!
