#####################################################################
#####################################################################
## Copyright 2005 Steven R. Newcomb                                ##
##                                                                 ##
## This software is licensed under the Apache License, Version 2.0 ##
## (the "License"); you may not use this file except in compliance ##
## with the License.  Copies of the License are available at       ##
##        http://www.apache.org/licenses/LICENSE-2.0               ##
##                                                                 ##
## Unless required by applicable law or agreed to in writing,      ##
## software distributed under the License is distributed on an "AS ##
## IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either ##
## express or implied.  See the License for the specific language  ##
## governing permissions and limitations under the License.        ##
##                                                                 ##
## Copies of the source code and the license are available at      ##
##        http://www.versavant.org                                 ##
#####################################################################
#####################################################################


import sys


###################################
## "Person Name Proxy" PC implementation ##
## (most of the stuff is in Lib/PCproxyIdSet.py)
###################################

#######################################################
def ValueIsInvalidFor_Person_Name_Proxy_Property( vsv, propertyValue, propertyClass, propertyInstance = None, **args):
    TMAName = propertyClass.TMA.TMAName
    topicMap = propertyClass.topicMap

    if len( propertyValue) != 1:
        if propertyInstance:
            vsv.errMsg( 'Every person should have exactly one name, but %s contains %s references to proxies.' % ( propertyInstance.propertyInstanceKey, len( propertyValue)))
        else:
            vsv.errMsg( 'Every person should have exactly one name, but proposed value (%s) for a %s property contains %s references to proxies.' % ( propertyValue, vsv.catKey( TMAName, propertyClass.propertyClassName), len( propertyValue)))
        return True

    if vsv.nowImporting:
        return False  ## can't make the rest of these checks yet

    for proxyId in propertyValue:
        proxy = vsv.isProxyId( proxyId, topicMap)

        foundNameSIPInstance = False
        for propertyInstanceKey2 in proxy.propertyInstances:
            tmaName, propertyClassName = vsv.parsePropertyKey2( propertyInstanceKey2)
            if TMAName == tmaName:
                if propertyClassName == 'Name':
                    foundNameSIPInstance = True
                    break
                
        if not foundNameSIPInstance:
            if propertyInstance:
                vsv.errMsg( 'Proxy referenced in the value of property %s does not have a %s property, but it should.' % ( propertyInstance.propertyInstanceKey, vsv.catKey( TMAName, 'Name')))
            else:
                vsv.errMsg( 'Proxy referenced in the proposed value of a %s property does not have a %s property, but it should.' % ( vsv.catKey( TMAName, propertyClass.propertyClassName), vsv.catKey( TMAName, 'Name')))
            return True
        
    return False

#######################################################
def ValueIsInvalidFor_shoeSizeObservation_Property( vsv, propertyValue, propertyClass, propertyInstance = None, **args):
    TMAName = propertyClass.TMA.TMAName
    topicMap = propertyClass.topicMap

    if len( propertyValue) != 2:
        if propertyInstance:
            vsv.errMsg( 'Every shoe size observation should consist of two proxyIds, but %s contains %s references to proxies.' % ( propertyInstance.propertyInstanceKey, len( propertyValue)))
        else:
            vsv.errMsg( 'Every shoe size observation should consist of two proxyIds, but the proposed value (%s) for a %s property contains %s references to proxies.' % ( propertyValue, vsv.catKey( TMAName, propertyClass.propertyClassName), len( propertyValue)))
        return True

    if vsv.nowImporting:
        return False  ## can't make the rest of these checks yet
    
    proxyIdCount = -1
    for proxyId in propertyValue:
        proxyIdCount += 1
        propertyClassList = [ None, None]
        proxy = vsv.isProxyId( proxyId, topicMap)
        for propertyInstanceKey2 in proxy.propertyInstances:
            tmaName, propertyClassName = vsv.parsePropertyKey2( propertyInstanceKey2)
            if TMAName == tmaName:
                if propertyClassName == 'Shoe Size' or propertyClassName == 'Age':
                    propertyClassList[ proxyIdCount] = propertyClassName
                    break
                
    if not propertyClassList != [ 'Shoe Size', 'Age']:
        if propertyInstance:
            vsv.errMsg( 'The two proxies referenced by the value of property %s should have a "%s" and a "%s" property, respectively, and at least one of them does not meet this criterion (check list == "%s").' % ( propertyInstance.propertyInstanceKey, vsv.catKey( TMAName, 'Shoe Size'), vsv.catKey( TMAName, 'Age'), propertyClassList))
        else:
            vsv.errMsg( 'The two proxies referenced by the value of property %s should have a "%s" and a "%s" property, respectively, and at least one of them does not meet this criterion (check list == "%s").' % ( vsv.catKey( TMAName, propertyClass.propertyClassName), vsv.catKey( TMAName, 'Shoe Size'), vsv.catKey( TMAName, 'Age'), propertyClassList))
        return True
        
    return False

