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

from __future__ import division

from django.db import transaction
from django_curtail_uuid import CurtailUUID
from django_logit import logit
from django_response import response
from ipaddr import client_ip
from isoweek import Week
from paginator import pagination
from TimeConvert import TimeConvert as tc

from account.models import LensmanInfo, UserIncomeExpensesInfo, UserInfo
from group.models import GroupInfo, GroupPhotoInfo, GroupPhotoOrderInfo
from message.models import SystemMessageInfo
from pay.models import OrderInfo
from photo.models import PhotosInfo
from utils.error.errno_utils import LensmanStatusCode, OrderStatusCode, UserStatusCode
from utils.message_utils import system_messages
from utils.redis.connect import r
from utils.redis.rbrief import set_brief_info
from utils.redis.rgroup import set_group_info, set_group_info_by_id
from utils.redis.rkeys import GROUP_LAST_PHOTO_PK, TODAY_INCOME, TODAY_UPLOAD_PHOTO_AMOUNT, WEEK_INCOME, WEEK_SOLD
from utils.redis.rlock import upload_lock
from utils.redis.rorder import set_lensman_order_record
from utils.redis.rprice import get_lensman_price_fixed, set_lensman_price_fixed
from utils.redis.rprofile import set_profile_info
from utils.storage_qiniu_utils import file_save


@logit
def lensman_submit_api(request):
    """ 摄影师信息提交 """
    lensman_type = int(request.POST.get('lensman_type', 0))
    unionid = request.POST.get('unionid', '')
    openid = request.POST.get('openid', '')
    phone = request.POST.get('phone', '')

    if LensmanInfo.objects.filter(phone=phone).exclude(unionid=unionid).exists():
        return response(LensmanStatusCode.LENSMAN_PHONE_ALREADY_EXISTS)

    fields = {
        'name': request.POST.get('name', ''),
        'sex': int(request.POST.get('sex', 1)),
        'phone': phone,
        'location': request.POST.get('location', ''),
        'user_status': LensmanInfo.UNVERIFIED,
    }

    lensman, created = LensmanInfo.objects.get_or_create(unionid=unionid, defaults=fields)

    # 状态为 UNVERIFIED 的允许修改, 其他需要登录摄影师 APP 进行信息的修改
    # 用户状态校验
    if lensman_type == UserInfo.COMMON:  # 普通摄影师校验
        if lensman.is_common_lensman and lensman.user_status not in [LensmanInfo.UNVERIFIED, LensmanInfo.REFUSED]:
            return response(LensmanStatusCode.LENSMAN_ALREADY_NOT_UNVERIFIED)
    elif lensman_type == UserInfo.OUTTAKE:  # 花絮摄影师校验
        if lensman.is_common_lensman and lensman.user_status not in [LensmanInfo.UNVERIFIED, LensmanInfo.REFUSED]:
            return response(LensmanStatusCode.LENSMAN_ALREADY_NOT_UNVERIFIED)

    for key, value in fields.iteritems():
        setattr(lensman, key, value)

    if lensman_type == LensmanInfo.COMMON:
        lensman.is_common_lensman = True
    elif lensman_type == LensmanInfo.OUTTAKE:
        lensman.is_outtake_lensman = True

    lensman.save()

    return response(200, 'Submit Success', u'提交成功')


@logit
@transaction.atomic
def lensman_wx_authorize_api(request):
    lensman_type = int(request.POST.get('lensman_type', 0))
    unionid = request.POST.get('unionid', '')

    # 用户校验
    try:
        user = UserInfo.objects.select_for_update().get(unionid=unionid, islensman=True, status=True)
    except UserInfo.DoesNotExist:
        return response(LensmanStatusCode.LENSMAN_NOT_FOUND)

    # 用户状态校验
    if lensman_type == UserInfo.COMMON:  # 普通摄影师校验
        if user.is_common_lensman and user.user_status != UserInfo.ACTIVATED:
            return response(LensmanStatusCode.LENSMAN_NOT_ACTIVATED)
    elif lensman_type == UserInfo.OUTTAKE:  # 花絮摄影师校验
        if user.is_outtake_lensman and user.outtake_status != UserInfo.ACTIVATED:
            return response(LensmanStatusCode.LENSMAN_NOT_ACTIVATED)

    # Set User Key's Value
    user.openid_lensman = request.POST.get('openid', '')
    user.sex = request.POST.get('sex', 0)
    user.nickname = request.POST.get('nickname', '') or request.POST.get('screen_name', '')
    user.avatar = request.POST.get('headimgurl', '') or request.POST.get('profile_image_url', '')
    user.country = request.POST.get('country', '')
    user.province = request.POST.get('province', '')
    user.city = request.POST.get('city', '')
    user.signup_ip = client_ip(request)
    user.signup_at = tc.utc_datetime()
    user.save()

    set_profile_info(user)

    return response(200, 'Lensman Login Success', u'摄影师登录成功', user.data)


