from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from rest_framework import serializers
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
from . import vi  # 确保你的vi模块可用

# 定义 Schema
class ImageUrlRequestSchema(serializers.Serializer):
    image_url = serializers.URLField(required=True, help_text="图片URL")

class ImageUrlResponseSchema(serializers.Serializer):
    image_url = serializers.URLField(help_text="处理后的图片URL")

class ErrorResponseSchema(serializers.Serializer):
    message = serializers.CharField(help_text="错误消息")
    details = serializers.CharField(help_text="详细错误信息")
    
    class Meta:
        ref_name = "AliViErrorResponse"

class TemplateRequestSchema(serializers.Serializer):
    template_id = serializers.CharField(required=True, help_text="模板ID")
    image_url = serializers.URLField(required=True, help_text="图片URL")

class BeautyRequestSchema(serializers.Serializer):
    image_url = serializers.URLField(required=True, help_text="图片URL")
    sharp = serializers.FloatField(required=False, default=1, help_text="锐化程度，范围0-1")
    smooth = serializers.FloatField(required=False, default=1, help_text="平滑程度，范围0-1")
    white = serializers.FloatField(required=False, default=0.3, help_text="美白程度，范围0-1")

class TidyupRequestSchema(serializers.Serializer):
    image_url = serializers.URLField(required=True, help_text="图片URL")
    shape_type = serializers.IntegerField(required=False, default=0, help_text="五官调整类型，0-瘦脸，1-大眼，2-瘦鼻")
    strength = serializers.FloatField(required=False, default=1, help_text="调整强度，范围0-1")

class MakeupRequestSchema(serializers.Serializer):
    image_url = serializers.URLField(required=True, help_text="图片URL")
    resource_type = serializers.IntegerField(required=False, default=0, help_text="化妆类型，0-淡妆，1-浓妆")
    strength = serializers.FloatField(required=False, default=1, help_text="化妆强度，范围0-1")

class LiquifyRequestSchema(serializers.Serializer):
    image_url = serializers.URLField(required=True, help_text="图片URL")
    slim_degree = serializers.FloatField(required=False, default=1, help_text="瘦脸程度，范围0-1")

class RetouchRequestSchema(serializers.Serializer):
    image_url = serializers.URLField(required=True, help_text="图片URL")
    retouch_degree = serializers.FloatField(required=False, default=1, help_text="磨皮程度，范围0-1")
    whitening_degree = serializers.FloatField(required=False, default=1, help_text="美白程度，范围0-1")

@swagger_auto_schema(
    method='post',
    request_body=ImageUrlRequestSchema,
    responses={
        200: openapi.Response('成功', ImageUrlResponseSchema),
        400: openapi.Response('请求错误', ErrorResponseSchema)
    },
    operation_description="添加图片模板",
    tags=['图像处理']
)
@api_view(['POST'])
def add_template(request):
    """添加模板"""
    try:
        image_url = request.data.get('image_url')
        my_vi = vi.VI()
        resp = my_vi.add_template(image_url)
        return Response({"image_url": resp})
    except Exception as e:
        return Response({"message": "An error occurred", "details": str(e)}, status=status.HTTP_400_BAD_REQUEST)

@swagger_auto_schema(
    method='post',
    request_body=TemplateRequestSchema,
    responses={
        200: openapi.Response('成功', ImageUrlResponseSchema),
        400: openapi.Response('请求错误', ErrorResponseSchema)
    },
    operation_description="合并图片和人脸模板",
    tags=['图像处理']
)
@api_view(['POST'])
def merge_image_face(request):
    """合并图片和模板"""
    try:
        template_id = request.data.get('template_id')
        image_url = request.data.get('image_url')
        my_vi = vi.VI()
        resp = my_vi.merge_image_face(template_id, image_url)
        return Response({"image_url": resp})
    except Exception as e:
        return Response({"message": "An error occurred", "details": str(e)}, status=status.HTTP_400_BAD_REQUEST)

