# -*- coding: utf-8 -*-

from django.db import transaction
from django.shortcuts import render
from django_curtail_uuid import CurtailUUID
from django_logit import logit
from django_response import response
from ipaddr import client_ip
from rest_framework import viewsets
from TimeConvert import TimeConvert as tc

from account.models import LensmanInfo, UserInfo
from group.models import GroupInfo, GroupPhotoInfo, GroupUserInfo
from photo.models import PhotosInfo
from photo.serializers import PhotosInfoSerializer
from utils.error.errno_utils import LensmanStatusCode, PhotoStatusCode
from utils.redis.connect import r
from utils.redis.rgroup import get_group_info, set_group_info, set_group_users_info
from utils.redis.rkeys import (GROUP_LAST_PHOTO_PK, GROUP_USERS_DELETED_SET, GROUP_USERS_PASSED_SET,
                               GROUP_USERS_QUIT_SET, GROUP_USERS_REFUSED_SET, UUID_LIST)
from utils.redis.rprice import get_lensman_price_fixed
from utils.redis.ruuid import generate_uuids, update_uuids
from utils.storage_qiniu_utils import file_save


@logit
def uuid_init(request):
    """ 生成唯一标识 """
    num = int(request.GET.get('num', 1000))

    # 生成 UUID
    generate_uuids(num)

    return response(200, 'UUID Refresh Success', u'UUID 更新成功')


# curl -X POST -F user_id=xxxxxxx -F num=100 http://api.pai.ai/uuid
@logit
@transaction.atomic
def uuid(request):
    """ 获取唯一标识 """
    lensman_id = request.POST.get('user_id', '')
    num = int(request.POST.get('num', 100))

    # 从 Redis 中 Pop 中指定数量的 UUID
    uuids, succeed, left = r.multi_pop(UUID_LIST, num)

    # # 异步更新 UUID 数据库中状态
    # if uuids:
    #     async(update_uuids, lensman_id, uuids)
    #
    # # 当可用 UUID 数量少于 500 时, 异步创建
    # if left < 500:
    #     async(generate_uuids)

    return response(200, 'Get UUID Success', u'获取唯一标识成功', uuids)


# [How to do a PUT request with curl?](http://stackoverflow.com/questions/13782198/how-to-do-a-put-request-with-curl)
# Unfortunately, the -T is no substitute for -X PUT if you want to specify parameters with -d or -F.
# -T sends the content of a file via PUT. To achieve the GET after a redirect, add the parameter --location
#
# -F, --form <name=content>
#               (HTTP)  This  lets  curl emulate a filled-in form in which a user has pressed the submit button. This causes curl to POST data
#               using the Content-Type multipart/form-data according to RFC 2388. This enables uploading of binary files  etc.  To  force  the
#               'content'  part  to  be a file, prefix the file name with an @ sign. To just get the content part from a file, prefix the file
#               name with the symbol <. The difference between @ and < is then that @ makes a file get attached in the post as a file  upload,
#               while the < makes a text field and just get the contents for that text field from a file.
#
# curl -X POST -F user_id=xxxxxxx -F session_id=xxxxxxx -F photo_id=xxxxxxx -F photo=@xxxxxxx.jpg http://api.pai.ai/photos/upload
@logit
def upload_photo(request):
    """ 上传图片 """
    lensman_id = request.POST.get('user_id', '')
    session_id = request.POST.get('session_id', '')
    photo_id = request.POST.get('photo_id', '')

    photo = request.FILES.get('photo', '')

    if not (lensman_id and session_id and photo):
        return response(PhotoStatusCode.PARAMS_ERROR)

    try:
        LensmanInfo.objects.get(lensman_id=lensman_id)
    except LensmanInfo.DoesNotExist:
        return response(LensmanStatusCode.LENSMAN_NOT_FOUND)

    photo_info = file_save(photo, prefix='photo', ext='.jpeg', watermark=True)

    photo, created = PhotosInfo.objects.get_or_create(
        lensman_id=lensman_id,
        session_id=session_id,
        photo_id=photo_id,
        p_photo_path=photo_info.photo_watermark_path,
        m_photo_path=photo_info.photo_path,
    )

    return response(200, 'Photo Upload Success', u'照片上传成功', photo.data)


