from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse
from django.utils.encoding import smart_unicode
from django.core.exceptions import ObjectDoesNotExist
from friends.models import Friend
from users.models import UserInfo
from utils.packed_json import toJSON
from utils.packed_jpush import jpush_send_message
import json
import time as _time
from datetime import datetime
from django.contrib.auth.models import User
import logging

logger = logging.getLogger(__name__)

def recommend(request):
	recommend_friends = {}
	friend_list = []
	friends = User.objects.all()
	for friend in friends:
		if(friend.username == "root"):
			continue
		logger.debug("friend is : %s" %friend.username)
		friend_info = UserInfo.objects.get(user=friend)
		recommend = {}
		recommend['u'] = friend.username
		recommend['a'] = friend_info.avatar_url()

		friend_list.append(recommend)

	recommend_friends['recommend'] = friend_list

	return HttpResponse(toJSON(recommend_friends))

def add_friend(request):
	data = {}

	if request.method == 'POST':
		logger.debug(str(request.POST))
		user_name = request.POST.get('username')
		logger.debug("src user name : "+user_name)
		try:
			src_user = User.objects.get(username = user_name)
		except ObjectDoesNotExist:
			data['status']=34
			return HttpResponse(toJSON(data))

		target_user=request.POST.get('target_user')
		# comment: for identify who that add
		# comment = request.POST.get('comment')

		try:
			add_user = User.objects.get(username=target_user)

			try:
				check_friend = Friend.objects.get(handle=src_user, friend=add_user)
				logger.debug("friend already add, skip it")
			except ObjectDoesNotExist:
				wait_friend = Friend.objects.create(handle = src_user, friend=add_user)
				wait_friend.verify_status = 2
				wait_friend.save()

				push_data = {}
				push_data = wait_friend.get_friend_info()
				logger.debug("friend info : "+str(push_data))
				jpush_send_message(toJSON(push_data), target_user, 1002)

			data['status']=0
			return HttpResponse(toJSON(data))
		except ObjectDoesNotExist:
			data['status']=28
			return HttpResponse(toJSON(data))

def accept_friend(request):
	data = {}

	if request.method == 'POST':
		logger.debug(str(request.POST))

		user_name = request.POST.get('username')
		# 1: agree, 0: disagree
		nok = request.POST.get('nok')
		try:
			src_user = User.objects.get(username = user_name)

		except ObjectDoesNotExist:
			data['status']=34
			return HttpResponse(toJSON(data))

		target_user=request.POST.get('target_user')

		try:
			target = User.objects.get(username=target_user)
			origin = User.objects.get(username=user_name)
			friend = Friend.objects.get(handle=target,friend=origin)
			if(nok is 1):
				# change friend verify status
				friend.verify_status = 1
				friend.save()
			else:
				# disagree to add, so delete it
				friend.delete()

			push_data = {}
			push_data['user_name'] = user_name
			jpush_send_message(toJSON(push_data), target_user, 1003)

			data['status']=0
			return HttpResponse(toJSON(data))
		except ObjectDoesNotExist:
			data['status']=28
			return HttpResponse(toJSON(data))

def update_friend(request):
	data = {}

	if request.method == 'POST':
		logger.debug(str(request.POST))

		user_name = request.POST.get('username')
		name_comment = request.POST.get('name_comment')
		descrip = request.POST.get('description')

		target_user=request.POST.get('target_user')

		try:
			src_user = User.objects.get(username = user_name)
			logger.debug("friend description is "+descrip)
			update_user = User.objects.get(username=target_user)
			my_friend = Friend.objects.get(handle=src_user,friend=update_user)

			# set breakpoint to trace
			#import pdb; pdb.set_trace()
			my_friend.name_comment = name_comment
			my_friend.description = descrip
			my_friend.save()

			data['status']=0
			return HttpResponse(toJSON(data))
		except ObjectDoesNotExist:
			data['status']=28
			return HttpResponse(toJSON(data))



def list_contains_record(record_list, record):
	if (record is None):
		return False

	record_id = record.id
	for next in record_list:
		if ((next != None) and (next['sid'] == record_id)):
			return True
	return False

def safe_attr(obj, attr_name):
	if attr_name in obj:
		return obj[attr_name]
	return None

def process_client_changes(src_user, friends_buffer):
	logger.debug('* Processing client changes')

	# build an array of generic objects containing contact data,
	# using the Django built-in JSON parser
	logger.debug('Uploaded friends buffer: ' + smart_unicode(friends_buffer))
	json_list = json.loads(friends_buffer)
	logger.debug('Client-side updates: ' + str(len(json_list)))

	for jrecord in json_list:
		logger.debug('json record ' + str(jrecord))
		new_record = False
		sid = safe_attr(jrecord, 'sid')
		if(sid != None):
			logger.debug('Updating record: ' + str(sid))
			record = Friend.objects.get(id=sid)

		# if the 'change' for this record is that they were deleted
		# on the client-side, all we want to do is set the deleted
		# flag here, and we're done.
		if(safe_attr(jrecord,'d') == 1):
			record.deleted = True
			record.save()
			logger.debug('Deleted record: '+record.handle)