'''
    DS2000
    Spring 2022
    Comparing runners -- who ran the most mileage in january 2022?
'''

import matplotlib.pyplot as plt

RUN_FILE = "runner_data.txt"

def plot_runner(x_values, y_values, name):
    ''' Function: plot_runner
        Parameters: one string that should be the x-values, list of floats to 
                      be the y values
        Returns: nothing, just makes a plot
    '''
    x_values = x_values.split()[1:]
    int_x = [int(x) for x in x_values]
    plt.scatter(int_x, y_values, label = name)
    
    

def main():
   # Initialize max mileage to a reasonble default value
   max_mileage = -1
   max_runner = ""
    
   # Step one -- gather data from the file
   with open(RUN_FILE, "r") as infile:
        
        # Read in the date line but then just ignore it
        dates = infile.readline()
        
        # Use a while loop to read in the rest of the runners
        while True:
            runner = infile.readline()
            if runner == "":
                break
            
            # Make a list of strings for the data
            runner = runner.split()
            name = runner[0]
            miles = [float(run) for run in runner[2:]]
            
            # Plot the runner's data
            plot_runner(dates, miles, runner[0])
            
            # Get the current runner's total mileage
            total_mileage = sum(miles)
            
            #print("Looking at runner", name)
            if total_mileage > max_mileage:
                #print("Got a new max mileage of", total_mileage)
                max_mileage = total_mileage
                max_runner = name
            
        
   # Report who ran the most
   print("The highest-mileage runner was:", max_runner, "with",
          max_mileage, "miles!")
   plt.legend()
    
          
            
        
        
    
    
main()