@logit
@transaction.atomic
def session_join_api(request):
    """ Session 详情 """
    user_id = request.POST.get('user_id', '')
    session_id = request.POST.get('session_id', '')
    lensman_id = request.POST.get('lensman_id', '')
    nickname = request.POST.get('nickname', '')
    current_id = -1

    # 摄影师校验
    try:
        lensman = UserInfo.objects.get(user_id=lensman_id)
    except UserInfo.DoesNotExist:
        return response(LensmanStatusCode.LENSMAN_NOT_FOUND)

    # 判断 user_id 是否存在，如果不存在，则直接分配帐户
    user, user_created = UserInfo.objects.get_or_create(user_id=user_id, defaults={
        'user_id': CurtailUUID.uuid(UserInfo, 'user_id'),
        'user_status': UserInfo.ASSIGN,
        'assign_ip': client_ip(request),
        'assign_at': tc.utc_datetime(),
    })
    user_id = user.user_id

    # 判断通过 session_id 创建的群组是否存在，如果不存在，则直接创建
    group, group_created = GroupInfo.objects.select_for_update().get_or_create(session_id=session_id, group_from=GroupInfo.SESSION_GROUP, defaults={
        'group_id': CurtailUUID.uuid(GroupInfo, 'group_id'),
        # 'admin_id': lensman_id,
        'group_name': lensman.final_nickname,
        'group_default_avatar': 0,
    })
    group_id = group.group_id

    # Redis 群组数据缓存
    group_info = set_group_info(group) if group_created else get_group_info(group_id)

    # 判断 group_id/user_id 的群组用户是否存在，如果不存在，则直接创建
    group_user, group_user_created = GroupUserInfo.objects.get_or_create(group_id=group_id, user_id=user_id, defaults={
        # 'current_id': int(r.get(GROUP_LAST_PHOTO_PK % group_id) or -1),
        'current_id': current_id,  # 通过扫描 session_id 二维码进群的用户，默认可以查看该群组所有照片
        'nickname': nickname or user.final_nickname,
        'avatar': user.avatar,
        # 'admin': group_created,
        'user_status': GroupUserInfo.PASSED,
        'passed_at': tc.utc_datetime(),
    })
    if not group_user_created:
        group_user.current_id = current_id
        group_user.user_status = GroupUserInfo.PASSED
        group_user.save()

    # 管理员处理
    tobe_admin = not GroupUserInfo.objects.filter(admin=True, status=True).exists()
    if tobe_admin:
        group.admin_id = user_id
        group.save()
        group_user.admin = True
        group_user.save()

    # Redis 群组用户数据缓存
    group_users = set_group_users_info(group)

    # Redis 群组通过集合缓存
    r.srem(GROUP_USERS_REFUSED_SET % group_id, user_id)
    r.srem(GROUP_USERS_DELETED_SET % group_id, user_id)
    r.srem(GROUP_USERS_QUIT_SET % group_id, user_id)
    r.sadd(GROUP_USERS_PASSED_SET % group_id, user_id)

    #
    if group_created:
        # 获取 Session 照片
        photos = PhotosInfo.objects.filter(session_id=session_id, status=True)

        # 获取摄影师定价
        price_info = get_lensman_price_fixed(lensman_id)

        group_photo = None
        for photo in photos:
            photo_info = file_save(photo_path=photo.p_photo_path, prefix='photo', ext='.jpeg', thumbnail=True)

            # 群组照片记录创建
            group_photo, created = GroupPhotoInfo.objects.get_or_create(
                group_id=group_id,
                user_id=user_id,
                photo_md5=photo_info.photo_md5,
                defaults={
                    'nickname': user.final_nickname,
                    'avatar': user.avatar,
                    'photo_path': photo_info.photo_watermark_path,
                    'has_watermark': True,
                    'photo_w': photo_info.photo_w,
                    'photo_h': photo_info.photo_h,
                    'photo_thumbnail_path': photo_info.photo_thumbnail_path,
                    'photo_thumbnail_w': photo_info.photo_thumbnail_w,
                    'photo_thumbnail_h': photo_info.photo_thumbnail_h,
                    'photo_thumbnail2_path': photo_info.photo_thumbnail2_path,
                    'photo_thumbnail2_w': photo_info.photo_thumbnail2_w,
                    'photo_thumbnail2_h': photo_info.photo_thumbnail2_h,
                    'photo_from': GroupPhotoInfo.SESSION_GROUP,
                    'session_id': photo.session_id,
                    'lensman_id': photo.lensman_id,
                    'lensman_photo_id': photo.photo_id,
                    'nomark': price_info.get('nomark', 999),
                    'origin': price_info.get('origin', 999),
                }
            )

        if group_photo:
            # 设置群组最后一张照片PK
            r.set(GROUP_LAST_PHOTO_PK % group_id, group_photo.pk)

            # Redis 群组数据缓存
            set_group_info(group)

    # 获取从 current_id 到 now 的群组照片列表
    group_photos = GroupPhotoInfo.objects.filter(
        group_id=group_id,
        status=True,
        pk__gt=group_user.current_id,
    ).order_by(
        '-pk'
    )
    latest_photo = group_photos.first()

    return response(200, 'Apply Join Session Group Success', u'申请加入摄影师群成功', {
        'current_id': latest_photo and latest_photo.pk or current_id,
        'photos': [photo.photo_info(user_id) for photo in group_photos],
        'group_id': group_id,
        'group': group_info,
        'user_id': user_id,
        'users': group_users,
    })


@logit
def session_detail(request, session_id):
    photos = PhotosInfo.objects.filter(session_id=session_id, status=True)
    return render(request, 'photo/session_detail.html', {'photos': photos})


@logit
def photo_standard(request, photo_id):
    photo = PhotosInfo.objects.get(photo_id=photo_id)
    return render(request, 'photo/photo_detail.html', {'photo_url': photo.p_photo_url})


@logit
def photo_standard_api(request, photo_id):
    photo = PhotosInfo.objects.get(photo_id=photo_id)
    return response(200, 'Get Photo Detail Success', u'获取照片详情成功', {
        'photo': photo.detail,
    })


@logit
def photo_medium(request, photo_id):
    photo = PhotosInfo.objects.get(photo_id=photo_id)
    return render(request, 'photo/photo_detail.html', {'photo_url': photo.m_photo_url})


@logit
def photo_large(request, photo_id):
    photo = PhotosInfo.objects.get(photo_id=photo_id)
    return render(request, 'photo/photo_detail.html', {'photo_url': photo.l_photo_url})


@logit
def photo_raw(request, photo_id):
    photo = PhotosInfo.objects.get(photo_id=photo_id)
    return render(request, 'photo/photo_detail.html', {'photo_url': photo.r_photo_url})


class PhotoInfoViewSet(viewsets.ModelViewSet):
    queryset = PhotosInfo.objects.all().order_by('-pk')
    serializer_class = PhotosInfoSerializer
