from datetime import datetime
import json
from utils.base import APIView
from utils.logger import error_logger
from alpha_types.venus import REPLY_TYPE, REPLY_BELONG_TYPE, USER_EXTRA_TYPE


class ReplyUpdateOrCreateView(APIView):
    def get(self, request):
        topic_id = request.GET.get('topic_id')
        pictorial_id = request.GET.get('pictorial_id')
        offset = int(request.GET.get('page', 1))
        limit = int(request.GET.get('limit', 10))
        filter = self.handle_filter(request.GET.get('filter', {}))

        if topic_id:
            filter.update({'topic_id': topic_id})
        elif pictorial_id:
            filter.update({'pictorial_id': pictorial_id})

        try:
            data = self.rpc['venus/sun/reply/list'](offset=(offset-1) * limit, limit=limit, filters=filter).unwrap()
        except Exception as e:
            error_logger.error(u'回复帖子失败%s' , e)
            raise
        return data

    def post(self, request):

        reply_ids = json.loads(request.POST.get('reply_ids', []))
        try:
            self.rpc['venus/sun/reply/batch_delete'](ids=reply_ids).unwrap()
        except Exception as e:
            error_logger.error(u'批量更新回复失败%s', e)
            raise
        return {
            'message': '操作成功'
        }


class ReplyCreate(APIView):
    def post(self, request):

        request.POST.get('be_replied_id', None)
        data = {
            'user_id': request.POST.get('user_id').split(':')[0],
            'replied_id': request.POST.get("replied_id", None),
            'content': request.POST.get('content'),
            'replied_user_id': request.POST.get('replied_user_id', None)
        }
        topic_id = request.POST.get('topic_id')
        pictorial_id = request.POST.get('pictorial_id')

        if topic_id:
            data['topic_id'] = topic_id

        if pictorial_id:
            data['pictorial_id'] = pictorial_id

        try:
            data = self.rpc['venus/sun/reply/edit'](id=None, data=data).unwrap()
        except Exception as e:
            error_logger.error(u'编辑评论失败%s', e)
            raise
        return {'data': data}


class ReplyVote(APIView):
    def post(self, request):

        reply_id = int(request.POST.get('reply_id'))
        vote_num = int(request.POST.get('vote_num'))

        try:
            self.rpc['venus/sun/reply/vote'](reply_id=reply_id, virtual_vote_num=vote_num).unwrap()
        except Exception as e:
            error_logger.error(u'编辑帖子失败%s', e)
            raise

        return {
            'message': '操作成功'
        }


class ReplyManageListView(APIView):
    def get(self, request):
        page = int(request.GET.get('page', 1))
        limit = int(request.GET.get('limit', 10))
        topic_id = request.GET.get('topic_id', None)
        content = request.GET.get('content', None)
        topic_content = request.GET.get('topic_content', None)
        user_name = request.GET.get('user_name', None)
        belong_type = request.GET.get('belong_type', None)
        reply_level = request.GET.get('reply_level', None) #历史问题  后期优化
        reply_user_type = request.GET.get('reply_user_type', None)
        replied_user_type = request.GET.get('replied_user_type', None)
        is_online = request.GET.get('is_online', None)
        from_create_time = request.GET.get('from_create_time', None)
        to_create_time = request.GET.get('to_create_time', None)
        from_topic_create_time = request.GET.get('from_topic_create_time', None)
        to_topic_create_time = request.GET.get('to_topic_create_time', None)

        filters = {}
        if topic_content:
            filters['topic_content'] = topic_content.strip()
        if reply_user_type:
            reply_user_type = int(reply_user_type)
            if reply_user_type is not None:
                filters['reply_user_type'] = reply_user_type
        if replied_user_type:
            replied_user_type = int(replied_user_type)
            if replied_user_type is not None:
                filters['replied_user_type'] = replied_user_type
        if from_topic_create_time and to_topic_create_time:
            filters['topic_create_time__gte'] = from_topic_create_time
            filters['topic_create_time__lte'] = to_topic_create_time
        if user_name:
            filters['user_name'] = user_name
        if topic_id:
            filters['topic_id'] = int(topic_id)
        if belong_type:
            if belong_type in REPLY_BELONG_TYPE:
                filters['belong_type'] = belong_type

        if is_online:
            is_online = int(is_online)
            filters['is_online'] = is_online
        if from_create_time and to_create_time:
            filters['create_time__gte'] = from_create_time
            filters['create_time__lte'] = to_create_time

        other_filters = {}
        if content:
            other_filters['content__contains'] = content.strip()\

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


class ReplyManageCreateView(APIView):

    def post(self, request):
        id_ = request.POST.get('id', None)
        replied_id = request.POST.get('replied_id')
        topic_id = request.POST.get('topic_id')
        pictorial_id = request.POST.get('pictorial_id')
        user_id = request.POST.get('user_id')
        content = request.POST.get('content')

        data = {
            'replied_id': replied_id,
            'topic_id': topic_id if topic_id else 0,
            'pictorial_id': pictorial_id if pictorial_id else 0,
            'user_id': user_id,
            'content': content,
            'type': REPLY_TYPE.PUPPET_REPLY_ANS,
        }
        try:
            rep = self.rpc['venus/sun/reply/manage/edit'](id_=id_, data=data).unwrap()
        except Exception as e:
            error_logger.error(u'创建/编辑%s-reply信息失败%s' % (id, e))
            raise
        return {
            "message": '更新成功',
            "id": rep['id']
        }


class ReplyManageBatchUpdate(APIView):

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

        data = { }

        if request.POST.get('is_online') is not None:
            data['is_online'] = True if int(request.POST.get('is_online', 0)) else False
        if request.POST.get('is_quality') is not None:
            data['is_quality'] = True if int(request.POST.get('is_quality', 0)) else False

        try:
            rep = self.rpc['venus/sun/reply/manage/batch/update'](ids=ids, data=data).unwrap()
        except Exception as e:
            error_logger.error(u'更新-reply信息失败%s' % (e))
            raise
        return {
            "message": '更新成功',
        }


class ReplyManageBatchMove(APIView):

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

        try:
            self.rpc['venus/sun/reply/manage/batch/move'](ids=ids, topic_id=topic_id, pictorial_id=pictorial_id).unwrap()
        except Exception as e:
            error_logger.error(u'平移reply失败%s' % (e))
            raise
        return {
            "message": '更新成功',
        }


class ReplyManageEdit(APIView):

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

        try:
            self.rpc['venus/sun/reply/edit'](id=id_, data={'content': content}).unwrap()
        except Exception as e:
            error_logger.error(u'平移reply失败%s' % (e))
            raise
        return {
            "message": '更新成功',
        }
