from flask.ext.restful import Resource, reqparse, fields, marshal
from .base import api_auth
from ..user import UserModel
from ..league.models import LeagueModel, create_league, update_league, delete_league


league_template = {
    'league_id': fields.String,
    'name': fields.String,
    'rating_scheme': fields.String,
    'description': fields.String,
}


class LeagueListAPI(Resource):
    decorators = [api_auth.login_required]

    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('username', type=str, required=True, location='authorization')
        self.reqparse.add_argument('name', type=str, location='json')
        self.reqparse.add_argument('rating_scheme', type=str, default="ELO", location='json')
        self.reqparse.add_argument('description', type=str, location='json')
        super(LeagueListAPI, self).__init__()

    def get(self):
        """ return all leagues associated with the user """
        args = self.reqparse.parse_args()
        user_id = args['username']
        ancestor_key = UserModel.build_key(user_id=user_id)

        leagues = LeagueModel.query(ancestor=ancestor_key).fetch()
        return {'data': [marshal(l, league_template) for l in leagues]}

    def post(self):
        """ create a new league """
        args = self.reqparse.parse_args()
        user_id = args['username']
        user = UserModel.build_key(user_id=user_id).get()

        new_league = create_league(user, args.get('name'), args.get('rating_scheme'),
                                   description=args.get('description'))
        return {'data': marshal(new_league, league_template)}


class LeagueAPI(Resource):
    decorators = [api_auth.login_required]

    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('username', type=str, required=True, location='authorization')
        self.reqparse.add_argument('name', type=str, location='json')
        self.reqparse.add_argument('rating_scheme', type=str, default="ELO", location='json')
        self.reqparse.add_argument('description', type=str, location='json')
        super(LeagueAPI, self).__init__()

    def get(self, league_id):
        """ return the specified league """
        args = self.reqparse.parse_args()
        user_id = args['username']
        ancestor_key = UserModel.build_key(user_id=user_id)

        league = LeagueModel.build_key(league_id=league_id, user_key=ancestor_key).get()
        return {'data': marshal(league, league_template)}

    def put(self, league_id):
        """ update the specified league """
        args = self.reqparse.parse_args()
        user_id = args['username']
        user = UserModel.build_key(user_id=user_id).get()

        updated_league = update_league(user, league_id, args.get('name'), args.get('rating_scheme'),
                                       description=args.get('description'))
        return {'data': marshal(updated_league, league_template)}

    def delete(self, league_id):
        """ delete the specified league """
        args = self.reqparse.parse_args()
        user_id = args['username']
        user = UserModel.build_key(user_id=user_id).get()

        delete_league(user, league_id)
        return {'data': 'Success'}
@@ -834,32 +834,156 @@
 ocation='json')%0A
+        self.args = self.reqparse.parse_args()%0A        self.user = UserModel.build_key(user_id=self.args%5B'username'%5D).get()%0A
         super(Le
@@ -1096,146 +1096,8 @@
 %22%22%22%0A
-        args = self.reqparse.parse_args()%0A        user_id = args%5B'username'%5D%0A        ancestor_key = UserModel.build_key(user_id=user_id)%0A%0A
     
@@ -1137,25 +1137,26 @@
 ncestor=
-ancestor_
+self.user.
 key).fet
@@ -1293,144 +1293,8 @@
 %22%22%22%0A
-        args = self.reqparse.parse_args()%0A        user_id = args%5B'username'%5D%0A        user = UserModel.build_key(user_id=user_id).get()%0A%0A
     
@@ -1324,22 +1324,32 @@
 _league(
+self.
 user, 
+self.
 args.get
@@ -1350,32 +1350,37 @@
 gs.get('name'), 
+self.
 args.get('rating
@@ -1429,32 +1429,37 @@
     description=
+self.
 args.get('descri
@@ -2027,253 +2027,239 @@
    s
-uper(LeagueAPI, self).__init__()%0A%0A    def get(self, league_id):%0A        %22%22%22 return the specified league %22%22%22%0A        args = self.reqparse.parse_args()%0A        user_id = args%5B'username'%5D%0A        ancestor_key = UserModel.build_key(user_id=user_id)%0A
+elf.args = self.reqparse.parse_args()%0A        self.user = UserModel.build_key(user_id=self.args%5B'username'%5D).get()%0A        super(LeagueAPI, self).__init__()%0A%0A    def get(self, league_id):%0A        %22%22%22 return the specified league %22%22%22
 %0A   
@@ -2328,17 +2328,18 @@
 key=
-ancestor_
+self.user.
 key)
@@ -2482,144 +2482,8 @@
 %22%22%22%0A
-        args = self.reqparse.parse_args()%0A        user_id = args%5B'username'%5D%0A        user = UserModel.build_key(user_id=user_id).get()%0A%0A
     
@@ -2509,32 +2509,37 @@
 = update_league(
+self.
 user, league_id,
@@ -2539,16 +2539,21 @@
 gue_id, 
+self.
 args.get
@@ -2562,16 +2562,21 @@
 name'), 
+self.
 args.get
@@ -2645,16 +2645,21 @@
 ription=
+self.
 args.get
@@ -2823,144 +2823,8 @@
 %22%22%22%0A
-        args = self.reqparse.parse_args()%0A        user_id = args%5B'username'%5D%0A        user = UserModel.build_key(user_id=user_id).get()%0A%0A
     
@@ -2841,16 +2841,21 @@
 _league(
+self.
 user, le

clean up some redundant arg and user related code