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

# The team logos were shamelessly purloined from the ESPN website, and
# they remain the property of their respective institutions.  They
# are included here only for the purposes of this academic exercise and
# should not be used with out the consent of their owners.

import json
from tickets import Conference, Team, DBSession, startup_info
import urllib2

session = DBSession()

espn_img_url = "http://a.espncdn.com/combiner/i?img=/i/teamlogos/ncaa/500/%s.png"

teams = session.query(Team).all()

for team in teams:
    espn_id = str(team.espn_id)
    img = urllib2.urlopen(espn_img_url % espn_id).read()
    fname = "images/" + team.name + "_" + team.nickname + ".png"
    fname = fname.replace(" ", "_").lower()
    f = open(fname, "wb")
    f.write(img)
    f.close()
    print fname
        seat = random.randint(1,30)
        num_seats = random.choice([1,2,2,2,2,4,4,4,4])
        price = random.choice([35,40,50,52,55,58,65,67,70,72,74,78,85,88,100,110,120,195,250,300])
        ticket_lot = Ticket_Lot(
            seller = seller,
            game = game,
            section = section,
            row = row,
            price = price
        )
        session.add( ticket_lot)
        for j in range(num_seats):
            session.add( Ticket(
                lot = ticket_lot,
                seat = seat + j
            ))
    session.commit()

def populate_db():
    load_conferences_from_json()
    load_teams_from_json()
    dates = get_saturdays(2016, 11)
    for conf in session.query(Conference).all():
        schedule_games(conf.teams, dates)
    create_users(1000)
    create_tickets(10000)

session = DBSession()
populate_db()
session.close()