from Restaurant import Restaurant
from lat_long_to_dist import *

def parse_line(line):
    """Parses a single line of the yelp file, keeping some of the
    data, and throwing away the rest.
    """

    line = line.strip('\n')
    values = line.split('|')
    s_rating = values[6:]
    scores = []
    for s in s_rating:
        scores.append( int(s) )
    result = [ values[0], \
               float(values[1]),\
               float(values[2]), \
               values[3], \
               values[4], \
               values[5], \
               scores ]
    
    restaurant = Restaurant(result[0],result[1],result[2],result[3].replace("+","\n"),result[4],result[5],scores)
    return restaurant

def read_yelp(filename):
    """ Parses the given filename containing yelp data and
    returns a list of restaurants. Each item is a list containing 
    restaurant information.
    """

    restaurants = []
    for line in open(filename):
        new_r = parse_line(line)
        restaurants.append(new_r)
    return restaurants

if __name__ == "__main__":
    restaurants = read_yelp("yelp.txt")
    print restaurants
    
    position = raw_input("Position ==> ").split(",")
    for i in range(len(position)):
        position[i] = float(position[i])
    distance = float(raw_input("Distance ==> "))
    rating = float(raw_input("Rating ==> "))
    rType = raw_input("Type ==> ")
    
    for restaurant in restaurants:
        # if restaurant outside distance from given position
        if (distance < distance_from_lat_long(position[0],position[1],restaurant.latitude,restaurant.longitude)):
            continue
        # if average rating for restaurant less than input rating
        if (rating > restaurant.average_rating()):
            continue
        # check resturant is the same type
        if (rType.lower() != restaurant.category.lower()):
            continue
        
        print restaurant
        print