import json
from utils.base import APIView
from utils.logger import error_logger


class PictorialListView(APIView):
    def get(self, request):
        star_id = request.GET.get('star_id')
        user_id = request.GET.get('user_id')
        offset = int(request.GET.get('page', 1))
        limit = int(request.GET.get('limit', 10))
        filters = self.handle_filter(request.GET.get('filter', '{}'))
        product_id = request.GET.get('product_id')
        if star_id:
            filters.update({'star_id': star_id})
        if user_id:
            filters.update({'user_id': user_id})
        try:
            data = self.rpc['venus/sun/pictorial/list'](filters=filters, offset=(offset-1)*limit, limit=limit, product_id=product_id).unwrap()
        except Exception as e:
            error_logger.error(u'获取画报列表失败%s' , e)
            raise
        return data

    def post(self, request):
        ids = json.loads(request.POST.get('ids', '[]'))
        updates = json.loads(request.POST.get('update', '{}'))
        try:
            self.rpc['venus/sun/pictorial/batch/update'](updates=updates, ids=ids).unwrap()
        except Exception as e:
            error_logger.error(u'批量更新失败%s', e)
            raise
        return {
            "message": "更新成功"
        }


class PictorialUpdateOrCreate(APIView):
    def get(self, request):
        id = request.GET.get('id').split(':')[0]
        try:
            data = self.rpc['venus/sun/pictorial/get'](id=id).unwrap()
        except Exception as e:
            error_logger.error(u'获取%s画报信息失败%s'%(id, e))
            raise
        return data

    def post(self, request):

        id = request.POST.get('id')
        star_ids = json.loads(request.POST.get('star', '[]'))
        pictorial_user_ids = json.loads(request.POST.get('pictorial_user_ids', '[]'))
        collection_tag_ids = json.loads(request.POST.get('collection_tag_ids', '[]'))
        pictorial_activity_id = request.POST.get('pictorial_activity_id', 0)
        data = {
            'name': request.POST.get('name', ''),
            'description': request.POST.get('description', ''),
            'creator_id': request.POST.get('creator_id', ''),
            'celebrity_ids': star_ids,
            'is_online': int(request.POST.get('is_online', 1)),
            'is_recommend': int(request.POST.get('is_recommend', 0)),
            'pictorial_user_ids': pictorial_user_ids,
            'icon': request.POST.get('icon', ''),
            'collection_tag_ids': collection_tag_ids,
            'is_home_recommend': int(request.POST.get('is_home_recommend', 0)),
            'add_score': int(request.POST.get('add_score', 0)),
            'is_public': True if request.POST.get('is_public') == "true" else False,
            'is_feed': int(request.POST.get('is_feed', 0)),
            'pictorial_activity_ids': [int(pictorial_activity_id)],
            'alias': request.POST.get('alias', ''),

        }
        try:
            data = self.rpc['venus/sun/pictorial/edit'](id=id, data=data).unwrap()
        except Exception as e:
            error_logger.error(u'编辑%s用户信息失败%s' % (id, e))
            raise
        if not data.get('id'):
            return {
                'message': '画报已存在'
            }
        return {
            'message': '更新成功',
            'data': data
        }

    
class PictorialTopics(APIView):
    """多个帖子关系"""

    def post(self, request):
        topic_ids = json.loads(request.POST.get('topic_ids', '[]'))
        pictorial_id = request.POST.get('pictorial_id')

        try:
            self.rpc['venus/sun/pictorial/add_topics'](topic_ids=topic_ids, pictorial_id=pictorial_id).unwrap()
        except Exception as e:
            error_logger.error(u'编辑%s用户信息失败%s' % (id, e))
            raise
        return {
            'message': '更新成功'
        }


class PictorialFeedlListView(APIView):
    def get(self, request):
        order_by = request.GET.get('order_by', "-id")
        offset = int(request.GET.get('offset', 1))
        limit = int(request.GET.get('limit', 10))

        try:
            data = self.rpc['venus/sun/pictorial/feed/list'](offset=(offset - 1) * limit, limit=limit,
                                                             order_by=order_by).unwrap()
        except Exception as e:
            error_logger.error(u'获取画报列表失败%s' , e)
            raise
        return data


class PictorialFeedlRank(APIView):
    """修改画报首页like流rank"""

    def post(self, request):
        id_ = request.POST.get('id')
        rank = int(request.POST.get('rank'))

        try:
            self.rpc['venus/sun/pictorial/feed/rank'](id_=id_, rank=rank).unwrap()
        except Exception as e:
            error_logger.error(u'编辑%信息失败%s' % (e))
            raise
        return {
            'message': '更新成功'
        }


class PictorialFeedDelete(APIView):
    """删除画报首页like流"""

    def post(self, request):
        ids = json.loads(request.POST.get('ids', '[]'))

        try:
            self.rpc['venus/sun/pictorial/feed/delete'](ids=ids).unwrap()
        except Exception as e:
            error_logger.error(u'删除信息失败%s' % (e))
            raise
        return {
            'message': '删除成功'
        }


class PictorialProductAdd(APIView):
    """画报添加商品关系"""

    def post(self, request):
        ids = json.loads(request.POST.get('ids', '[]'))
        product_id = request.POST.get('product_id')

        try:
            self.rpc['venus/sun/pictorial/add_product'](ids=ids, product_id=product_id).unwrap()
        except Exception as e:
            error_logger.error(u'编辑%s画报信息失败%s' % (ids, e))
            raise
        return {
            'message': '更新成功'
        }


class PictorialProductDel(APIView):
    """画报删除商品关系"""

    def post(self, request):
        ids = json.loads(request.POST.get('ids', '[]'))
        product_id = request.POST.get('product_id')

        try:
            self.rpc['venus/sun/pictorial/del_product'](ids=ids, product_id=product_id).unwrap()
        except Exception as e:
            error_logger.error(u'编辑%s画报信息失败%s' % (ids, e))
            raise
        return {
            'message': '更新成功'
        }