@swagger_auto_schema(
    method='post',
    request_body=BeautyRequestSchema,
    responses={
        200: openapi.Response('成功', ImageUrlResponseSchema),
        400: openapi.Response('请求错误', ErrorResponseSchema)
    },
    operation_description="图片美颜处理",
    tags=['图像处理']
)
@api_view(['POST'])
def face_beauty(request):
    """美颜"""
    try:
        image_url = request.data.get('image_url')
        sharp = request.data.get('sharp', 1)
        smooth = request.data.get('smooth', 1)
        white = request.data.get('white', 0.3)
        my_vi = vi.VI()
        resp = my_vi.face_beauty(image_url, sharp, smooth, white)
        return Response({"image_url": resp})
    except Exception as e:
        return Response({"message": "An error occurred", "details": str(e)}, status=status.HTTP_400_BAD_REQUEST)

@swagger_auto_schema(
    method='post',
    request_body=TidyupRequestSchema,
    responses={
        200: openapi.Response('成功', ImageUrlResponseSchema),
        400: openapi.Response('请求错误', ErrorResponseSchema)
    },
    operation_description="五官调整",
    tags=['图像处理']
)
@api_view(['POST'])
def face_tidyup(request):
    """五官调整"""
    try:
        image_url = request.data.get('image_url')
        shape_type = request.data.get('shape_type', 0)
        strength = request.data.get('strength', 1)
        my_vi = vi.VI()
        resp = my_vi.face_tidyup(image_url, shape_type, strength)
        return Response({"image_url": resp})
    except Exception as e:
        return Response({"message": "An error occurred", "details": str(e)}, status=status.HTTP_400_BAD_REQUEST)

@swagger_auto_schema(
    method='post',
    request_body=MakeupRequestSchema,
    responses={
        200: openapi.Response('成功', ImageUrlResponseSchema),
        400: openapi.Response('请求错误', ErrorResponseSchema)
    },
    operation_description="化妆效果处理",
    tags=['图像处理']
)
@api_view(['POST'])
def face_makeup(request):
    """化妆"""
    try:
        image_url = request.data.get('image_url')
        resource_type = request.data.get('resource_type', 0)
        strength = request.data.get('strength', 1)
        my_vi = vi.VI()
        resp = my_vi.face_makeup(image_url, resource_type, strength)
        return Response({"image_url": resp})
    except Exception as e:
        return Response({"message": "An error occurred", "details": str(e)}, status=status.HTTP_400_BAD_REQUEST)

@swagger_auto_schema(
    method='post',
    request_body=LiquifyRequestSchema,
    responses={
        200: openapi.Response('成功', ImageUrlResponseSchema),
        400: openapi.Response('请求错误', ErrorResponseSchema)
    },
    operation_description="瘦脸处理",
    tags=['图像处理']
)
@api_view(['POST'])
def liquify_face(request):
    """瘦脸"""
    try:
        image_url = request.data.get('image_url')
        slim_degree = request.data.get('slim_degree', 1)
        my_vi = vi.VI()
        resp = my_vi.liquify_face(image_url, slim_degree)
        return Response({"image_url": resp})
    except Exception as e:
        return Response({"message": "An error occurred", "details": str(e)}, status=status.HTTP_400_BAD_REQUEST)

@swagger_auto_schema(
    method='post',
    request_body=RetouchRequestSchema,
    responses={
        200: openapi.Response('成功', ImageUrlResponseSchema),
        400: openapi.Response('请求错误', ErrorResponseSchema)
    },
    operation_description="皮肤磨皮和美白",
    tags=['图像处理']
)
@api_view(['POST'])
def retouch_skin(request):
    """皮肤磨皮"""
    try:
        image_url = request.data.get('image_url')
        retouch_degree = request.data.get('retouch_degree', 1)
        whitening_degree = request.data.get('whitening_degree', 1)
        my_vi = vi.VI()
        resp = my_vi.retouch_skin(image_url, retouch_degree, whitening_degree)
        return Response({"image_url": resp})
    except Exception as e:
        return Response({"message": "An error occurred", "details": str(e)}, status=status.HTTP_400_BAD_REQUEST)
