import csv
from boltons import statsutils

with open("jaako_generation.csv", "r") as csv_file:
    csv_reader = csv.reader(csv_file)
    next(csv_reader)

    bpm = []
    energy = []
    dance = []
    loud = []
    valence = []
    acoustic = []
    speech = []
    popularity = []

    def compare(x, y):
        result = int(x) - int(y)
        if result < 0:
            return f"{result:.2f} is lower than the average/mean"
        elif result > 0:
            return f"{result:.2f} is greater than the average/mean"
        else:
            return f"{result:.2f} the same with the average/mean"

    for line in csv_reader:
        num = [int(val) for val in line[4:]]

        bpm.append(num[0])
        bpmmean = statsutils.Stats(bpm).mean

        energy.append(num[1])
        energymean = statsutils.Stats(energy).mean

        dance.append(num[2])
        dancemean = statsutils.Stats(dance).mean

        loud.append(num[3])
        loudmean = statsutils.Stats(loud).mean

        valence.append(num[5])
        valencemean = statsutils.Stats(valence).mean

        acoustic.append(num[7])
        acousticmean = statsutils.Stats(acoustic).mean

        speech.append(num[8])
        speechmean = statsutils.Stats(speech).mean

        popularity.append(num[9])
        popularitymean = statsutils.Stats(popularity).mean

        print(f"[{line[1]}] - {line[2]}")
        print(f"Genre: {line[3]}")
        print(f"RESULTS:")
        print(
            f"Beats Per Minute: {float(line[4]):.2f}/{bpmmean:.2f} ({compare(float(line[4]), bpmmean)})"
        )
        print(
            f"Energy: {float(line[5]):.2f}/{energymean:.2f} ({compare(float(line[5]), energymean)})"
        )
        print(
            f"Danceability: {float(line[6]):.2f}/{dancemean:.2f} ({compare(float(line[6]), dancemean)})"
        )
        print(
            f"Loudness: {float(line[7]):.2f}/{loudmean:.2f} ({compare(float(line[7]), loudmean)})"
        )
        print(
            f"Valence: {float(line[9]):.2f}/{valencemean:.2f} ({compare(float(line[9]), valencemean)})"
        )
        print(
            f"Acousticness: {float(line[11]):.2f}/{acousticmean:.2f} ({compare(float(line[11]), acousticmean)})"
        )
        print(
            f"Speechiness: {float(line[12]):.2f}/{speechmean:.2f} ({compare(float(line[12]), speechmean)})"
        )
        print(
            f"Popularity: {float(line[13]):.2f}/{popularitymean:.2f} ({compare(float(line[12]), speechmean)})"
        )
        print("\n")
