class TopTrader(QMainWindow, ui):
    def __init__(self):
        super().__init__()
        # self.setupUi(self)  # load app screen
        self.logger = TTlog(logger_name="RealCondi").logger
        self.mongo = MongoClient()
        self.tt_db = self.mongo.TopTrader
        self.slack = Slack(config_manager.get_slack_token())
        self.kw = Kiwoom()
        self.login()

        # ready to search condi
        self.load_stock_info()
        t = datetime.today()
        self.s_time = datetime(t.year, t.month, t.day, 9, 0, 0)  # 장 시작시간, 오전9시

        # fake trading
        self.timer = None
        self.start_timer()

        # core function
        # self.screen_no = 4000
        # self.N1, self.N2 = 0, 10

        self.screen_no = 4001
        self.N1, self.N2 = 10, 20

        self.real_condi_search()

    def load_stock_info(self):
        self.stock_dict = {}
        doc = self.tt_db.stock_information.find({})
        for d in doc:
            code = d["code"]
            self.stock_dict[code] = d
        self.logger.info("loading stock_information completed.")

    def start_timer(self):
        if self.timer:
            self.timer.stop()
            self.timer.deleteLater()
        self.timer = QTimer()
        self.timer.timeout.connect(self.fake_check_to_sell)
        # self.timer.setSingleShot(True)
        self.timer.start(1000)  # 1 sec interval

    def fake_check_to_sell(self):
        """

        :return:
        """

    def search_result(self, event_data):
        """

        :param event_data:
        :return:
        """
        curr_time = datetime.today()

        if curr_time < self.s_time:
            self.logger.info("=" * 100)
            self.logger.info("장 Open 전 입니다. 오전 9:00 이후에 검색 시작합니다.")
            self.logger.info("=" * 100)
            return

        # 실시간 조건검색 이력정보
        self.tt_db.real_condi_search.insert({
            'date':
            curr_time,
            'code':
            event_data["code"],
            'stock_name':
            self.stock_dict[event_data["code"]]["stock_name"],
            'market':
            self.stock_dict[event_data["code"]]["market"],
            'event':
            event_data["event_type"],
            'condi_name':
            event_data["condi_name"]
        })

    def real_condi_search(self):
        # callback fn 등록
        self.kw.reg_callback("OnReceiveRealCondition", "", self.search_result)
        # self.kw.notify_callback["OnReceiveRealCondition"] = self.search_condi

        condi_info = self.kw.get_condition_load()
        self.logger.info("실시간 조건 검색 시작합니다.")
        for condi_name, condi_id in list(condi_info.items())[self.N1:self.N2]:
            # 화면번호, 조건식이름, 조건식ID, 실시간조건검색(1)
            self.logger.info("화면번호: {}, 조건식명: {}, 조건식ID: {}".format(
                self.screen_no, condi_name, condi_id))
            self.kw.send_condition(str(self.screen_no), condi_name,
                                   int(condi_id), 1)
            time.sleep(0.5)

    def login(self):
        err_code = self.kw.login()
        if err_code != 0:
            self.logger.error("Login Fail")
            exit(-1)
        self.logger.info("Login success")