#!/usr/bin/env python3

import csv
import os

# If abs is True, we need to have observed more than 32 connections on
# some day to be considered blocked.
def is_blocked (harshness, bridge_ips_max, bridge_ips_today, abs, no_min):
    if bridge_ips_max is None:
        return False

    threshold = 8 * harshness

    if abs:
        # absolute threshold
        return bridge_ips_today <= threshold and (no_min or bridge_ips_max > 32)
    else:
        # relative threshold based on prior connection counts
        return bridge_ips_today <= threshold and (no_min or bridge_ips_today <= bridge_ips_max - 32 + threshold)

def evaluate (harshness, fingerprint, abs=True, no_min=False):
    fingerprint = fingerprint.upper()
    bridge_ips_max = None

    with open (f"data/bridge_data_cleaned/{fingerprint}", 'r') as file:
        bridge_data = csv.reader(file, delimiter=',')

        for row in bridge_data:
            bridge_ips_today = int(row[1])

            if not bridge_ips_max is None:
                if is_blocked (harshness, bridge_ips_max, bridge_ips_today, abs, no_min):
                    return row[0]

            # Start bridge_ips_max only when we have a non-zero
            # connection count
            elif bridge_ips_today > 0:
                bridge_ips_max = bridge_ips_today

        # If we got here, the bridge is not blocked
        return None

# Remove any previous blocked_* files, start over
for i in range(5):
    if os.path.exists (f"data/blocked_{i}"):
        os.remove (f"data/blocked_{i}")
    if os.path.exists (f"data/blocked_{i}_abs"):
        os.remove (f"data/blocked_{i}_abs")
    if os.path.exists (f"data/blocked_{i}_nomin"):
        os.remove (f"data/blocked_{i}_nomin")

with open ("data/all-bridges", 'r') as all_bridges:
    for fingerprint in all_bridges:
        fingerprint = fingerprint.strip()
        if fingerprint:
            # Go through all harshness values
            for harshness in range(5):
                blocked = evaluate (harshness, fingerprint, False)
                # If the bridge is blocked add its fingerprint and
                # blocked date to the list for that harshness level
                if not blocked is None:
                    with open (f"data/blocked_{harshness}", 'a') as f:
                        f.write(f"{fingerprint},{blocked}\n")

                blocked = evaluate (harshness, fingerprint, True)
                if not blocked is None:
                    with open (f"data/blocked_{harshness}_abs", 'a') as f:
                        f.write(f"{fingerprint},{blocked}\n")

                blocked = evaluate (harshness, fingerprint, False, True)
                if not blocked is None:
                    with open (f"data/blocked_{harshness}_nomin", 'a') as f:
                        f.write(f"{fingerprint},{blocked}\n")
