import random
import string

from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
from account.decorators import login_required
from .models import Folder
from file.models import File
import json
from account.models import get_user

# Create your views here.
DEBUG = 1


@login_required
def get_root_folder(request):
    # 获取根目录
    if request.method == 'POST':
        # 获取所有根文件夹id
        user = get_user(request)
        return JsonResponse({'code': 200, 'root_folder_id': user.profile.root_folder_id})
    elif request.method == 'GET' and DEBUG:
        return render(request, 'folder_list.html')
    else:
        return HttpResponse(status=400)


# 在根目录下列出所有文件夹与文件
@login_required
def folder_list(request):
    if request.method == 'POST':
        data = request.POST
        folder_id = data['folder_id']
        user = get_user(request)
        try:
            folder = user.folders.get(folder_id=folder_id)
        except:
            print('无法访问')
            return JsonResponse({'code': 404, 'error': '无法访问'})

        children_folders = folder.children_folders.all()
        children_files = folder.children_files.all()
        children = [*map(lambda f: {'type': 'folder', **f.to_json}, children_folders),
                    *map(lambda f: {'type': 'file', **f.to_json}, children_files)]
        return JsonResponse({'code': 200, 'children': children})
    elif request.method == 'GET' and DEBUG:
        return render(request, 'folder_list.html')
    else:
        return HttpResponse(status=400)


# 父文件夹id 子文件夹名字
# 增文件夹
@login_required
def add_folder(request):
    if request.method == 'POST':
        data = request.POST
        user = get_user(request)
        # folder_id_random = ''.join(random.sample(string.digits, 8))
        # father_folder = Folder.objects.filter(folder_id=data.get('father_folder_id')).get()

        father_folder_id = data.get('father_folder_id')
        try:
            father_folder = user.folders.get(folder_id=father_folder_id)
        except:
            print('上级文件夹不存在')
            return JsonResponse({'code': '421', 'error': '上级文件夹不存在'})

        folder_name = data.get('folder_name')
        Folder.objects.create(folder_name=folder_name, father_folder=father_folder)
        return JsonResponse({'code': '200'})
    elif request.method == 'GET' and DEBUG:
        return render(request, 'add_folder.html')
    else:
        return HttpResponse(status=400)


# 删除文件夹
@login_required
def delete_folder(request):
    if request.method == 'POST':
        data = request.POST
        user = get_user(request)
        folder_id = data.get('folder_id')
        try:
            folder = user.folders.get(folder_id=folder_id)
        except:
            print('无此文件夹')
            return JsonResponse({'code': '421', 'error': '无此文件夹'})
        folder.delete()
        return JsonResponse({'code': 200})
    elif request.method == 'GET' and DEBUG:
        return render(request, 'delete_folder.html')
    else:
        return HttpResponse(status=400)
