from flask import Flask, jsonify, url_for, redirect, request
from flask.ext.pymongo import PyMongo
from flask_restful import Resource, Api
import json

app = Flask(__name__)
#mongo = PyMongo(app)

# connect to another MongoDB server altogether
app.config['MONGO_HOST'] = 'ds021701.mlab.com'
app.config['MONGO_PORT'] = 21701
app.config['MONGO_DBNAME'] = 'wishapp'
app.config['MONGO_USERNAME'] = 'admin'
app.config['MONGO_PASSWORD'] = 'password'

mongo = PyMongo(app, config_prefix='MONGO')

APP_URL = "http://127.0.0.1:5000"

class Users(Resource):
    def get(self, username=None):
        data = []
        
        if username:
            user_info = mongo.db.user_details.find_one({"user_name": username}, {"_id": 0, "update_time": 0})

            if user_info:
                return {"status": "ok", "data": user_info}
            else:
                return {"response": "no user found for {}".format(username)}

        else:
            cursor = mongo.db.user_details.find({}, {"_id": 0, "update_time": 0}).limit(10)

            for user in cursor:
                print user
                data.append(user)

            return {"data": data}

    def post(self):
    	data = []
    	dataStr = request.data
        data = json.loads(dataStr)
        print type(dataStr)
        print type(data)
        
        if not data:
            data = {"response": "ERROR"}
            return jsonify(data)
        else:
            username = data.get('user_name')
            print "I'm here", username
            if username:
                if mongo.db.user_details.find_one({"user_name": username}):
                    return {"response": "user already exists."}
                else:
                    result = mongo.db.user_details.insert(data) 
                    return {"status": "ok"} 
            return {"response": "ERROR no username"}

    def put(self):
        data = []
        datadic = request.data
        data = json.loads(datadic)
        
        if not data:
            data = {"response": "ERROR"}
            return jsonify(data)
        else:
            username = data.get('user_name')
            wishlist = data.get('wish_list')
            print "I'm here in put---", username
            print "I'm here in put---", wishlist
            if username:
                result = mongo.db.user_details.find_one({"user_name": username})
                if result:
                    mongo.db.user_details.update({'user_name': username}, {'$push': {'wish_list': {'$each':wishlist}}}, True)
                    return {"status": "upserted ok"}
                else:
                    return {"response": "User doesn't exist."}
            return {"response": "ERROR no username"}

    def delete(self):
        data = []
        datadic = request.data
        data = json.loads(datadic)
        
        if not data:
            data = {"response": "ERROR"}
            return jsonify(data)
	
	username = data.get('user_name')
	sem3id = data.get('sem3id')        
        result = mongo.db.user_details.find_one({"user_name": username})
        wishlist = result['wish_list']
        new_wishlist = []
        item_flag = 0
        
        print "I'm here in delete---", username
        print "I'm here in delete---", wishlist
        
        if result:
            for item in wishlist:
                if item['sem3id']!= sem3id:
                    print "================================="
                    print "item---", item
                    print "sem3id---", sem3id
                    print "================================="
                    new_wishlist.append(item)
                else:
                    item_flag = 1
        else:
            return {"response": "ERROR no username"}
        
        if item_flag == 1:
            print "I'm here in delete---", new_wishlist
            mongo.db.user_details.update({'user_name': username}, {'$set': {'wish_list': new_wishlist}})
            return {"status":"ok"}
        else:
            return{"status":"Illegal delete. Record not present."}


api = Api(app)

api.add_resource(Users, "/users/<string:username>", endpoint="user_details2")
api.add_resource(Users, "/users/", endpoint="user_details")

if __name__ == "__main__":
    app.run(host='0.0.0.0', debug=True)
