#!/usr/bin/env python
# coding: utf-8

# In[2]:


import time
import pandas as pd
import numpy as np

df_chicago = pd.read_csv("chicago.csv")
df_new_york_city = pd.read_csv("new_york_city.csv")
df_washington = pd.read_csv("washington.csv")

CITY_DATA = { 'chicago': df_chicago,
              'new york city': df_new_york_city,
              'washington': df_washington }

def get_filters():
    """
    Asks user to specify a city, month, and day to analyze.

    Returns:
        (str) city - name of the city to analyze
        (str) month - name of the month to filter by, or "all" to apply no month filter
        (str) day - name of the day of week to filter by, or "all" to apply no day filter
    """
    print('Hello! Let\'s explore some US bikeshare data!')
    # TO DO: get user input for city (chicago, new york city, washington). HINT: Use a while loop to handle invalid inputs
    while True:
        city = input("please write the city you would like to analyze from chicago, new york city and washington: ")
        city = city.lower()
        if city not in CITY_DATA.keys():
            print("Please try again")
        else:
            break


    # TO DO: get user input for month (all, january, february, ... , june)
    while True:
        month = input("please write to select a month from january to june, or just write all: ")
        month = month.lower()
        if month not in ("january", "february", "march", "april", "may", "june", "all"):
            print("Please try again")
        else:
            break    

    # TO DO: get user input for day of week (all, monday, tuesday, ... sunday)
    while True:
        day = input("please write to select a weekday from monday to sunday, or just write all: ")
        day = day.lower()
        if day not in ("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday", "all"):
            print("Please try again")
        else:
            break


    print('-'*40)
    return city, month, day

def load_data(city, month, day):

    df = CITY_DATA[city]
    df["Start Time"] = pd.to_datetime(df["Start Time"])
    df["month"] = df["Start Time"].dt.month
    df['day_of_week'] = df['Start Time'].dt.day_name
    
    if month != "all":
        months = ['january', 'february', 'march', 'april', 'may', 'june']
        month = months.index(month)+1
        
        df = df[df["month"] == month]
    
    if day != "all":
        df = df[df['day_of_week'] == day.title()]
    
    return df

def time_stats(df):
    """Displays statistics on the most frequent times of travel."""

    print('\nCalculating The Most Frequent Times of Travel...\n')
    start_time = time.time()

    # TO DO: display the most common month
    month_values = df["month"].value_counts()
    month_values.head()




city, month, day = get_filters()
df = load_data(city, month, day)
time_stats(df)
