# from libs import osaf
import os
import platform
import sys

from libs.spider_logger.logger import SpiderLog


class PlatformDetector:

    def __init__(self, logger : SpiderLog):
        self.log = logger

    def nomalize_arch(self, architecture):

        r = architecture
        amd64_list = [
            'amd64',
            'x86-64',
            '64bit'
        ]

        if architecture.lower() in amd64_list:
            r = 'x86-64'

        return r

    def detect(self):
        pi = PlatformInfo()

        pi.basic_platform = platform.system()
        if (self.is_windows_nt()):
            version = platform.win32_ver()[0]
            architecture = platform.machine()
            pi.arch = self.nomalize_arch(architecture)
            pi.distribution = OS_distr_info()
            pi.distribution.name = pi.basic_platform + ' ' + version
            # pi.distribution.release =
            pi.distribution.edition = platform.win32_edition()
            pi.distribution.build = platform.win32_ver()[1]
            # self.log.d(vars(pi))
            # self.log.d(vars(pi.distribution))
        return pi

    def is_windows_nt(self):
        checks = [
            os.name == 'nt',
            'Windows' in platform.system(),
            sys.platform.startswith('win'),
            'Windows_NT' in os.environ
        ]
        return any(checks)

class OS_distr_info:
    name = None
    family = None
    release = None
    edition = None
    build = None

class PlatformInfo:
    basic_platform = None
    kernel = None
    distribution = None
    arch = None
