#import PyRSS2Gen
import optparse
import sys
import datetime
from ConfigParser import RawConfigParser
from ftracker.yahoo import YahooFFStats

#class FFStatPublisher(object):
#
#    def __init__(self, destination,
#                 docTitle='Fantasy Football Stats',
#                 docLink=None):
#        self.destination = destination
#        self.rssGen = PyRSS2Gen.RSS2(
#            title = docTitle,
#            link = docLink,
#            description = docTitle,
#            lastBuildDate = datetime.datetime.now())
#
#    def createItem(self, itemDict):
#        item = PyRSS2Gen.RSSItem(
#            title = itemDict["title"],
#            description = itemDict["content"],
#            pubDate = datetime.datetime.now())
#        self.rssGen.items.append(item)
#
#    def addItems(self, itemList):
#        self.rssGen.items.append(itemList)
#
#    def write(self):
#        self.rssGen.write_xml(open(self.destination, "w"))
#

def getConfig(path):
    rc = RawConfigParser()
    if path not in rc.read(path):
        print("Invalid Configuration File")
        sys.exit(1)
    return rc

def main():
    op = optparse.OptionParser()
    op.add_option("-c", "--config", dest="config",
                  help="configuration file")
    #op.add_option("-o", "--output", dest="output")
    (options, args) = op.parse_args()

    if options.config is None:
        print("Configuration file required")
        sys.exit(1)

    config = getConfig(options.config)
    login = {'user': config.get('main','yahoologin'),
             'passwd': config.get('main','yahoopasswd')}
    fs = YahooFFStats(login, config.get('main','url'))
    #pub = FFStatPublisher(options.output,
    #                      config.get('rss', 'title'),
    #                      config.get('rss', 'url'))

    fs.genStats()
    standings, scoreboard = fs.getScoreboardAndStandings()
    contentBuffer = "Fantasy Football Standings:\n\n"
    for eTeam in standings:
        contentBuffer += "%s\n" % eTeam['team']
        contentBuffer += "Rank: %s\n" % eTeam['rank']
        contentBuffer += "Record: %s\n" % eTeam['wlt']
        contentBuffer += "Win Percentage: %s\n" % eTeam['winperc']
        contentBuffer += "Points: %s\n" % eTeam['pts']
        contentBuffer += "Streak: %s\n\n" % eTeam['streak']
    #pub.createItem({"title": "FF Standings %s" %
    #                datetime.datetime.now().isoformat(),
    #                "content": contentBuffer.replace("\n", "<br>")})

    scoreBuffer = "Fantasy Football Scoreboard:\n\n"
    for matchUp in scoreboard:
        for team in matchUp:
            scoreBuffer += "%s: %s\nvs.\n" % (team, matchUp[team])
        scoreBuffer = scoreBuffer[:-4] + "\n"
    #pub.createItem({"title": "FF Scoreboard %s" %
    #                datetime.datetime.now().isoformat(),
    #                "content": scoreBuffer.replace("\n", "<br>")})
    #pub.write()
    print contentBuffer
    print scoreBuffer

if __name__ == "__main__":
    main()
