'''This script adds a type_label of type {high, medium, low} to a json file containing clusters.'''
import json
import statistics as stat


def print_min_max(layer_filename):
    with open(layer_filename, 'r') as layer_file:
        content = json.loads(layer_file.read())

    min_ = 1000000
    max_ = -1000000

    for entry in content:
        # print(entry['label'])
        if entry['label'] == 'noise':
            continue

        mean = stat.mean([float(e) for e in entry['label'].split('--')])

        cur_val = mean
        max_ = max(max_, cur_val)
        min_ = min(min_, cur_val)

    print(f"max: {max_}")
    print(f"min: {min_}")


def create_type_label(layer_filename, treshold1, treshold2):
    with open(layer_filename, 'r') as layer_file:
        content = json.loads(layer_file.read())

    for entry in content:
        if entry['label'] == 'noise':
            entry['type_label'] = 'noise'
            continue

        mean = stat.mean([float(e) for e in entry['label'].split('--')])

        if mean > treshold2:
            entry['type_label'] = 'high'
        elif mean > treshold1:
            entry['type_label'] = 'medium'
        else:
            entry['type_label'] = 'low'

    with open(layer_filename, 'w') as layer_file:
        layer_file.write(json.dumps(content))


# print_min_max("Solar_Production_Layer.json")
# create_type_label("Solar_Production_Layer.json",
#                   treshold1=1.4301666666666666, treshold2=2.860333333333333)

# print_min_max("User_Demand_Layer.json")
# create_type_label("User_Demand_Layer.json",
#                   treshold1=1.8306666666666667, treshold2=3.6608333333333336)


def check_values():
    with open("User_Demand_Layer.json", 'r') as layer_file:
        content = json.loads(layer_file.read())

    d = {'high': 0, 'medium': 0, 'low':0, 'noise':0}

    for e in content:
        d[e['type_label']] += 1
        # print(f"{e['label']} : {e['type_label']}")
    print(d)

check_values()