#!/usr/bin/env python3

import argparse
import os
import glob
import json
import numpy as np
import matplotlib.pyplot as plt
import configparser
from util import script as scriptUtils

def main():
    args = __parseArguments()
    
    __stats(args["comparisonDir"], args["outputDir"])


def __parseArguments():
    parser = scriptUtils.ArgParser()
    
    parser.addInstanceDirArg()
    
    parser.addArg(alias="comparisonDir", shortFlag="c", longFlag="comparison_dir",
                  help="the direcotry with all comparison files", type=str)
    
    parser.addArg(alias="outputDir", shortFlag="s", longFlag="comparison_stats_dir",
                  help="Directory to store the stats", type=str)
                  
    arguments = parser.parse()
    
    arguments["datasetDir"] = os.path.abspath(arguments["datasetDir"])
    
    arguments["comparisonDir"] = os.path.join(arguments["datasetDir"],
                                              arguments["comparisonDir"])
                                              
    arguments["outputDir"] = os.path.join(arguments["datasetDir"],
                                          arguments["outputDir"])
    
    return arguments

def __stats(comparisonDir, outputDir):
    runs = glob.glob(os.path.join(comparisonDir, "run*"))
    
    for run in runs:
        
        stats = __collectStats(run)
    
        runOutputDir = os.path.join(outputDir, os.path.basename(run))
        
        __writeStats(stats, runOutputDir)
    
def __collectStats(comparisonDir):
    files = glob.glob(os.path.join(comparisonDir, "*.cmp"))
    
    stats = {}
    stats["match"] = 0
    stats["false_positive"] = 0
    stats["false_negative"] = 0
    stats["unsat"] = 0
    
    for path in files:
        
        comparison = __readComparison(path)
        
        minisat_satisfiable = comparison["minisat_satisfiable"]
        qubo_satisfiable = comparison["qubo_satisfiable"]
        
        if minisat_satisfiable == qubo_satisfiable:
            stats["match"] += 1
        elif minisat_satisfiable == False and qubo_satisfiable == True:
            stats["false_positive"] += 1
        elif minisat_satisfiable == True and qubo_satisfiable == False:
            stats["false_negative"] += 1
    
        if not minisat_satisfiable:
            stats["unsat"] += 1
            
    return stats
        
def __readComparison(path):
    cmpFile = open(path, "r")
    comparison = json.load(cmpFile)
    cmpFile.close()
    
    return comparison
    
    
def __writeStats(stats, outputDir):
    if not os.path.exists(outputDir):
        os.makedirs(outputDir)
    
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    numInstances = stats["match"] + stats["false_negative"] + stats["false_positive"]
    
    matchBar = ax.bar(x=0, height=stats["match"])
    
    falsePositiveBar = ax.bar(x=1, height=stats["false_positive"])
    falseNegativeBar = ax.bar(x=1,
                               height=stats["false_negative"],
                               bottom=stats["false_positive"])
                               
    ax.axhline(y=stats["match"], linestyle="--", color="gray")
    ax.axhline(y=stats["false_negative"], linestyle="--", color="gray")
    
    
    plt.ylabel("SAT Instanzen")
    plt.title("Verlgeich Minisat / WMIS qubo mit qbsolv")
    plt.xticks([0, 1], ("Gleiches Ergebnis", "Unterschiedliches Ergebnis"))
    plt.yticks([0, stats["match"], stats["false_negative"], numInstances])
    plt.legend((matchBar, falsePositiveBar, falseNegativeBar),
               ("Gleiches Ergebnis",
                "False Positive",
                "False Negative"))
    
    plt.savefig(os.path.join(outputDir, "stats.png"))
    
if __name__ == "__main__":
    main()
