from logging_handler import LoggerHandler


class EmVersionHandler:
    """
     class for handling get EM version

    """

    @staticmethod
    def get_em_product_suffix():
        """
        :return: EM product suffix
        """
        return 'NFT'

    @staticmethod
    def get_latest_version(installed_version_path, smart_number_suffix):
        """

        :param installed_version_path: installed versions path
        :param smart_number_suffix: smart number suffix
        :return: latest em version installed ( with fix pack included )
        """
        LoggerHandler.print_message(f"Start retrieving EM latest version from {installed_version_path}",
                                    logger_level='debug')
        smart_numbers_list = EmVersionHandler. \
            get_product_sorted_version_list_from_installed_versions_file(installed_version_path, smart_number_suffix)
        if len(smart_numbers_list) == 0:
            LoggerHandler.print_message(' Version is not found in ' + installed_version_path + ' file')
            return None
        else:
            # return the highest FP found
            #  ' is ' + smart_numbers_list[len(smart_numbers_list) - 1])
            LoggerHandler.print_message('Latest installed version found for product ' + smart_number_suffix +
                                        ' is ' + smart_numbers_list[len(smart_numbers_list) - 1])
            return smart_numbers_list[len(smart_numbers_list) - 1]

    @staticmethod
    def get_product_sorted_version_list_from_installed_versions_file(installed_version_path, smart_number_suffix):
        """
        This method returns list with all EM version installed.

        :param installed_version_path: path for the installed-versions.txt file
        :param smart_number_suffix: smart number suffix
        :return: list with all EM version installed
        """
        smart_numbers_list = []
        LoggerHandler.print_message(f"Searching for all installed version of product - '{installed_version_path}'")

        for line in open(installed_version_path, 'r'):
            line = line.split(' ')[0]  # get only smart number from the current line
            # check if smart_number we are looking for is in the current line - if so - extract the version number
            # and put it the smart number list
            if smart_number_suffix.upper() in line.upper():
                line = line.split(smart_number_suffix + '.', 1)[1]
                smart_numbers_list.append(line)
                LoggerHandler.print_message('Found version match the given product suffix: ' + line,
                                            logger_level='debug')

        if len(smart_numbers_list) > 1:
            # sort the list
            smart_numbers_list.sort()

        return smart_numbers_list

    @staticmethod
    def remove_fix_pack_from_em_version(product_version):
        """

        :param product_version: The em product version
        :return: em product version without fix packs ( 9.0.21.227 -> 9.0.21.200 )
        """
        LoggerHandler.print_message(f"Product version is: {product_version}", logger_level='debug')
        # Split the version number into components
        components = product_version.split('.')
        num = components[-1]
        split_num = list(num)
        split_num[-1] = "0"
        split_num[-2] = "0"
        components[-1] = ''.join(split_num)
        return '.'.join(components)