@logit
def lensman_price_fix_api(request):
    lensman_id = request.POST.get('user_id', '')
    nomark = request.POST.get('nomark', 299)
    origin = request.POST.get('origin', 999)

    # 用户校验
    try:
        lensman = LensmanInfo.objects.get(lensman_id=lensman_id, status=True)
    except LensmanInfo.DoesNotExist:
        return response(UserStatusCode.USER_NOT_FOUND)

    if 'nomark' in request.POST:
        lensman.nomark = nomark
    if 'origin' in request.POST:
        lensman.origin = origin
    lensman.save()

    set_lensman_price_fixed(lensman_id)

    return response(200, 'Lensman Price Fix Success', u'摄影师定价修改成功')


@logit
def lensman_photo_upload_api(request):
    """ 摄影师照片上传 """
    lensman_type = int(request.POST.get('lensman_type', 0) or 0)
    user_id = lensman_id = request.POST.get('user_id', '')
    nickname = request.POST.get('nickname', '')
    group_id = request.POST.get('group_id', '')
    session_id = request.POST.get('session_id', '')

    photo_id = request.POST.get('photo_id', '')

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

    longitude = float(request.POST.get('lon', 0))  # 经度
    latitude = float(request.POST.get('lat', 0))  # 纬度
    location = float(request.POST.get('loc', 0))  # 位置

    # 用户校验
    try:
        user = UserInfo.objects.get(user_id=user_id, islensman=True, status=True)
    except UserInfo.DoesNotExist:
        return response(LensmanStatusCode.LENSMAN_NOT_FOUND)

    # 用户状态校验
    if lensman_type == UserInfo.COMMON:  # 普通摄影师校验
        watermark = True
        if user.is_common_lensman and user.user_status != UserInfo.ACTIVATED:
            return response(LensmanStatusCode.LENSMAN_NOT_ACTIVATED)
    elif lensman_type == UserInfo.OUTTAKE:  # 花絮摄影师校验
        watermark = False
        if user.is_outtake_lensman and user.outtake_status != UserInfo.ACTIVATED:
            return response(LensmanStatusCode.LENSMAN_NOT_ACTIVATED)

    if not group_id:
        # 判断通过 session_id 创建的群组是否存在，如果不存在，则直接创建
        group, group_created = GroupInfo.objects.get_or_create(session_id=session_id, group_from=GroupInfo.SESSION_GROUP, defaults={
            'group_id': CurtailUUID.uuid(GroupInfo, 'group_id'),
            # 'admin_id': user_id,  # 摄影师非管理员，首个扫码进群的用户为管理员
            'group_name': user.final_nickname,
            'group_default_avatar': 0,
        })

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

        group_id = group.group_id

    if photo and upload_lock(group_id, user_id, photo):
        photo_info = file_save(photo, prefix='photo', ext='.jpeg', watermark=watermark, thumbnail=True)

        # 写 PhotosInfo 表
        photo, created = PhotosInfo.objects.get_or_create(
            lensman_id=lensman_id,
            session_id=session_id,
            photo_id=photo_id,
        )
        # 无水印
        photo.m_photo_path = photo_info.photo_path
        # 有水印
        photo.p_photo_path = photo_info.photo_watermark_path
        photo.save()

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

        # 写 GroupPhotoInfo 表
        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 if watermark else photo_info.photo_path,
                'has_watermark': watermark,
                '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),
                'lensman_type': lensman_type,
                'latitude': latitude,
                'longitude': longitude,
                'location': location,
            }
        )

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

            # 更新今日上传照片数量
            r.incr(TODAY_UPLOAD_PHOTO_AMOUNT % (user_id, tc.local_string(format='%Y%m%d')))

            # Redis 群组数据缓存
            set_group_info_by_id(group_id)

    return response(200, 'Lensman Upload Photo Success', u'摄影师照片上传成功', {
        'group_id': group_id,
    })


