import os
import logging
logging.basicConfig(level=logging.INFO)
from stock.util.LabelChecker import LabelChecker

class LabelMaker:
    db_filename = None
    labelChecker = None

    def __init__(self, RESOURCE_PATH):
        self.labelChecker = LabelChecker(RESOURCE_PATH)
        self.db_filename = os.path.join(RESOURCE_PATH, "hts.db")
        return

    def clearLabel(self, stock_code, ymd):
        self.labelChecker.clearLabel(self.db_filename, stock_code, ymd)
        return

    def showLabels(self, stock_code, ymd):
        self.labelChecker.showLabels(stock_code, ymd)
        return

    def update(self, stock_code, ymd, hms, label):
        self.labelChecker.makeLabel(self.db_filename, stock_code, ymd, hms, label)
        return

    def write(self, stock_code, ymd, outFp):
        logging.info(stock_code + " / " + ymd)
        self.labelChecker.showLabels(stock_code, ymd, outFp)
        return

    def check(self, stock_codes):
        # 종목에 대해서 주어진 일자에 대해서 통계치 추출하기
        for stock_code in stock_codes:
            ymds = self.labelChecker.getDate(stock_code)
            for ymd in ymds:
                logging.info(stock_code + " / " + ymd)
                bsLine, data = self.labelChecker.makeCandidate(stock_code, ymd)
                self.labelChecker.updateLabel(self.db_filename, stock_code, bsLine, data, ymd)
        return

if __name__ == "__main__":
    PROJECT_HOME = os.path.join(os.path.dirname(__file__))
    RESOURCE_PATH = os.path.join(PROJECT_HOME, "resources")

    labelMaker = LabelMaker(RESOURCE_PATH)


    MODE = "WRITE"
    if MODE == "UPDATE":
        # 매일 입력하면서 정답 셋 만들기
        stock_code = "252670"
        ymd = '20220531'

        labelMaker.clearLabel(stock_code, ymd)

        labelMaker.update(stock_code, ymd, "0901", 2)
        labelMaker.update(stock_code, ymd, "0902", 2)
        labelMaker.update(stock_code, ymd, "0903", 2)
        labelMaker.update(stock_code, ymd, "1429", 2)
        labelMaker.update(stock_code, ymd, "1430", 2)
        labelMaker.update(stock_code, ymd, "1431", 2)
        labelMaker.update(stock_code, ymd, "1432", 2)
        labelMaker.update(stock_code, ymd, "1433", 2)
        labelMaker.update(stock_code, ymd, "1434", 2)
        labelMaker.update(stock_code, ymd, "1435", 2)
        labelMaker.update(stock_code, ymd, "1436", 2)
        labelMaker.update(stock_code, ymd, "1437", 2)
        labelMaker.update(stock_code, ymd, "1438", 2)
        labelMaker.update(stock_code, ymd, "1439", 2)
        labelMaker.update(stock_code, ymd, "1440", 2)
        labelMaker.update(stock_code, ymd, "1441", 2)
        labelMaker.update(stock_code, ymd, "1442", 2)
        labelMaker.update(stock_code, ymd, "1443", 2)
        labelMaker.update(stock_code, ymd, "1444", 2)
        labelMaker.update(stock_code, ymd, "1445", 2)
        labelMaker.update(stock_code, ymd, "1446", 2)
        labelMaker.update(stock_code, ymd, "1447", 2)

        labelMaker.update(stock_code, ymd, "0930", 1)
        labelMaker.update(stock_code, ymd, "0931", 1)
        labelMaker.update(stock_code, ymd, "0932", 1)
        labelMaker.update(stock_code, ymd, "0933", 1)
        labelMaker.update(stock_code, ymd, "0934", 1)
        labelMaker.update(stock_code, ymd, "0935", 1)
        labelMaker.update(stock_code, ymd, "1516", 1)
        labelMaker.update(stock_code, ymd, "1517", 1)
        labelMaker.update(stock_code, ymd, "1518", 1)
        labelMaker.update(stock_code, ymd, "1519", 1)


        labelMaker.showLabels(stock_code, ymd)

    else:
        stock_codes = {
            "252670": [
                '20220502', '20220503', '20220504', '20220506', '20220509',
                '20220510', '20220511', '20220512', '20220513', '20220516',
                '20220517', '20220518', '20220519', '20220520', '20220523',
                '20220524', '20220525', '20220526', '20220527', '20220530',
                '20220531', '20220602', '20220603', '20220607', '20220608',
                '20220609', '20220610', '20220613', '20220614', '20220615',
                '20220616', '20220617', '20220620', '20220621', '20220622',
                '20220623', '20220624', '20220627', '20220628', '20220629',
                '20220630', '20220624', '20220627', '20220628', '20220629',
                '20220701', '20220704', '20220705', '20220706', '20220707',
                '20220708', '20220711', '20220712', '20220713', '20220714',
                '20220715', '20220718', '20220719', '20220720', '20220721',
                '20220722', '20220725', '20220726', '20220727', '20220728',
                '20220729', '20220801', '20220802', '20220803', '20220804',
                '20220805', '20220808', '20220809', '20220810', '20220811',
                '20220812', '20220816', '20220817', '20220818', '20220819'],
            "122630": ['20220701', '20220704', '20220705', '20220706', '20220707',
                       '20220708', '20220711', '20220712', '20220713', '20220714',
                       '20220715', '20220718', '20220719', '20220720', '20220721',
                       '20220722', '20220725', '20220726', '20220727', '20220728',
                       '20220729', '20220801', '20220802', '20220803', '20220804',
                       '20220805', '20220808', '20220809', '20220810', '20220811',
                       '20220812', '20220816', '20220817'],
        }

        if MODE == "WRITE":
            for stock_code in stock_codes:
                fileName = os.path.join(RESOURCE_PATH, "tmp", "check_"+stock_code+".txt")
                outFp = open(fileName, "w")
                for ymd in stock_codes[stock_code]:
                    labelMaker.write(stock_code, ymd, outFp)
                outFp.close()
        else:
            labelMaker.check(stock_codes)