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

logger = logging.getLogger(__name__)

MIGRATION_VERSION = '20.3.26'
CLASS_NAME = 'ConfigMigration200326'


class ConfigMigration200326(ConfigMigration):

    @restricted_to(Config.POS_TYPE_XPIENT)
    def up(self):
        """
        Rename the SurchargeItem config directive to ServiceChargeItems which now supports a list of items to treat as
        service charges.
        """
        old_directive = "SurchargeItem"
        old_directive_value = self._config.get_undefaulted_raw_value(XPIENT_CLIENT_SECTION, old_directive)
        new_directive = "ServiceChargeItems"

        # The "set" method ignores defaults, ensuring the value appears in the INI file
        if old_directive_value is not None:
            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, XPIENT_CLIENT_SECTION, old_directive)
            self._config.set_value(self._config.FILE_GENERAL, XPIENT_CLIENT_SECTION, new_directive, old_directive_value)
        else:
            logger.info("{old_directive} is unset; skipping config migration".format(
                old_directive=old_directive,
            ))
