import t_statistics as t_stat
import binning as binn
import sys
import common as com
import all_printing as pr

def calculate_weights():
    print("Creating Weight Matrix based on the bins..")
    Weights = [[0 for x in range(t_stat.num_hpc)] for y in range(t_stat.num_indicator)]
    with open("base.txt", "r") as base:
        benign = base.readlines()
    with open("input.txt", "r") as tar:
        target = tar.readlines()
    total_count = 0
    loopcount = 0
    for r in range(t_stat.num_indicator):
        for c in range(t_stat.num_hpc):
            count = 0
            for b in benign:
                for t in target:
                    loopcount += 1
                    sys.stdout.write('\r')
                    sys.stdout.write("[%-20s] %d%%" % ('*'*int((20*loopcount)/(len(benign)*len(target)*t_stat.num_hpc*t_stat.num_indicator)), ((100*loopcount)/(len(benign)*len(target)*t_stat.num_hpc*t_stat.num_indicator))))
                    sys.stdout.flush()
                    b_name = b.split("/")[-1].split(".")[0]
                    t_name = t.split("/")[-1].split(".")[0]
                    data = com.load_matrix(binn.bin_directory+b_name+"_"+t_name+".txt")
                    if data[r][c] == 1:
                        count = count + 1
            Weights[r][c] = count
            total_count = total_count + count
    com.save_matrix("weights.txt", Weights)
    print("\n=============")
    print("Weight Matrix")
    print("=============")
    pr.print_matrix(Weights)
    for r in range(t_stat.num_indicator):
        for c in range(t_stat.num_hpc):
            Weights[r][c] = float(Weights[r][c]) / float(total_count)    
    com.save_matrix("normalized_weights.txt", Weights)
    print("==================")
    print("Sensitivity Matrix")
    print("==================")
    pr.print_matrix(Weights)

def predict(test_directory, t):
    with open("base.txt", "r") as base:
        benign = base.readlines()
    Count = [[0 for x in range(t_stat.num_hpc)] for y in range(t_stat.num_indicator)]
    t_name = t.split("/")[-1].split(".")[0]
    for r in range(t_stat.num_indicator):
        for c in range(t_stat.num_hpc):
            Count[r][c] = 0
            count = 0
            for b in benign:
                b_name = b.split("/")[-1].split(".")[0]
                data = com.load_matrix(binn.bin_directory+b_name+"_"+t_name+".txt")
                if data[r][c] == 1:
                    count = count + 1
            Count[r][c] = count
    com.save_matrix(test_directory+"count_"+t_name+".txt", Count)
    print("\n=============")
    print("Count Matrix")
    print("=============")
    pr.print_matrix(Count)
    for r in range(t_stat.num_indicator):
        for c in range(t_stat.num_hpc):
            Count[r][c] = float(Count[r][c]) / float(len(benign))
    com.save_matrix(test_directory+"normalized_count_"+t_name+".txt", Count)
    weight = com.load_matrix("normalized_weights.txt")
    result = [[i * j for i, j in zip(*row)] for row in zip(Count, weight)]
    score = sum(map(sum, result))
    print("============")
    print("Score Matrix")
    print("============")
    pr.print_matrix(result)
    com.save_matrix(test_directory+"score_"+t_name+".txt", result)
    print("\nScore (lambda) : %0.6f" % score)
    return 0 if score < com.threshold else 1