from qa.models.answer import Answer, Question
from qa.models.recommendscore import AnswerRelatedRecommendScore
from data_sync.utils import to_epoch, tzlc
from qa.models.answer import QuestionTag
from utils.rpc import get_rpc_invoker
from qa.models.toutiao import by_contentz_type_id_get_keywords, get_content_star_keywords, get_content_title_keywords, get_content_star_first_keyword

rpc = get_rpc_invoker()


def get_answers(pks):
    answers = Answer.objects.filter(id__in=pks)
    rs = AnswerRelatedRecommendScore.objects.filter(answer_id__in=pks).values(
        'answer_id', 'score'
    )
    rs_dict = {k: v for (k, v) in rs}

    data = []
    for answer in answers:
        try:
            item = {}
            item['id'] = answer.id
            item['good_click'] = answer.get_good_click
            item['user_id'] = answer.user_id
            item['question_id'] = answer.question.id
            item['smart_rank'] = answer.smart_rank()
            item['smart_rank_v2'] = answer.smart_rank_v2()
            item['title'] = answer.question.title
            item['desc'] = answer.question.content
            item['question_type'] = answer.question.question_type
            item['answer'] = answer.content
            item['content_length'] = len(item['answer'])
            item['has_picture'] = answer.get_has_picture()
            item["is_online"] = bool(answer.is_online and answer.question.is_online)
            item['recommend_score'] = rs_dict.get(answer.id, 0)
            item['tag_ids'] = [t.tag_id for t in answer.question.tags]
            tags_id_list = [t.tag_id for t in answer.question.tags]
            item['tag_name'] = QuestionTag.get_name_list(tags_id_list)
            item['tag_name_analyze'] = QuestionTag.get_name_list(tags_id_list)
            item["hot_score"] = answer.get_hot_score_answer()
            # 新标签
            fresh_tag_result = rpc["api/agile_tag/tuple_new_tags"](old_tag_ids=tags_id_list)
            fresh_tag_id_list = list()
            fresh_tag_name_list = list()
            for fresh_tag_id, fresh_tag_name in fresh_tag_result.unwrap():
                fresh_tag_id_list.append(fresh_tag_id)
                fresh_tag_name_list.append(fresh_tag_name)
            item["fresh_tag_ids"] = fresh_tag_id_list
            item["fresh_tag_name"] = fresh_tag_name_list
            item['content_level'] = answer.level
            item['content_type'] = answer.content_type
            item['create_time'] = tzlc(answer.create_time)
            item['create_time_epoch'] = to_epoch(tzlc(answer.create_time))
            item['is_recommend'] = answer.is_recommend
            # item["last_answer_add_time"] = Question.object.filter(answer.question.id).order_by("-create_time")
            item['title_pre'] = answer.question.title
            item['answer_pre'] = answer.content
            item['desc_pre'] = answer.question.content
            item["last_any_reply_time"] = tzlc(answer.get_last_any_reply_time())
            item["content_keyword"] = by_contentz_type_id_get_keywords(id=answer.id, content_type="answer")
            item["content_star_keyword"] = get_content_star_keywords(id=answer.id, content_type="answer")
            item["content_star_first_keyword"] = get_content_star_first_keyword(id=answer.id, content_type="answer")
            # item["title_keyword"] = get_content_title_keywords(id=answer.id, content_type="answer")

            data.append(item)
        except (Answer.DoesNotExist, Question.DoesNotExist):
            pass

    return data


def get_answer_ids(pks):
    questions = Question.objects.filter(id__in=pks)

    ids = []

    for question in questions:
        id = list(question.answers.filter(is_online=True).values_list('id', flat=True))
        ids = ids + id

    return get_answers_rpc(ids)


def get_answers_rpc(pks):
    answers = Answer.objects.filter(id__in=pks)

    rs = AnswerRelatedRecommendScore.objects.filter(answer_id__in=pks).values(
        'answer_id', 'score'
    )
    rs_dict = {k: v for (k, v) in rs}

    data = []
    for answer in answers:
        try:
            item = {}
            item['id'] = answer.id
            item['user_id'] = answer.user_id
            item['question_id'] = answer.question.id
            item['smart_rank'] = answer.smart_rank()
            item['title'] = answer.question.title
            item['desc'] = answer.question.content
            item['question_type'] = answer.question.question_type
            item['answer'] = answer.content
            item["is_online"] = bool(answer.is_online and answer.question.is_online)
            item['recommend_score'] = rs_dict.get(answer.id, 0)
            item['tag_ids'] = [t.tag_id for t in answer.question.tags]
            item['content_level'] = answer.level
            item['content_type'] = answer.content_type
            item['create_time'] = answer.create_time.timestamp()
            item['create_time_epoch'] = to_epoch(tzlc(answer.create_time))
            item['is_recommend'] = answer.is_recommend
            key = {}
            key['id'] = answer.id

            value = {}

            value['value'] = item
            value['key'] = key

            data.append(value)

        except (Answer.DoesNotExist, Question.DoesNotExist):
            pass

    return data
