#
# OMNIVORE CONFIDENTIAL
# __________________
#
#  [2013] - [2020] Omnivore Technologies
#  All Rights Reserved.
#
# NOTICE:  All information contained herein is, and remains
# the property of Omnivore Technologies and its suppliers,
# if any.  The intellectual and technical concepts contained
# herein are proprietary to Omnivore Technologies
# and its suppliers and may be covered by U.S. and Foreign Patents,
# patents in process, and are protected by trade secret or copyright law.
# Dissemination of this information or reproduction of this material
# is strictly forbidden unless prior written permission is obtained
# from Omnivore Technologies.
#
import logging

from agent_common.config import Config

from agent_updater.config_migration.config_migration import (
    ConfigMigration,
    MICROS3700_CLIENT_SECTION,
    restricted_to,
)

logger = logging.getLogger(__name__)

MIGRATION_VERSION = '20.4.29'
CLASS_NAME = 'ConfigMigration200429'


class ConfigMigration200429(ConfigMigration):

    @restricted_to(Config.MICROS3700_POS_TYPE)
    def up(self):
        """
        Replace TenderTypeOverride with SvcTotTender.
        """
        old_directive = "TenderTypeOverride"
        old_directive_value = self._config.get_undefaulted_raw_value(MICROS3700_CLIENT_SECTION, old_directive)

        new_directive = "SvcTotTender"
        existing_directive_value = self._config.get_undefaulted_raw_value(MICROS3700_CLIENT_SECTION, new_directive)
        new_directive_value = None

        if old_directive_value is not None:

            self._config.remove_value(self._config.FILE_GENERAL, MICROS3700_CLIENT_SECTION, old_directive)

            if not existing_directive_value:
                logger.info(
                    "{old_directive} is set to '{old_directive_value}'; "
                    "renaming to {new_directive}".format(
                        old_directive=old_directive,
                        old_directive_value=old_directive_value,
                        new_directive=new_directive,
                    ),
                )
                new_directive_value = old_directive_value

            elif ',' in existing_directive_value:
                keyed_values = str(existing_directive_value).split(',')

                if ':' in keyed_values[-1:][0]:
                    new_directive_value = "{existing_directive_value},{old_directive_value}".format(
                        existing_directive_value=existing_directive_value,
                        old_directive_value=old_directive_value,
                    )

            if new_directive_value:
                logger.info(
                    "{old_directive} is set to '{old_directive_value}'; "
                    "{new_directive} is set to '{existing_directive_value}'; "
                    "appending default value to {new_directive}".format(
                        old_directive=old_directive,
                        old_directive_value=old_directive_value,
                        new_directive=new_directive,
                        existing_directive_value=existing_directive_value,
                    ),
                )
                self._config.set_value(self._config.FILE_GENERAL, MICROS3700_CLIENT_SECTION, new_directive, new_directive_value)
            else:
                logger.info(
                    "{new_directive} already has a default value set; "
                    "removing {old_directive}".format(
                        old_directive=old_directive,
                        new_directive=new_directive,
                    ),
                )

        else:
            logger.info("{old_directive} is unset; skipping config migration".format(
                old_directive=old_directive,
            ))
