# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2002-2007 Nexedi SARL and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions, PropertySheet
from Products.ERP5Type.ERP5Type import ERP5TypeInformation
from Products.ERP5Type.Cache import getReadOnlyTransactionCache

class SolverTypeInformation(ERP5TypeInformation):
  """
    EXPERIMENTAL - DO NOT USE THIS CLASS BESIDES R&D

    A Type Information class which (will) implement
    all Solver related methods
  """
  # CMF Type Definition
  meta_type = 'ERP5 Solver Type Information'
  portal_type = 'Solver Type'

  # Declarative security
  security = ClassSecurityInfo()
  security.declareObjectProtected(Permissions.AccessContentsInformation)

  # Default Properties
  property_sheets = ( PropertySheet.BaseType
                    , PropertySheet.SolverType
                    , PropertySheet.Configurable
                    )

  def conflictsWithSolver(self, other_solver):
    """
    Returns True if the solver conflicts with other_solver. False else.
    """
    if self.getTestedProperty() == other_solver.getTestedProperty():
      return True
    # XXX more condition is needed?
    return False

  def reduceConfigurationList(self, configuration_property_id_list):
    """
    Note: if one reduces production by 10% for one line and one reduces
    production by 20% for another line or for the same line the total
    reduction is 20% for both lines this is the goal of
    reduceConfigurationList
    """
    # XXX real implementation is needed.
    return configuration_property_id_list

  def getDefaultConfigurationPropertyDict(self, configurable):
    """
    Returns a dictionary of default properties for specified
    configurable object
    (implementation)

    configurable -- a configurable document (Solver Decision
                    or Target Solver)
                    
    """
    # Implemented through type based method
    # and using read transaction cache
    if configurable.getPortalType() == 'Solver Decision':
      try:
        solver_portal_type = configurable.getSolverValue().getId()
      except AttributeError:
        return {}
    else:
      solver_portal_type = configurable.getPortalType()

    cache = getReadOnlyTransactionCache(self)
    if cache is not None:
      key = ('getDefaultConfigurationPropertyDict', solver_portal_type)
      try:
        method = cache[key]
      except KeyError:
        method = self._getTypeBasedMethod(
          'getDefaultConfigurationPropertyDict',
          fallback_script_id='Solver_getDefaultConfigurationPropertyDict')
        cache[key] = method
    return method(configurable)

  def getDefaultConfigurationPropertyList(self, id, configurable):
    """
    Returns a list of possible values for a given property
    (public API)

    configurable -- a configurable document (Solver Decision
                    or Target Solver)
    """

  def getDefaultConfigurationProperty(self, id, configurable):
    """
    Returns the default value for a given property
    (public API)

    configurable -- a configurable document (Solver Decision
                    or Target Solver)
    """
