from extend.models.gaia import DiarySpreadFit
from libs.region import Graph
# from libs.ssdb import SsdbHelper

from gm_types.doris import CARD_TYPE
from gm_types.gaia import DIARY_CONTENT_LEVEL


g = Graph.buildGraph()

CANDIDATE_LEVEL = (DIARY_CONTENT_LEVEL.FINE, DIARY_CONTENT_LEVEL.EXCELLENT)


# class FeedBuilder(object):
#     def __init__(self, ssdb_client, device_id, feed_type="diary"):
#         """
#
#         :param ssdb_client:
#         :param device_id:
#         :param feed_type:
#         """
#         self.ssdb_client = ssdb_client
#         self.device_id = device_id
#         self.type = feed_type
#
#     def get(self, count, **kwargs):
#         """
#         get feed.
#         :param count: feed size
#         :param kwargs level 评级, city_id 当前城市
#         :return:
#         """
#         level = kwargs.get("content_level")
#         cityid = kwargs.get("city_id")
#
#         local, nearby = kwargs.get("local", 0), kwargs.get("nearby", 0)
#
#         if local == nearby == 0:
#             try:
#                 spread = DiarySpreadFit.objects.get(city_id=cityid, is_online=True)
#                 local = spread.commoncity
#                 nearby = spread.bescity - local
#             except DiarySpreadFit.DoesNotExist:
#                 local = nearby = count / 2
#
#         unread_key = SsdbHelper.getQueueKey(self.type, self.device_id)
#         readed_key = SsdbHelper.getQueueKey(self.type, self.device_id, True)
#         unread = self.ssdb_client.zrange(unread_key, 0, -1, True)
#         readed = self.ssdb_client.zrange(readed_key, 0, -1, True)
#
#         unread_zset = ZsetBuilder(unread)
#         readed_zset = ZsetBuilder(readed)
#
#         if self.type == CARD_TYPE.DIARY:
#             data = unread_zset.popLocal(local, city_id=cityid, content_level=level)
#             data.extend(unread_zset.popNearby(nearby, city_id=cityid))
#         else:
#             data = unread_zset.popAny(count)
#
#         readed_zset.items.extend(data)
#
#         self.ssdb_client.zclear(unread_key)
#         self.ssdb_client.zclear(readed_key)
#
#         if len(data) < count or len(unread_zset.items) < count:
#             """
#             循环队列
#             """
#             unread_zset.items.extend(readed_zset.items)
#             if len(unread_zset.rebuild()):
#                 self.ssdb_client.multi_zset(unread_key, *unread_zset.rebuild())
#         else:
#             """
#             """
#             if len(unread_zset.rebuild()):
#                 self.ssdb_client.multi_zset(unread_key, *unread_zset.rebuild())
#             if len(readed_zset.rebuild()):
#                 self.ssdb_client.multi_zset(readed_key, *readed_zset.rebuild())
#
#         return [obj['pk'] for obj in data]

# class ZsetBuilder(object):
#     def __init__(self, raw, readed=False):
#         """
#
#         :param raw:
#         :param readed:
#         """
#         self.raw = raw
#         self.readed = readed
#         self.items = []
#         self.initialize()
#
#     def initialize(self):
#         for i in range(0, len(self.raw), 2):
#             key = self.raw[i].decode()
#             pk, author_id, content_level, city_id = key.split(":")
#             self.items.append({
#                 "pk": pk,
#                 "author_id": author_id,
#                 "content_level": content_level,
#                 "city_id": city_id,
#                 "score": self.raw[i + 1].decode(),
#             })
#
#     def remove(self, item):
#         self.items.remove(item)
#
#     def append(self, item):
#         self.items.append(item)
#
#     def rebuild(self):
#         data = []
#         for obj in self.items:
#             data.append("{}:{}:{}:{}".format(obj['pk'], obj['author_id'], obj['content_level'], obj['city_id']))
#             data.append(int(obj['score']))
#         return data
#
#     def popLocal(self, num, city_id, content_level=None):
#         """
#
#         :param num:
#         :param city_id:
#         :param content_level:
#         :return:
#         """
#         data = []
#         author_ids = []
#
#         # 优先取同城，指定content—level的日记
#         for idx, obj in enumerate(self.items):
#             if len(data) >= num:
#                 break
#
#             if not obj or obj['author_id'] in author_ids:
#                 continue
#
#             if city_id == obj['city_id']:
#                 if content_level and content_level != obj['content_level']:
#                     continue
#                 author_ids.append(obj['author_id'])
#                 data.append(obj)
#                 self.items[idx] = None
#
#         # 取临近地域的优秀日记本补足
#         if len(data) < num:
#             for idx, obj in enumerate(self.items):
#                 if len(data) >= num:
#                     break
#
#                 if not obj or obj['author_id'] in author_ids:
#                     continue
#
#                 if g.isNearBy(city_id, obj['city_id']) and obj['content_level'] in CANDIDATE_LEVEL:
#                     author_ids.append(obj['author_id'])
#                     data.append(obj)
#                     self.items[idx] = None
#
#         # 取无地域限制的优秀日记本补足
#         if len(data) < num:
#             for idx, obj in enumerate(self.items):
#                 if len(data) >= num:
#                     break
#
#                 if not obj or obj['author_id'] in author_ids:
#                     continue
#
#                 if obj['content_level'] in CANDIDATE_LEVEL:
#                     author_ids.append(obj['author_id'])
#                     data.append(obj)
#                     self.items[idx] = None
#
#         self.items = list(filter(lambda x: x is not None, self.items))
#
#         return data
#
#     def popNearby(self, num, city_id):
#         """
#
#         :param num:
#         :param city_id:
#         :return:
#         """
#         data = []
#         author_ids = []
#
#         for idx, obj in enumerate(self.items):
#
#             if len(data) >= num:
#                 break
#
#             if not obj or obj['author_id'] in author_ids:
#                 continue
#
#             if g.isNearBy(city_id, obj['city_id']):
#                 author_ids.append(obj['author_id'])
#                 data.append(obj)
#                 self.items[idx] = None
#
#         if len(data) < num:
#             for idx, obj in enumerate(self.items):
#                 if len(data) >= num:
#                     break
#
#                 if not obj or obj['author_id'] in author_ids:
#                     continue
#                 if obj['content_level'] in CANDIDATE_LEVEL:
#                     author_ids.append(obj['author_id'])
#                     data.append(obj)
#                     self.items[idx] = None
#
#         self.items = list(filter(lambda x: x is not None, self.items))
#         return data
#
#     def popAny(self, num):
#         """
#
#         :param num:
#         :return:
#         """
#         data = []
#         author_ids = []
#         for idx, obj in enumerate(self.items):
#             if len(data) >= num:
#                 break
#
#             if not obj or obj['author_id'] in author_ids:
#                 continue
#
#             author_ids.append(obj['author_id'])
#             data.append(obj)
#             self.items[idx] = None
#
#         self.items = list(filter(lambda x: x is not None, self.items))
#         return data



