import re

from module.logger import logger

SERVER = ['cn', 'en', 'jp', 'tw']
ARCHIVES_PREFIX = {
    'cn': '档案_',
    'en': 'archives_',
    'jp': 'archives_',
    'tw': '檔案_'
}


class Event:
    def __init__(self, text):
        self.date, self.directory, self.name, self.cn, self.en, self.jp, self.tw = [x.strip() for x in
                                                                                    text.strip('| \n').split('|')]

        self.directory = self.directory.replace(' ', '_')
        self.cn = self.cn.replace('、', '')
        self.en = self.en.replace(',', '').replace('\'', '').replace('\\', '').replace(' ', '_')
        self.jp = self.jp.replace('、', '')
        self.tw = self.tw.replace('、', '')
        self.is_war_archives = self.directory.startswith('war_archives')
        for server in SERVER:
            if self.__getattribute__(server) == '-':
                self.__setattr__(server, None)
            else:
                if self.is_war_archives:
                    self.__setattr__(server, ARCHIVES_PREFIX[server] + self.__getattribute__(server))


class EventExtractor:
    def __init__(self, file):
        logger.info(f'Reading {file}')
        events = []
        with open(file, encoding='utf-8') as f:
            for text in f.readlines():
                if re.search('\d{8}', text):
                    events.append(Event(text))

        self.events = events

    def write(self, file):
        logger.info(f'Writing {file}')
        dic_event = {}
        dic_archives = {}
        for server in SERVER:
            dic_event[server] = {}
            dic_archives[server] = {}
        for event in self.events:
            for server in SERVER:
                name = event.__getattribute__(server)
                if not name:
                    continue
                if event.is_war_archives:
                    dic_archives[server][name] = event.directory
                else:
                    dic_event[server][name] = event.directory

        lines = """
        # This file was automatically generated by dev_tools.event_extractor.
        # Don't modify it manually.
        # See ./campaign/Readme.md
        """
        lines = [line.strip() for line in lines.strip().split('\n')]
        lines.append('')
        lines.append('')

        lines.append('dic_event = {')
        for server in SERVER:
            events = dic_event[server]
            lines.append('    \'%s\': {' % server)
            for key, value in events.items():
                lines.append('        \'%s\': \'%s\',' % (key, value))
            lines.append('    },')
        lines.append('}')

        lines.append('')

        lines.append('dic_archives = {')
        for server in SERVER:
            events = dic_archives[server]
            lines.append('    \'%s\': {' % server)
            for key, value in events.items():
                lines.append('        \'%s\': \'%s\',' % (key, value))
            lines.append('    },')
        lines.append('}')

        with open(file, 'w', encoding='utf-8') as f:
            for text in lines:
                f.write(text + '\n')


e = EventExtractor('./campaign/Readme.md')
e.write('./module/config/event.py')
