# -*- coding: UTF-8 -*-
"""PyPoll Homework Challenge Solution."""

# Add our dependencies
import csv
import os

# Add a variable to load a file from a path
file_to_load = os.path.join(".", "Resources", "election_results.csv")
# Add a variable to save the file to a path
file_to_save = os.path.join("analysis", "election_analysis.txt")

# Initialize a total vote counter
total_votes = 0

# Candidate Options and candidate votes
# List of candidates
candidate_options = []
# Dictionary linking candidates to votes
candidate_votes = {}

# 1: Create a county list and county votes dictionary
# List of counties
county = []
# Dictionary linking counties to votes
county_votes = {}

# Track the winning candidate, vote count and percentage
# This variable will hold an empty string value for the winning candidate
winning_candidate = ""
winning_count = 0
winning_percentage = 0

# 2: Track the largest county and county voter turnout
# This variable will hold an empty string value for the county with the largest voter turnout
largest_turnout = ""
winning_turnout = 0

# Read the csv and convert it into a list of dictionaries
# Open the election results and read the file
with open(file_to_load) as election_data:
    
    #Read the file object with the reader function
    reader = csv.reader(election_data)

    # Skip the first row (headers) in "election_results.csv" and go to the next item
    header = next(reader)

    # For each row in election_results.csv
    for row in reader:

        # Add to the total vote count
        total_votes = total_votes + 1

        # Get the candidate name from each row
        candidate_name = row[2]

        # 3: Extract the county name from each row
        county_name = row[1]

        # If the candidate does not match any existing candidate, add it to
        # the candidate list
        if candidate_name not in candidate_options:

            # Add the candidate name to the candidate list
            candidate_options.append(candidate_name)

            # And begin tracking that candidate's voter count
            candidate_votes[candidate_name] = 0

        # Add a vote to that candidate's count
        candidate_votes[candidate_name] += 1

        # 4a: Write an if statement that checks that the county
        # does not match any existing county in the county list
        if county_name not in county:

            # 4b: Add the existing county to the list of counties if it is not
            # in the county list
            county.append(county_name)

            # 4c: Begin tracking the county's vote count
            county_votes[county_name] = 0

        # 5: Add a vote to that county's vote count
        county_votes[county_name] += 1

# Save the results to our text file
with open(file_to_save, "w") as txt_file:

    # Print the final vote count (to terminal)
    # Election results will have four variables written to it
    election_results = (
        f"\nElection Results\n"
        f"-------------------------\n"
        f"Total Votes: {total_votes:,}\n"
        f"-------------------------\n\n"
        f"County Votes:\n"
    )
    # election_results will with the parameter end="", which is equal to an empty string
    print(election_results, end="")

    # Save the final vote count to the text file
    txt_file.write(election_results)

    # 6a: Write a for loop to get the county from the county dictionary
    for county_name in county:

        # 6b: Retrieve the county vote count 
        # This variable will hold county votes as they are retrieved from the county 
        # vote dictionary
        turnout = county_votes[county_name]

        # 6c: Calculate the percentage of votes for the county
        turnout_percentage = float(turnout) / float(total_votes) * 100

        # 6d: Print the county results to the terminal
        # This will print the county, percentage of total votes, and total votes
        county_results = (f'{county_name}: {turnout_percentage:.1f}% ({turnout:,})\n')

        print(county_results)

        # 6e: Save the county votes to a text file
        txt_file.write(county_results)

        # 6f: Write an if statement to determine the winning county and get its vote count
        # This if statement determines the county with the largest vote count and the adds
        # that county to its vote count to the variables created in Step 2
        if (turnout > winning_turnout):

            winning_turnout = turnout

            largest_turnout = county_name

    # 7: Print the county with the largest turnout to the terminal
    largest_turnout_summary = (
        f"--------------------------\n"
        f"Largest Turnout: {largest_turnout}\n"
        f"--------------------------\n"
    )
    print(largest_turnout_summary)

    # 8: Save the county with the largest turnout to a text file
    txt_file.write(largest_turnout_summary)

    # Save the final candidate vote count to the text file
    for candidate_name in candidate_votes:

        # Retrieve vote count and percentage
        votes = candidate_votes.get(candidate_name)
        vote_percentage = float(votes) / float(total_votes) * 100
        candidate_results = (
            f"{candidate_name}: {vote_percentage:.1f}% ({votes:,})\n")

        # Print each candidate's voter count and percentage to the terminal
        print(candidate_results)
        #  Save the candidate results to our text file.
        txt_file.write(candidate_results)

        # Determine winning vote count, winning percentage, and candidate
        if (votes > winning_count) and (vote_percentage > winning_percentage):
            winning_count = votes
            winning_candidate = candidate_name
            winning_percentage = vote_percentage

    # Print the winning candidate (to terminal)
    winning_candidate_summary = (
        f"-------------------------\n"
        f"Winner: {winning_candidate}\n"
        f"Winning Vote Count: {winning_count:,}\n"
        f"Winning Percentage: {winning_percentage:.1f}%\n"
        f"-------------------------\n"
    )
    print(winning_candidate_summary)

    # Save the winning candidate's name to the text file
    txt_file.write(winning_candidate_summary)