import os
from pathlib import Path
from dataclasses import dataclass


@dataclass
class MailConfig:
    server: str
    port: int
    secure: str
    username: str
    password: str
    email: str


@dataclass
class Cognos7Config:
    program_dir: str
    iqd_dir: str
    models_dir: str
    cube_dir: str
    report_dir: str
    publish_dir: str
    prot_dir: str


def joinpath(path, *other):
    return str(Path(path).joinpath(*other)).lower()


class Config:
    kunde = 'Test'
    version = 'C11'
    versand_separat = False
    cognos7: Cognos7Config
    _cfg = {}

    def __init__(self, ini='GAPS.ini'):
        self._cfg = {}

        self.tools_dir = Path(os.curdir).absolute()
        self.tasks_dir = str(self.tools_dir.parent).lower()
        if self.tools_dir.parent.name.lower() != 'tasks':
            # development
            self.tools_dir = self.tools_dir.joinpath('gctools')
            self.portal_dir = 'c:\\globalcube'
            if not Path(self.portal_dir).exists():
                self.portal_dir = joinpath(self.tools_dir, 'Portal')
        else:
            base_dir = self.tools_dir.parent.parent
            if base_dir.name.lower() != 'globalcube':
                self.version = 'C7'
            self.portal_dir = str(base_dir)

        self.system_dir = joinpath(self.portal_dir, 'System')
        self.tasks_dir = joinpath(self.portal_dir, 'Tasks')
        self.xml_dir = joinpath(self.tasks_dir, 'config')
        self.log_dir = joinpath(self.tasks_dir, 'logs')

        if ':' not in ini:
            ini = joinpath(self.tasks_dir, ini)
        with open(ini, 'r') as stream:
            for line in stream.readlines():
                if '=' in line:
                    key, value = line.split('=')
                    self._cfg[key] = value.replace('"', '').replace('\n', '')
        self.kunde = self._cfg.get('KUNDE', 'Test')
        self.kunde_safe = self.kunde.replace(' ', '-').lower()

        self.system = self._cfg.get('SYSTEM', '')
        if self.system != '':
            self.system_dir = joinpath(self.system_dir, self.system)
        self.mail_config()
        self.cognos7_config()
        self.cognos11_config()

    def mail_config(self):
        self.smtp = MailConfig(**{
            'server': 'mail.global-cube.com',
            'port': '465',
            'secure': 'ssl',
            'username': 'versand',
            'password': 'y6!avXX3tQvr',
            'email': 'versand+neuer-kunde@global-cube.com'
        })
        if self._cfg.get('SMTP_HOST', '') != '':
            secure = {'': '', 'N': '', 'J': 'ssl', 'SSL': 'ssl', 'TLS': 'tls'}

            self.smtp = MailConfig(
                server=self._cfg.get('SMTP_HOST'),
                port=self._cfg.get('SMTP_PORT'),
                secure=secure[self._cfg.get('SMTP_SSL', '')],
                username=self._cfg.get('SMTP_USER'),
                password=self._cfg.get('SMTP_PW'),
                email=self._cfg.get('SMTP_FROM'),
            )
        self.versand_separat = self._cfg.get('VERSAND_SEPARAT') == 'J'

    def cognos7_config(self):
        self.cognos7 = Cognos7Config(
            program_dir=self._cfg.get('COGNOS', 'c:\\program files (x86)\\cognos\\cer5\\bin'),
            iqd_dir=joinpath(self.system_dir, 'IQD'),
            models_dir=joinpath(self.system_dir, 'Models'),
            cube_dir=joinpath(self.system_dir, 'Cube_out'),
            report_dir=joinpath(self.system_dir, 'Report'),
            publish_dir=joinpath(self.portal_dir, 'Publish', 'daten'),
            prot_dir=joinpath(self.system_dir, 'Prot')
        )
        if not Path(self.cognos7.publish_dir).exists():
            self.cognos7.publish_dir = joinpath(self.portal_dir, 'daten'),

    def cognos11_config(self):
        pass
