#
# Copyright 2017 VMware, Inc.  All rights reserved. -- VMware Confidential
#
"""
Contains patch for APIExplorer properties
"""
import logging
from .. import properties_util
from .base import PatchBase
from l10n import localizedString, msgMetadata as _T
from distutils.version import LooseVersion

logger = logging.getLogger(__name__)


class Patch_1_0_0_1(PatchBase):
    """
    Patches apiexplorer configuration properties in endpoint.properties
    It also patches logging configuration for apiexplorer script in endpoint-log4j.properties.
    """

    def __init__(self):
        """
        Constructor
        Set the patch version and summary.
        """

        version = LooseVersion("1.0.0.1")
        summary = localizedString(_T(
            "vapi.endpoint.patch_1_0_0_1.summary",
            "Adds some properties related to apiexplorer to endpoint.properties"))
        PatchBase.__init__(self, version, summary)

    def patch_endpoint_properties(self, patch_context):
        properties_file_path = "/etc/vmware-vapi/endpoint.properties"

        # Backup the original file first
        properties_util.backup(properties_file_path)

        # Append a property if it doesn't already exist
        properties_util.add(properties_file_path, "apiexplorer.enableFiltering", "true")
        properties_util.add(properties_file_path, "apiexplorer.scriptLocation", "/usr/lib/vmware-vapi/apiexplorer/vmodl2swagger.py")
        properties_util.add(properties_file_path, "apiexplorer.scriptOutputDirectory", "/etc/vmware-vapi/apiexplorer/json")


    def patch_endpoint_log4j(self, patch_context):
        properties_file_path = "/etc/vmware-vapi/endpoint-log4j.properties"
        # Backup the original file first
        properties_util.backup(properties_file_path)

        # Append a property if it doesn't already exist
        properties_util.add(properties_file_path, "log4j.logger.com.vmware.vapi.endpoint.rest.APIExplorerAutomationTask", "INFO, vmodl2swagger.info")
        properties_util.add(properties_file_path, "log4j.additivity.com.vmware.vapi.endpoint.rest.APIExplorerAutomationTask", "false")
        properties_util.add(properties_file_path, "log4j.appender.vmodl2swagger.info", "com.vmware.log4j.appender.NonAppendingRollingFileAppender")
        properties_util.add(properties_file_path, "log4j.appender.vmodl2swagger.info.Append", "true")
        properties_util.add(properties_file_path, "log4j.appender.vmodl2swagger.info.Encoding", "UTF-8")
        properties_util.add(properties_file_path, "log4j.appender.vmodl2swagger.info.Threshold", "INFO")
        properties_util.add(properties_file_path, "log4j.appender.vmodl2swagger.info.File", "/var/log/vmware/vapi/endpoint/vmodl2swagger.log")
        properties_util.add(properties_file_path, "log4j.appender.vmodl2swagger.info.MaxFileSize", "10MB")
        properties_util.add(properties_file_path, "log4j.appender.vmodl2swagger.info.MaxBackupIndex", "200")
        properties_util.add(properties_file_path, "log4j.appender.vmodl2swagger.info.layout", "org.apache.log4j.PatternLayout")
        properties_util.add(properties_file_path, "log4j.appender.vmodl2swagger.info.layout.conversionPattern", "%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX} | %-5.5p | %-25.50t | %-30.50c{1} | %m%n")


    def do_patching(self, patch_context):
        self.patch_endpoint_properties(patch_context)
        self.patch_endpoint_log4j(patch_context)
        logger.info('Patch_1_0_0_1 was applied')

