#!/usr/bin/env python
#
# @file    ExtensionFunctions.py
# @brief   class to create functions specific to the extension class
# @author  Frank Bergmann
# @author  Sarah Keating
#
# <!--------------------------------------------------------------------------
#
# Copyright (c) 2013-2015 by the California Institute of Technology
# (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
# and the University of Heidelberg (Germany), with support from the National
# Institutes of Health (USA) under grant R01GM070923.  All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
# Neither the name of the California Institute of Technology (Caltech), nor
# of the European Bioinformatics Institute (EMBL-EBI), nor of the University
# of Heidelberg, nor the names of any contributors, may be used to endorse
# or promote products derived from this software without specific prior
# written permission.
# ------------------------------------------------------------------------ -->

from util import strFunctions, global_variables


class ExtensionFunctions():
    """Class for extension functions"""

    def __init__(self, language, package, elements, offset, num_versions=1):
        self.language = language
        self.package = package
        self.elements = elements
        self.offset = offset
        self.num_versions = num_versions

        # derived members
        self.up_package = strFunctions.upper_first(self.package)
        self.cap_language = language.upper()
        self.struct_name = '{0}Extension'.format(self.up_package)

    ########################################################################

    # Functions to get name of package

    # function to write getName
    def write_get_name(self):
        # create comment parts
        title_line = 'Returns the name of this SBML Level&nbsp;3 ' \
                     'package (\"{0}\").'.format(self.package)
        params = []
        return_lines = ['@return a string representing the name of '
                        'this package (\"{0}\").'.format(self.package)]
        additional = []

        # create the function declaration
        arguments = []
        function = 'getName'
        return_type = 'const std::string&'

        # create the function implementation
        implementation = ['return getPackageName()']
        code = [dict({'code_type': 'line', 'code': implementation})]

        # return the parts
        return dict({'title_line': title_line,
                     'params': params,
                     'return_lines': return_lines,
                     'additional': additional,
                     'function': function,
                     'return_type': return_type,
                     'arguments': arguments,
                     'constant': True,
                     'virtual': True,
                     'object_name': self.struct_name,
                     'implementation': code})

    ########################################################################

    # Functions to get uri of package

    # function to write getName
    def write_get_uri(self):
        # create comment parts
        title_line = 'Returns a string representing the {0} XML namespace of ' \
                     'this {0} Level&nbsp;3 package.'.format(self.cap_language)
        params = ['@param {0}Level the level of {1}.'.format(self.language,
                                                             self.cap_language),
                  '@param {0}Version the version of '
                  '{1}.'.format(self.language, self.cap_language),
                  '@param pkgVersion the version of this package.']
        return_lines = ['@return a string representing the name of '
                        'this package (\"{0}\").'.format(self.package)]
        additional = ['The namespace URI constructed by this method '
                      'corresponds to the combination of the Level and '
                      'Version of SBML, and the Version of the SBML '
                      'Level&nbsp;3 package.  (At the time of this writing, '
                      'the only 
SBML Level that supports packages is '
                    
  'Level&nbsp;3, so the value of @p sbmlLevel is '
                      'necessarily always <code>3</code>.)']

        # create the function declaration
        level = '{0}Level'.format(self.language)
        vers = '{0}Version'.format(self.language)
        arguments = ['unsigned int {0}'.format(level),
                     'unsigned int {0}'.format(vers),
                     'unsigned int pkgVersion']
        function = 'getURI'
        return_type = 'const std::string&'

        # create the function implementation
        if self.num_versions == 1:
            bottom_if = self.create_code_block('if',
                                               ['pkgVersion == 1',
                                                'return getXmlnsL3V1V1()'])
        else:
            bottom_if = self.create_code_block('if_else',
                                               ['pkgVersion == 1',
                                                'return getXmlnsL3V1V1()',
                                                'else',
                                                'return getXmlnsL3V1V2()'])
        middle_if = self.create_code_block('if', ['{0} == 1'.format(vers),
                                                  bottom_if])
        code = [self.create_code_block('if', ['{0} == 3'.format(level),
                                              middle_if])]
        implementation = ['static std::string empty = \"\"', 'return empty']
        code.append(self.create_code_block('line', implementation))
        # return the parts
        return dict({'title_line': title_line,
                     'params': params,
                     'return_lines': return_lines,
                     'additional': additional,
                     'function': function,
                     'return_type': return_type,
                     'arguments': arguments,
                     'constant': True,
                     'virtual': True,
                     'object_name': self.struct_name,
                     'implementation': code})

    ########################################################################

    # Functions to get other info of package

    # function to write getLevel/Version/PackageVersion
    def write_get_other(self, other):
        if other == 'Level':
            title_name = 'SBML Level'
            ret_name = 'SBML Level'
        elif other == 'Version':
            title_name = 'Version within the SBML Level'
            ret_name = 'SBML Version within the SBML Level'
        elif other == 'PackageVersion':
            title_name = 'SBML Level&nbsp;3 package version'
            ret_name = 'version of the SBML Level&nbsp;3 package'
        else:
            title_name = ''
            ret_name = ''
        # create comment parts
        title_line = 'Returns the {0} for the given URI  of this ' \
                     'package.'.format(title_name)
        params = ['@param uri the string of the URI that represents one '
                  'of 