from django.http import HttpResponse
from django.shortcuts import render
from account.decorators import login_required
from .models import Group
from account.models import User
from folder.models import Folder
import json
import random
import string

# Create your views here.
DEBUG = 1


# 首先是 加入/退出 群组，客户端发送申请，把该用户直接 加入/删除 该群组的对象
@login_required
def add_group(request):
    if request.method == 'POST':
        data = request.POST
        # 查找是否有此人
        user = User.objects.filter(username=data['username']).get()
        if user:
            # 查找此人是否已经在群组里
            group = Group.objects.filter(group_id=data['group_id']).get()
            if group.member.filter(username=user.username).count() > 0:
                # 此人已在群组里
                return HttpResponse(status=421)
            else:
                group.member.add(user)
                group.save()
                return HttpResponse(status=200)
        else:
            return HttpResponse(status=422)
    elif request.method == 'GET':
        if DEBUG:
            return render(request, 'add_group.html')
    else:
        return HttpResponse(status=400)


@login_required
def create_group(request):
    if request.method == 'POST':
        data = request.POST
        # 查找是否有此人
        username = data.get('username', '')
        user = User.objects.filter(username=username)
        if user:
            group_id_random = ''.join(random.sample(string.digits, 8))
            folder_1 = Folder.objects.create(folder_id=group_id_random,
                                             folder_name=data['group_name'],
                                             father_folder=None)
            folder_1.save()
            group_1 = Group.objects.create(group_id=group_id_random,
                                           group_name=data['group_name'],
                                           creator=username,
                                           folder=folder_1,)
            group_1.member.set(user)
            group_1.save()
            return HttpResponse(status=200)
        else:
            return HttpResponse(status=422)
    elif request.method == 'GET':
        if DEBUG:
            return render(request, 'create_group.html')
    else:
        return HttpResponse(status=400)


# value = ''.join(random.sample(string.ascii_letters + string.digits, 8))

@login_required
def leave_group(request):
    if request.method == 'POST':
        data = request.POST
        # 查找是否有此人
        user = User.objects.filter(username=data['username']).get()
        if user:
            # 查找此人是否已经在群组里
            group = Group.objects.filter(group_id=data['group_id']).get()
            if group.member.filter(username=user.username).count() == 0:
                # 此人不在群组里
                return HttpResponse(status=421)
            else:
                group.member.remove(user)
                group.save()
                return HttpResponse(status=200)
        else:
            return HttpResponse(status=422)
    elif request.method == 'GET':
        if DEBUG:
            return render(request, 'leave_group.html')
    else:
        return HttpResponse(status=400)


# 获取你所在的所有群组
@login_required
def group_list(request):
    if request.method == 'POST':
        data = request.POST
        # 获取群组
        groups = Group.objects.filter(member__username=data['username'])
        response = []
        for i in groups:
            response.append(i.group_name)
        return HttpResponse(json.dumps(response), status=200)
    elif request.method == 'GET':
        if DEBUG:
            return render(request, 'group_list.html')
    else:
        return HttpResponse(status=400)


# 删除群组
