#
# 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,
    SIMPHONY1_CLIENT_SECTION,
    restricted_to,
)

logger = logging.getLogger(__name__)

MIGRATION_VERSION = '20.9.15'
CLASS_NAME = 'ConfigMigration200915'


class ConfigMigration200915(ConfigMigration):

    @restricted_to(Config.POS_TYPE_SIMPHONY1)
    def up(self):
        """
        Config directive was incorrectly named "ServiceChargeTender" when it should have been named
        "ChargedTipServiceCharge."
        """
        base_new_directive = "ChargedTipServiceCharge"
        base_old_directive = "ServiceChargeTender"

        old_directives = (directive for directive in self._config.get_configuration().get(SIMPHONY1_CLIENT_SECTION)
                          if directive.startswith("ServiceChargeTender"))

        for old_directive in old_directives:
            old_directive_value = self._config.get_undefaulted_raw_value(SIMPHONY1_CLIENT_SECTION, old_directive)

            if old_directive_value is not None:
                new_directive = old_directive.replace(base_old_directive, base_new_directive)
                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,
                    ),
                )
                self._config.remove_value(
                    self._config.FILE_GENERAL,
                    SIMPHONY1_CLIENT_SECTION,
                    old_directive
                )
                self._config.set_value(
                    self._config.FILE_GENERAL,
                    SIMPHONY1_CLIENT_SECTION,
                    new_directive,
                    old_directive_value
                )
            else:
                logger.info("{old_directive} is unset; skipping config migration".format(
                    old_directive=old_directive,
                ))

    @restricted_to(Config.POS_TYPE_SIMPHONY1)
    def down(self):
        # Purposefully does not remove the 'up' directive.
        base_up_directive = "ChargedTipServiceCharge"
        base_down_directive = "ServiceChargeTender"

        up_directives = (directive for directive in self._config.get_configuration().get(SIMPHONY1_CLIENT_SECTION)
                         if directive.startswith("ChargedTipServiceCharge"))

        for up_directive in up_directives:
            up_directive_value = self._config.get_undefaulted_raw_value(
                SIMPHONY1_CLIENT_SECTION,
                up_directive
            )

            if up_directive_value is not None:
                down_directive = up_directive.replace(base_up_directive, base_down_directive)
                logger.info(
                    "{up_directive} is set to '{up_directive_value}'; "
                    "renaming to {down_directive}".format(
                        up_directive=up_directive,
                        up_directive_value=up_directive_value,
                        down_directive=down_directive,
                    ),
                )
                self._config.set_value(
                    self._config.FILE_GENERAL,
                    SIMPHONY1_CLIENT_SECTION,
                    down_directive,
                    up_directive_value
                )
            else:
                logger.info("{up_directive} is unset; skipping config migration".format(
                    up_directive=up_directive,
                ))
