#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "chenwei"
# Date: 2018/11/15

import json
from utils.base import APIView
from utils.time_utils import analysis_time


class TopicListView(APIView):
    def get(self, request):
        page = int(request.GET.get('page', 0))
        limit = int(request.GET.get('limit', 10))
        filter = self.handle_filter(request.GET.get('filter', ""))
        try:
            data = self.rpc['venus/sun/topic/list'](offset=page, limit=limit, filters=filter).unwrap()
        except Exception as e:
            raise e
        return data


class TopicUpdateOrCreateView(APIView):
    def get(self, request):
        id = request.GET.get('id')

        try: # TODO 少返回了is_online
            data = self.rpc['venus/sun/topic/get'](id=id).unwrap()
        except Exception as e:
            raise e
        return {'data': data}

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

        posting_time = analysis_time(request.POST.get('posting_time', 0))
        topic_images = list(map(lambda x: x[:-2], json.loads(request.POST.get('topic_images', []))))

        data = {
            'topic_images': topic_images,
            'topic_ids': json.loads(request.POST.get('topic_ids', '')),
            'video_url': request.POST.get('video_url', ''),
            'posting_time': posting_time,
            'content': request.POST.get('content', ''),
            'content_level': request.POST.get('content_level', ''),
            'group_name': request.POST.get('group_name', ''),
            'user_id': request.POST.get('user_id', ''),
            'star_name': request.POST.get('star_name', ''),
            'tag_ids': json.loads(request.POST.get('tag_ids', [])),
        }
        try:
            self.rpc['venus/sun/topic/edit'](id=id, data=data).unwrap()
        except Exception as e:
            raise e
        return {
            'message': '更新成功'
        }


class ReplyUpdateOrCreateView(APIView):
    def get(self, request):
        id = request.GET.get('id')
        page = int(request.GET.get('page', 0))
        limit = int(request.GET.get('limit', 10))
        filter = self.handle_filter(request.GET.get('filter', ""))
        filter.update({'id': id})

        try:
            data = self.rpc['venus/sun/topic/reply/list'](offset=page, limit=limit, filters=filter).unwrap()
        except Exception as e:
            data = [
                {
                    'id': 1,
                    'reply_name': '真好',
                    'be_reply_name': '王二',
                    'create_time': '2018-09-10 23:23:23',
                    'reply_type': '贴主的评论',
                    'content': '内容真好',
                },
            ]
        return {
            'total': 10,
            'data': data
        }

    def post(self, request):
        reply_ids = json.loads(request.POST.get('reply_ids', []))
        try:
            self.rpc['venus/sun/topic/reply/batch_delete'](data=reply_ids).unwrap()
        except Exception as e:
            raise e
        return {
            'message': '操作成功'
        }


class ReplyCreate(APIView):
    def post(self, request):
        data = {
            'topic_id': request.POST.get('topic_id'),
            'reply_name': request.POST.get('reply_name'),
            'be_reply_name': request.POST.get('be_reply_name'),
            'content': request.POST.get('content'),
        }
        try:
            data = self.rpc['venus/sun/topic/reply/create'](data=data).unwrap()
            """
                data = {
                 'id': 1,
                 'reply_type': 1,
                'create_time': '2108-09-92 12:12:12',
                'reply_name': 'xxxxx',
                'be_reply_name': 'xxxxxx',
                'content': '测试测试',
                }
            """
        except Exception as e:
            # raise e
            data = {
                 'id': 6,
                 'reply_type': "贴住的评论",
                'create_time': '2108-09-92 12:12:12',
                'reply_name': 'xxxxx',
                'be_reply_name': 'xxxxxx',
                'content': '测试测试',
            }
        return {'data': data}

