import datetime

# A .status file looks like this
# <librec-auto-status>
#    <message>Completed</message>
#    <exp-no>1</exp-no>\
#    <param><name>rec.neighbors.knn.number</name><value>30</value></param>
#    <date>June 28, 11:00 PM</date>
# </librec-auto-status>


class Status():

    _HEADER = '<?xml version="1.0"?>\n<!-- DO NOT EDIT. File automatically generated by librec-auto -->\n'
    _TEMPLATE_FRONT = '<librec-auto-status>\n<message>{}</message>\n<exp-no>{}</exp-no>\n<date>{}</date>\n'
    _TEMPLATE_END = '</librec-auto-status>\n'
    _TEMPLATE_LINE = '<param><name>{}</name><value>{}</value></param>\n'


    # Accept list of vars and tuples
    @staticmethod
    def save_status(msg, exp_count, var_names, value_tuple, config, paths):
        status_file = paths.get_path('status')
        status_front = Status._TEMPLATE_FRONT.format(msg, exp_count, datetime.datetime.now())

        status_params = ''

        for var, val in zip(var_names, value_tuple):
            status_params = status_params + Status._TEMPLATE_LINE.format(var, val)

        status_info = Status._HEADER + status_front + status_params + Status._TEMPLATE_END

        with status_file.open(mode='w') as fh:
            fh.write(str(status_info))
