# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2002, 2004 Nexedi SARL and Contributors. All Rights Reserved.
#                    Jean-Paul Smets-Solanes <jp@nexedi.com>
#                    Romain Courteaud <romain@nexedi.com>
# Copyright (c) 2010 Nexedi SA and Contributors. All Rights Reserved.
#                    Jean-Paul Smets-Solanes <jp@nexedi.com>
#
# 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.
#
##############################################################################
import zope.interface

from warnings import warn
from AccessControl import ClassSecurityInfo

from Products.ERP5Type import Permissions, PropertySheet, interfaces
from Products.ERP5Type.XMLObject import XMLObject
from Products.ERP5Type.XMLMatrix import XMLMatrix

from Products.ERP5.Document.Amount import Amount
from Products.ERP5.Document.MappedValue import MappedValue

from Products.ERP5.Document.Predicate import Predicate

class TransformedResource(MappedValue, XMLMatrix, Amount):
    """
    TransformedResource defines which resource is being transformed
    in order to produce a product define in the parent Transformation
    document.

    TODO:
    - transformations used to work perfectly for more than 3 dimensions
      of variations. However, this feature was broken with time and 
      is no longer usable. It is time to reimplement it. This is 
      completely unrelated to MatrixBox reimplementation unlike
      what is stated in some comments.
    """

    meta_type = 'ERP5 Transformed Resource'
    portal_type = 'Transformed Resource'

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

    # Declarative properties
    property_sheets = ( PropertySheet.Base
                      , PropertySheet.SimpleItem
                      , PropertySheet.CategoryCore
                      , PropertySheet.Amount
                      , PropertySheet.Reference
                      , PropertySheet.TransformedResource
                      )

    # Declarative interfaces
    zope.interface.implements(interfaces.IAmountGenerator,
                              interfaces.IVariated,
                              interfaces.IVariationRange,)
    ### Mapped Value Definition
    # Provide default mapped value properties and categories if
    # not defined
    def getMappedValuePropertyList(self):
      return self._baseGetMappedValuePropertyList() or (
          'converted_quantity', 'efficiency')

    def getMappedValueBaseCategoryList(self):
      result = self._baseGetMappedValueBaseCategoryList()
      if not result:
        if not self.hasCellContent(base_id='variation'):
          result = self.getVariationRangeBaseCategoryList() # The current resource variation
      return result

    def getBaseApplication(self):
      """
      
      """
      # It is OK to try to acquire
      if getattr(self, '_baseGetBaseApplication', None) is not None:
        result = self._baseGetBaseApplication()
        if result:
          return result
      return 'produced_quantity'

    ### Variation matrix definition
    # XXX-JPS Some explanation needed
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'updateVariationCategoryList')
    def updateVariationCategoryList(self):
      """
        Check if variation category list of the resource changed and 
        update transformed resource by doing a set cell range
      """
      self.setQVariationBaseCategoryList(self.getQVariationBaseCategoryList())
      self.setVVariationBaseCategoryList(self.getVVariationBaseCategoryList())

    security.declareProtected(Permissions.ModifyPortalContent, 
                              '_setQVariationBaseCategoryList')
    def _setQVariationBaseCategoryList(self, value):
      """
        Defines the possible base categories which Quantity value (Q)
        variate on
      """
      self._baseSetQVariationBaseCategoryList(value)
      self._updateCellRange('quantity')

    security.declareProtected(Permissions.ModifyPortalContent, 
                              '_setVVariationBaseCategoryList')
    def _setVVariationBaseCategoryList(self, value):
      """
        Defines the possible base categories which Variation value (V)
        variate on
      """
      self._baseSetVVariationBaseCategoryList(value)
      # XXX calling updatecellRange is better
      self._updateCellRange('variation')
      # XXX-JPS This should be handled by interaction workflow or interactor
      # XXX-JPS SO many cases are not handled well...

    security.declareProtected(Permissions.ModifyPortalContent, 
                              'setVVariationBaseCategoryList')
    def setVVariationBaseCategoryList(self, value):
      """
        Defines the possible base categories which Variation value (V)
        variate on and reindex the object
      """
      self._setVVariationBaseCategoryList(value)
      self.reindexObject()