@logit
@transaction.atomic
def lensman_origin_photo_upload_api(request):
    order_id = request.POST.get('order_id', '')

    user_id = lensman_id = request.POST.get('user_id', '')
    session_id = request.POST.get('session_id', '')

    photo_id = request.POST.get('photo_id', '')

    deleted = int(request.POST.get('deleted', 0))

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

    try:
        order = OrderInfo.objects.select_for_update().get(order_id=order_id, pay_status=OrderInfo.PAID)
    except OrderInfo.DoesNotExist:
        return response(OrderStatusCode.WX_ORDER_NOT_FOUND)

    # 原图已删除, 处理退款逻辑
    if deleted and order.photo_status == OrderInfo.WANTED:
        # 用户余额增加
        try:
            from_user = UserInfo.objects.select_for_update().get(user_id=order.from_uid)
        except UserInfo.DoesNotExist:
            pass

        if from_user:
            # 余额增加
            from_user.balance += order.total_fee
            from_user.save()
            # 余额记录
            UserIncomeExpensesInfo.objects.create(
                user_id=order.from_uid,
                photo_id=order.photo_id,
                type=UserIncomeExpensesInfo.INCOME,
                amount=order.total_fee,
                balance=from_user.balance,
                freeze_income_amount=0,
                freeze_income_balance=from_user.freeze_income_balance,
                remark=u'高清图购买退款',
            )

        # 摄影师余额减少
        try:
            to_user = UserInfo.objects.select_for_update().get(user_id=order.to_uid)
        except UserInfo.DoesNotExist:
            pass

        if to_user:
            # 余额减少
            to_user.freeze_income_balance -= order.total_fee
            to_user.save()
            # 余额记录
            UserIncomeExpensesInfo.objects.create(
                user_id=order.to_uid,
                photo_id=order.photo_id,
                type=UserIncomeExpensesInfo.EXPENSE,
                amount=order.total_fee,
                balance=to_user.balance,
                freeze_income_amount=0,
                freeze_income_balance=to_user.freeze_income_balance,
                remark=u'高清图购买退款',
            )

        # 更新订单状态
        order.photo_status = OrderInfo.DELETED
        order.reback_status = True
        order.reback_at = tc.utc_datetime()
        order.save()

    if photo and upload_lock(order_id, user_id, photo):
        # 写 PhotosInfo 表
        photo_info = file_save(photo, prefix='photo', ext='.jpeg')

        PhotosInfo.objects.filter(
            lensman_id=lensman_id,
            session_id=session_id,
            photo_id=photo_id,
        ).update(
            r_photo_path=photo_info.photo_path
        )

        porder, created = GroupPhotoOrderInfo.objects.select_for_update().get_or_create(
            group_id=order.group_id,
            session_id=session_id,
            user_id=order.from_uid,
            photo_id=order.photo_id,
            lensman_photo_id=photo_id,
        )
        porder.r_photo_path = photo_info.photo_path
        porder.save()

        set_lensman_order_record(porder)

        # 摄影师余额解冻
        try:
            to_user = UserInfo.objects.select_for_update().get(user_id=order.to_uid)
        except UserInfo.DoesNotExist:
            pass

        if to_user:
            # 余额解冻
            to_user.balance += order.total_fee
            to_user.freeze_income_balance -= order.total_fee
            to_user.save()
            # Redis 数值更新
            set_brief_info(order.to_uid, order.photo_type, order.total_fee, dt=order.created_at)
            # 余额记录
            UserIncomeExpensesInfo.objects.create(
                user_id=order.to_uid,
                photo_id=order.photo_id,
                type=UserIncomeExpensesInfo.UNFREEZE,
                amount=order.total_fee,
                balance=to_user.balance,
                freeze_income_amount=order.total_fee,
                freeze_income_balance=to_user.freeze_income_balance,
                remark=u'高清图购买退款',
            )

        order.photo_status = OrderInfo.FETCHED
        order.save()

    return response(200, 'Lensman Upload Origin Photo Success', u'摄影师照片高清图上传成功')


@logit
def lensman_brief_api(request):
    user_id = request.POST.get('user_id', '')

    ymd = tc.local_string(format='%Y%m%d')
    week = Week.thisweek().isoformat()

    # 周收入
    origin_week_income = int(r.get(WEEK_INCOME % (user_id, OrderInfo.ORIGIN, week)) or 0)
    # 日收入
    nomark_today_income = int(r.get(TODAY_INCOME % (user_id, OrderInfo.NOMARK, ymd)) or 0)
    # 日上传
    today_upload = int(r.get(TODAY_UPLOAD_PHOTO_AMOUNT % (user_id, ymd)) or 0)
    # 周售出
    week_sold = int(r.get(WEEK_SOLD % (user_id, OrderInfo.ORIGIN, ymd)) or 0)

    # 摄影师端系统消息
    systems = system_messages(user_id, SystemMessageInfo.PAIAI_LENSMAN)
    systems, left = pagination(systems, 1, 10)
    systems = [msg.msg_info(user_id) for msg in systems]

    # 照片购买记录
    orders = OrderInfo.objects.filter(to_uid=user_id, pay_status=OrderInfo.PAID, status=True).order_by('-pk')
    orders, left = pagination(orders, 1, 10)
    orders = [order.data(user_id) for order in orders]

    # 获取摄影师定价
    price_fixed = get_lensman_price_fixed(user_id)

    return response(200, 'Get Lensman Brief Success', u'获取摄影师简报成功', {
        'origin_week_income': origin_week_income,
        'nomark_today_income': nomark_today_income,
        'today_upload': today_upload,
        'week_sold': week_sold,
        'messages': {
            'system': systems,
            'orders': orders,
        },
        'price_fixed': price_fixed,
    })


@logit
def lensman_origin_wanted_api(request):
    user_id = request.POST.get('user_id', '')

    orders = OrderInfo.objects.filter(
        to_uid=user_id,
        photo_type=OrderInfo.ORIGIN,
        photo_status=OrderInfo.WANTED,
        pay_status=OrderInfo.PAID,
        status=True
    ).order_by('pk')
    wanted = [order.lensdata for order in orders]

    return response(200, 'Get Origin Wanted Success', u'获取需要上传原图成功', {
        'wanted': wanted
    })
