#!/usr/bin/env python
# -*- coding: utf-8 -*-

from gm_types.gaia import REPLYOBJECT
from django.utils import timezone

from talos.libs.datetime_utils import get_timestamp_or_none


class ReplyTool(object):
    """reply tool for user。"""

    # 日记贴回答 + 新帖子评论 + 问题回答 + 回答评论

    def __init__(self, redis_c, user_id):
        self.user_id = user_id
        self.redis_c = redis_c

        self._topic_reply_key_prefix = 't:'
        self._problem_reply_key_prefix = 't:'
        self._tractate_reply_key_prefix = 'tv:'
        self._answer_key_prefix = 'q:'  # 问题回答
        self._answer_reply_key_prefix = 'a:'  # 回答评论
        self._soft_article_reply_key_prefix = 'srr'    #医生后台帖子评论 回复

        self._reply_key_prefix_format = 'user:{user_id}:replies:received'
        self._k = self._reply_key_prefix_format.format(user_id=user_id)

    def _cache_this(self, k):
        now = timezone.now()
        score = get_timestamp_or_none(now)
        self.redis_c.zadd(self._k, score, k)

    def receive_topic_reply(self, topicreply_id):
        v = self._topic_reply_key_prefix + str(topicreply_id)
        self._cache_this(v)

    def remove_topic_reply(self, topicreply_id):
        v = self._topic_reply_key_prefix + str(topicreply_id)
        self._del_cache(v)

    def receive_problem_reply(self, problemreply_id):
        v = self._problem_reply_key_prefix + str(problemreply_id)
        self._cache_this(v)

    def remove_problem_reply(self, problemreply_id):
        v = self._problem_reply_key_prefix + str(problemreply_id)
        self._del_cache(v)

    def receive_tractate_reply(self, tractate_reply_id):
        v = self._tractate_reply_key_prefix + str(tractate_reply_id)
        self._cache_this(v)

    def remove_tractate_reply(self, tractate_reply_id):
        v = self._tractate_reply_key_prefix + str(tractate_reply_id)
        self._del_cache(v)

    def receive_answer(self, answer_id):
        v = self._answer_key_prefix + str(answer_id)
        self._cache_this(v)

    def remove_answer(self, answer_id):
        v = self._answer_key_prefix + str(answer_id)
        self._del_cache(v)

    def receive_answer_reply(self, answer_id):
        v = self._answer_reply_key_prefix + str(answer_id)
        self._cache_this(v)

    def remove_answer_reply(self, answer_id):
        v = self._answer_reply_key_prefix + str(answer_id)
        self._del_cache(v)

    def receive_soft_article_reply(self, soft_article_reply_id):
        v = self._soft_article_reply_key_prefix + str(soft_article_reply_id)
        self._del_cache(v)

    def get_replies_received(self, start_num=0, count=10):
        """return list of tuple as [(id, type), ]"""
        result = self.redis_c.zrevrange(self._k, start_num, start_num + count - 1)
        if not result:
            return []

        res = []
        for r in result:
            t, v = r.decode("utf-8").split(':')   # 数据类型，byte -> str
            t += ':'  # hmmm,,,
            if t == self._topic_reply_key_prefix:
                res.append((v, REPLYOBJECT.TOPIC))

            elif t == self._tractate_reply_key_prefix:
                res.append((v, REPLYOBJECT.TRACTATE))

            elif t == self._answer_key_prefix:
                res.append((v, REPLYOBJECT.ANSWER))

            elif t == self._answer_reply_key_prefix:
                res.append((v, REPLYOBJECT.ANSWER_REPLY))

            elif t == self._soft_article_reply_key_prefix:  # 新增医生短文评论回复7780
                res.append((v, REPLYOBJECT.SOFT_ARTICLE))

        return res

    def get_reply_count(self):
        return self.redis_c.zcard(self._k)

    def _del_cache(self, v):
        self.redis_c.zrem(self._k, v)
