import os
from metrics import anonymity_metrics
import pandas as pd
import json
from datetime import datetime
import reg_phase

import argparse

def main(conf):
    logging_dir = os.path.join(conf["logging"]["dir"],
                               datetime.utcnow().isoformat().replace(':', '_'))
    print(f"Logs are saved to {logging_dir}")
    os.makedirs(logging_dir, exist_ok=True)

    gr_start = reg_phase.run(logging_dir, conf)

    #packetLogsDir = os.path.join(logging_dir,'packet_log.csv')
    #packetLogs = pd.read_csv(packetLogsDir, delimiter=';')
    messageLogsDir = os.path.join(logging_dir,'message_log.csv')
    msgLogs = pd.read_csv(messageLogsDir, delimiter=';')
    latency = anonymity_metrics.computeAverageE2ELatency(msgLogs, gr_start)

    print("-------- Performance metrics --------")
    print(">> Overall latency: %f seconds" % (latency))
    print("--------------------------------------------")

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('filename')
    args = parser.parse_args()
    with open(args.filename) as f:
        conf = json.load(f)
    conf["packet"] = {"packet_size" : 0}
    conf["misc"] = {
        "id_len": 4,
        "num_target_packets": 1
    }
    conf["message"] = {
        "min_msg_size": 2,
        "max_msg_size": 2
    }
    conf["clients"]["sim_add_buffer"] = 0.0
    conf["clients"]["cover_traffic"] = False
    conf["clients"]["cover_traffic_rate"] = 1.0
    conf["clients"]["dummies_acks"] = False
    conf["clients"]["ACK"] = False
    conf["mixnodes"]["batch"] = True
    conf["mixnodes"]["avg_delay"] = 0.1
    main(conf)