# coding:utf-8
import time

import redis
from kafka import TopicPartition

from settings import settings
from v2 import *
from v2.event_attr import EventAttr
from v2.ipregion import IpSearch, Ip2Region
from multiprocessing import Lock

ipsearch = IpSearch(Ip2Region, "ip2region.db")
lock = Lock()

from v2.log import logger

rdb = redis.Redis(**settings.REDIS_CONF)
event_attr = EventAttr(rdb)

partition = 0

def run():
    db_client = CK(**settings.CK_CONFIG)
    sketch = Sketch(db_client)
    handler_event = HandlerEvent(db_client, settings.GAME, ipsearch)
    handler_user = HandlerUser(db_client, settings.GAME)
    transmitter = Transmitter(db_client, settings.GAME, sketch, logger, lock, event_attr,partition)
    transmitter.add_source(handler_event, 1000, 10)
    transmitter.add_source(handler_user, 1000, 10)
    last_ts = int(time.time())
    consumer, kafka_client = create_consumer(partition)

    for msg in consumer():
        data = msg.value
        type_ = data['#type']
        del data['#type']
        ts = int(time.time())

        if 'user' in type_:
            # continue
            obj = getattr(handler_user, type_)
            handler_user.receive_data.append(UserAct(obj, data))
            if len(handler_user.receive_data) >= 1000 or last_ts + 60 < ts:
                last_ts = ts
                handler_user.execute()

        elif 'track' in type_:
            # continue
            obj = getattr(handler_event, type_)
            obj(data)
        elif type_ == settings.STOP_SIGNAL:
            # continue
            # 1 小时内有效
            print(type_)
            if data.get('#time', 0) + 3600 < int(time.time()):
                continue
            kafka_client.close()
            # 停止消费kafka
            print(f'进程{msg.partition} 等待90秒')
            time.sleep(1)
            print(f'进程{msg.partition} 写入数据')
            transmitter.run(kafka_client)
            print(f'进程{msg.partition} 结束')

            break
        elif type_ == 'test':
            print(f'topid->{msg.topic} | partition->{msg.partition} | offset->{msg.offset} | data-> {data}')
        else:
            continue

        transmitter.run(kafka_client)

    while True:
        time.sleep(5)
        print(f'消费分区{partition} 已结束。。。')


if __name__ == '__main__':
    run()
