import json
from pathlib import Path


class PlannerLoad:
    base_dir: Path
    account_values: list = None
    account_info: list = None
    plan_values: list = None
    marketing_values: list = None
    structure: list
    config = {
        'department': [1, 2, 3, 10, 30, 40, 50, 55, 81, 82],
        'translation': [10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
                        20, 21, 22, 23, 3, 4, 5, 6, 7, 4, 0, 4,
                        4, 4, 1, 4, 2, 4, 8, 9]
    }

    def __init__(self, base_dir: str):
        self.base_dir = Path(base_dir)

    def new_file(self, year: str):
        with open(self.base_dir.joinpath(f'accounts_{year}.json'), 'r') as frh:
            self.account_values = json.load(frh)['values']
        with open(self.base_dir.joinpath(f'planning_{year}.json'), 'r') as frh:
            self.plan_values = json.load(frh)['values']
        with open(self.base_dir.joinpath(f'marketing_{year}.json'), 'r') as frh:
            self.marketing_values = json.load(frh)
        with open(self.base_dir.joinpath('gcstruct.json'), 'r') as frh:
            gcstruct = json.load(frh)
        self.account_info = dict([(a['Konto_Nr'], a) for a in gcstruct['accounts']])
        return self.convert_file(gcstruct['flat']['Struktur_FB'])

    def convert_file(self, structure_source):
        self.structure = []
        for s in structure_source:
            accts = s['accounts']
            s['accounts'] = []
            s['planlevel'] = (len(accts) > 0)
            self.structure.append(s)

            for a in accts:
                if type(a) == str:
                    acct = {
                        'text': a + ' - ' + self.account_info[a]['Konto_Bezeichnung'],
                        'id': a,
                        'costcenter': self.account_info[a]['Konto_KST'],
                        'values': [],
                        'values2': None
                    }
                else:
                    acct = a
                    a = acct['id']
                acct['id'] = s['id'].replace(';;', ';' + acct['text'] + ';', 1)
                s['children'].append(acct['id'])

                new_account = {
                    "id": acct['id'],
                    "text": acct['text'],
                    "children": [],
                    "children2": [],
                    "parents": [s['id']] + s['parents'],
                    "accounts": [a],
                    "costcenter": acct['costcenter'],
                    "level": s['level'] + 1,
                    "drilldown": False,
                    "form": s['form'],
                    "accountlevel": False,
                    "absolute": True,
                    "seasonal": True,
                    "status": "0",
                    "values": acct['values'],
                    "values2": acct['values2']
                }
                self.structure.append(new_account)

        for s in self.structure:
            s['values2'] = self.get_values2(s)
            s['options'] = self.get_options(s)
            del s['accountlevel']
            del s['absolute']
            del s['seasonal']
            del s['status']
            s['sumvalues'] = [0] * 30

        return self.structure

    def get_values2(self, s):
        a_values = {}
        m_values = {}
        if s['accounts'] and s['values2'] is None:
            a_values = self.account_values.get(s['accounts'][0], dict())
            m_values = self.marketing_values.get(s['accounts'][0], dict())
        p_values = {}
        if self.plan_values is not None:
            p_values = self.plan_values.get(s['id'], dict())

        values = dict([(str(d), [0] * 30) for d in self.config['department']])
        for d in values.keys():
            if s['values2'] is not None and d in s['values2']:
                if len(s['values2'][d]) == 30:
                    values[d] = s['values2'][d]
                else:
                    values[d] = [s['values2'][d][i] if 0 <= i < len(s['values2'][d]) else 0
                                 for i in self.config['translation']]
            if d in p_values:
                for i, v in enumerate(p_values[d], 28):
                    values[d][i] = v
            if d in a_values:
                for i, v in enumerate(a_values[d], 20):
                    values[d][i] = v
            if d in m_values:
                for i, v in enumerate(m_values[d], 1):
                    values[d][i] = v
                values[d][0] = sum(m_values[d])
                values[d][14] = sum(m_values[d])
        return values

    def convert_values2(self, s):
        a_values = {}
        if s['accounts']:
            a_values = self.account_values.get(s['accounts'][0], dict())
        p_values = self.plan_values.get(s['id'], dict())
        values = dict([(str(d), [0] * 30) for d in self.config['department']])
        for d in values.keys():
            if d in s['values2']:
                if len(s['values2'][d]) == 30:
                    values[d] = s['values2'][d]
                else:
                    values[d] = [s['values2'][d].get(i, 0) for i in self.config['translation']]
            if d in p_values:
                for i, v in enumerate(p_values[d], 28):
                    values[d][i] = v
            if d in a_values:
                for i, v in enumerate(a_values[d], 20):
                    values[d][i] = v
        return values

    def get_options(self, s):
        status = "0"
        seasonal = True
        absolute = True
        planlevel = False
        if isinstance(s['absolute'], bool):
            absolute = s['absolute']
            s['absolute'] = {}
        if isinstance(s['seasonal'], bool):
            seasonal = s['seasonal']
            s['seasonal'] = {}
        if isinstance(s['status'], str):
            status = s['status']
            s['status'] = {}
        if 'planlevel' in s:
            planlevel = s['planlevel']

        opts = dict([(str(d), {
            'absolute': absolute,
            'seasonal': seasonal,
            'status': status,
            'planlevel': planlevel
        }) for d in self.config['department']])

        for d in opts.keys():
            if d in s['absolute']:
                opts[d]['absolute'] = s['absolute'][d]
            if d in s['seasonal']:
                opts[d]['seasonal'] = s['seasonal'][d]
            if d in s['status']:
                opts[d]['status'] = s['status'][d]
        return opts


if __name__ == '__main__':
    planner_dir = Path(__file__).parent.parent.joinpath('export')
    p_load = PlannerLoad(planner_dir)
    print(p_load.convert_file(
        json.load(open(planner_dir.joinpath('2022_V1.json'), 'r'))
    )[12])
