import time


def dw_WriteHydFile(fname, d):
    """
    Generates a Delwaq *.hyd file.

    d is dict holding all the required data:
        - d['runid']  : current run id
        - d['tref']   : reference time of simulation as datetime
        - d['tstart'] : start time of simulation as datetime
        - d['tstop']  : stop time of simulation as datetime
        - d['tstep']  : timestep of simulation as timedelta
        - d['m']      : number of grid cells in 1st direction
        - d['n']      : number of grid cells in 2nd direction
    """

    def datetime2str(dt):
        return "{:04}{:02}{:02}{:02}{:02}{:02}".format(
            dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second
        )

    def timedelta2str(td):
        return "{:04}{:02}{:02}{}".format(
            0, 0, td.days, time.strftime("%H%M%S", time.gmtime(td.seconds))
        )

    buff = ""
    buff += "task      full-coupling\n"
    buff += "geometry  unstructured\n"
    buff += "horizontal-aggregation       no\n"
    buff += "minimum-vert-diffusion-used  no\n"
    buff += "vertical-diffusion           calculated\n"
    buff += "description\n"
    buff += "'%-60s'\n" % "Generated by Wflow"
    buff += "'%s'\n" % (" " * 60)
    buff += "'%s'\n" % (" " * 60)
    buff += "end-description\n"
    buff += "reference-time           '%s'\n" % (datetime2str(d["tref"]))
    buff += "hydrodynamic-start-time  '%s'\n" % (datetime2str(d["tstart"]))
    buff += "hydrodynamic-stop-time   '%s'\n" % (datetime2str(d["tstop"]))
    buff += "hydrodynamic-timestep    '%s'\n" % (timedelta2str(d["tstep"]))
    buff += "conversion-ref-time      '%s'\n" % (datetime2str(d["tref"]))
    buff += "conversion-start-time    '%s'\n" % (datetime2str(d["tstart"]))
    buff += "conversion-stop-time     '%s'\n" % (datetime2str(d["tstop"]))
    buff += "conversion-timestep      '%s'\n" % (timedelta2str(d["tstep"]))
    buff += "grid-cells-first-direction              %7i\n" % d["noseg"]
    buff += "grid-cells-second-direction             %7i\n" % 1
    buff += "number-hydrodynamic-layers              %7i\n" % 1
    buff += "number-horizontal-exchanges             %7i\n" % d["noqh"]
    buff += "number-vertical-exchanges               %7i\n" % d["noqv"]
    buff += "number-water-quality-segments-per-layer %7i\n" % d["nosegh"]
    buff += "number-water-quality-layers      1\n"
    buff += "hydrodynamic-file        none\n"
    buff += "aggregation-file         none\n"
    buff += "boundaries-file          '%s.bnd'\n" % d["runid"]
    buff += "waqgeom-file             '%s_waqgeom.nc'\n" % d["runid"]
    buff += "volumes-file             '%s.vol'\n" % d["runid"]
    buff += "areas-file               '%s.are'\n" % d["runid"]
    buff += "flows-file               '%s.flo'\n" % d["runid"]
    buff += "pointers-file            '%s.poi'\n" % d["runid"]
    buff += "lengths-file             '%s.len'\n" % d["runid"]
    buff += "salinity-file            none\n"
    buff += "temperature-file         none\n"
    buff += "vert-diffusion-file      none\n"
    buff += "horizontal-surfaces-file '%s.srf'\n" % d["runid"]
    buff += "depths-file              none\n"
    buff += "discharges-file          none\n"
    buff += "chezy-coefficients-file  none\n"
    buff += "shear-stresses-file      none\n"
    buff += "walking-discharges-file  none\n"
    buff += "attributes-file          '%s.atr'\n" % d["runid"]
    buff += "constant-dispersion\n"
    buff += "   first-direction    0.0000E+00\n"
    buff += "   second-direction   0.0000E+00\n"
    buff += "   third-direction    0.0000E+00\n"
    buff += "end-constant-dispersion\n"
    buff += "hydrodynamic-layers\n"
    buff += "          1.000\n"
    buff += "end-hydrodynamic-layers\n"
    buff += "water-quality-layers\n"
    buff += "        1.000\n"
    buff += "end-water-quality-layers\n"
    buff += "discharges\n"
    buff += "end-discharges\n"
    f = open(fname, "w")
    f.write(buff)
    f.close()