# -*- coding: utf-8 -*-
#
# File: d3Material.py
#


__author__ = """unknown <unknown>"""
__docformat__ = 'plaintext'

from AccessControl import ClassSecurityInfo
from Products.Archetypes.atapi import *
from Products.ZWContentTypes.config import *
from Products.CMFCore.utils import getToolByName
##code-section module-header #fill in your manual code here
##/code-section module-header

schema = Schema((


    StringField(
        name='referenceNumber',
        widget=StringWidget(
            label="Material Reference",
            label_msgid='ZWContentTypes_label_referencenumber',
            i18n_domain='ZWContentTypes',
        ),
        schemata="Associate 3D material to Material Card"
    ),

    ReferenceField(
        name='searchItemMaterialCardlibrary',
        multiValued = 0,
        relationship='material',
        allowed_types=('MATERIALCARD','FABRICCARD','TRIMCARD','PACKAGINGCARD','GRAPHICCARD'),
        widget=ReferenceWidget(
            label="Search Item Material Card library",
            label_msgid='ZWContentTypes_label_searchItemMaterialCardlibrary',
            i18n_domain='ZWContentTypes',
            format='select',
        ),
        schemata="Associate 3D material to Material Card",
        vocabulary='get_material_cards_for_item',
    ),

    StringField(
        name='d3materialname',
        widget=StringWidget(
            label="3D Material Name",
            label_msgid='ZWContentTypes_label_d3materialname',
            i18n_domain='ZWContentTypes',     
        ),
        schemata="Create 3D Material"
    ),

    StringField(
        name='d3referenceNumber',
        widget=StringWidget(
            label="3D Material Reference Number",
            label_msgid='ZWContentTypes_label_referenceNumber',
            i18n_domain='ZWContentTypes',
        ),
        schemata="Create 3D Material"
    ),

    StringField(
        name='patternSystem3d',
        widget=SelectionWidget(
            label="3D Pattern System",
            label_msgid='ZWContentTypes_label_patternSystem3d',
            i18n_domain='ZWContentTypes',
            format='select',
        ),
        schemata="Create 3D Material",
        vocabulary=["Optitex"," Lectra"," Gerber"," Other"]
    ),

    TextField(
        name='comments',
        widget=TextAreaWidget(
            label="Comments",
            label_msgid='ZWContentTypes_label_comments',
            i18n_domain='ZWContentTypes',
        ),
        schemata="Create 3D Material"
    ),

    ImageField(
        name='defaultimage',
        widget=ImageWidget(
            label='Uplaod Image',
            label_msgid='ZWContentTypes_label_Image',
            i18n_domain='ZWContentTypes',
        ),
        sizes= {'preview' : (400, 400),
        'mini'    : (200, 200),
        'thumb'   : (128, 128),
        'tile'    :  (64, 64),
        'icon'    :  (32, 32),
        'listing' :  (16, 16),
        },
        storage=AttributeStorage(),
       schemata="Add Default Material View",
    ),


    StringField(
        name='materialFileType',
        widget=SelectionWidget(
            label="3D material File Type",
            label_msgid='ZWContentTypes_label_materialFileType',
            i18n_domain='ZWContentTypes',
            format='select',
        ),
        schemata="Add Default Material View",
        vocabulary=["JPEG","GIF","Other"]
    ),

    TextField(
        name='materialColorCombo',
        widget=StringWidget(
            label="3D Material Color Combo",
            label_msgid='ZWContentTypes_label_materialColorCombo',
            i18n_domain='ZWContentTypes',
        ),
        schemata="Add Default Material View"
    ),

    FileField(
        name='uploadFiles',
        schemata="Add Default Material View",
        widget=FileWidget(
            label="Upload Files",
            label_msgid='ZWContentTypes_label_uploadFiles',
            i18n_domain='ZWContentTypes',
        ),
        storage=AttributeStorage()
    ),

    TextField(
        name='defaultcomments',
        widget=TextAreaWidget(
            label="Comments",
            label_msgid='ZWContentTypes_label_comments',
            i18n_domain='ZWContentTypes',
        ),
        schemata="Add Default Material View"
    ),


    StringField(
        name='combo1',
        widget=StringWidget(
            label="Combo Name",
            label_msgid='ZWContentTypes_label_combo1',
            i18n_domain='ZWContentTypes',
        ),
        schemata="ADD Additional Material Color Combos"
    ),

    ImageField(
        name='image1',
        widget=ImageWidget(
            label='Uplaod Image',
            label_msgid='ZWContentTypes_label_Image1',
            i18n_domain='ZWContentTypes',
        ),
        sizes= {'preview' : (400, 400),
        'mini'    : (200, 200),
        'thumb'   : (128, 128),
        'tile'    :  (64, 64),
        'icon'    :  (32, 32),
        'listing' :  (16, 16),
        },
        storage=AttributeStorage(),
       schemata="ADD Additional Material Color Combos",
    ),


    FileField(
        name='file1',
        schemata="ADD Additional Material Color Combos",
        widget=FileWidget(
            label="Upload File",
            label_msgid='ZWContentTypes_label_upload1',
            i18n_domain='ZWContentTypes',
        ),
        storage=AttributeStorage()
    ),

    TextField(
        name='comments1',
        widget=TextAreaWidget(
            label="Comments",
            label_msgid='ZWContentTypes_label_comments',
            i18n_domain='ZWContentTypes',
        ),
        schemata="ADD Additional Material Color Combos"
    ),

    StringField(
        name='combo2',
        widget=StringWidget(
            label="Combo Name",
            label_msgid='ZWContentTypes_label_combo2',
            i18n_domain='ZWContentTypes',
        ),
        schemata="ADD Additional Material Color Combos"
    ),

    ImageField(
        name='image2',
        widget=ImageWidget(
            label='Uplaod Image',
            label_msgid='ZWContentTypes_label_Image2',
            i18n_domain='ZWContentTypes',
        ),
        sizes= {'preview' : (400, 400),
        'mini'    : (200, 200),
        'thumb'   : (128, 128),
        'tile'    :  (64, 64),
        'icon'    :  (32, 32),
        'listing' :  (16, 16),
        },
        storage=AttributeStorage(),
       schemata="ADD Additional Material Color Combos",
    ),


    FileField(
        name='file2',
        schemata="ADD Additional Material Color Combos",
        widget=FileWidget(
            label="Upload File",
            label_msgid='ZWContentTypes_label_uplaod2',
            i18n_domain='ZWContentTypes',
        ),
        storage=AttributeStorage()
    ),

    TextField(
        name='comments2',
        widget=TextAreaWidget(
            label="Comments",
            label_msgid='ZWContentTypes_label_comments',
            i18n_domain='ZWContentTypes',
        ),
        schemata="ADD Additional Material Color Combos"
    ),

    StringField(
        name='combo3',
        widget=StringWidget(
            label="Combo Name",
            label_msgid='ZWContentTypes_label_combo3',
            i18n_domain='ZWContentTypes',
        ),
        schemata="ADD Additional Material Color Combos"
    ),

    ImageField(
        name='image3',
        widget=ImageWidget(
            label='Uplaod Image',
            label_msgid='ZWContentTypes_label_Image3',
            i18n_domain='ZWContentTypes',
        ),
        sizes= {'preview' : (400, 400),
        'mini'    : (200, 200),
        'thumb'   : (128, 128),
        'tile'    :  (64, 64),
        'icon'    :  (32, 32),
        'listing' :  (16, 16),
        },
        storage=AttributeStorage(),
       schemata="ADD Additional Material Color Combos",
    ),


    FileField(
        name='file3',
        schemata="ADD Additional Material Color Combos",
        widget=FileWidget(
            label="Upload File",
            label_msgid='ZWContentTypes_label_upload3',
            i18n_domain='ZWContentTypes',
        ),
        storage=AttributeStorage()
    ),

    TextField(
        name='comments3',
        widget=TextAreaWidget(
            label="Comments",
            label_msgid='ZWContentTypes_label_comments',
            i18n_domain='ZWContentTypes',
        ),
        schemata="ADD Additional Material Color Combos"
    ),

    StringField(
        name='combo4',
        widget=StringWidget(
            label="Combo Name",
            label_msgid='ZWContentTypes_label_combo4',
            i18n_domain='ZWContentTypes',
        ),
        schemata="ADD Additional Material Color Combos"
    ),

    ImageField(
        name='image4',
        widget=ImageWidget(
            label='Uplaod Image',
            label_msgid='ZWContentTypes_label_Image4',
            i18n_domain='ZWContentTypes',
        ),
        sizes= {'preview' : (400, 400),
        'mini'    : (200, 200),
        'thumb'   : (128, 128),
        'tile'    :  (64, 64),
        'icon'    :  (32, 32),
        'listing' :  (16, 16),
        },
        storage=AttributeStorage(),
       schemata="ADD Additional Material Color Combos",
    ),


    FileField(
        name='file4',
        schemata="ADD Additional Material Color Combos",
        widget=FileWidget(
            label="Upload File",
            label_msgid='ZWContentTypes_label_upload4',
            i18n_domain='ZWContentTypes',
        ),
        storage=AttributeStorage()
    ),

    TextField(
        name='comments4',
        widget=TextAreaWidget(
            label="Comments",
            label_msgid='ZWContentTypes_label_comments',
            i18n_domain='ZWContentTypes',
        ),
        schemata="ADD Additional Material Color Combos"
    ),

    StringField(
        name='documentName',
        widget=StringWidget(
            label="Document Name",
            label_msgid='ZWContentTypes_label_documentName',
            i18n_domain='ZWContentTypes',
        ),
        schemata="Related Documents",
        searchable=True
    ),

    TextField(
        name='documentDescription',
        widget=TextAreaWidget(
            label="Document Description",
            label_msgid='ZWContentTypes_label_documentDescription',
            i18n_domain='ZWContentTypes',
        ),
        schemata="Related Documents",
        searchable=True
    ),

    StringField(
        name='documentAuthor',
        widget=StringWidget(
            label="Document Author",
            label_msgid='ZWContentTypes_label_documentAuthor',
            i18n_domain='ZWContentTypes',
        ),
        schemata="Related Documents",
        searchable=True
    ),


    DateTimeField('dateDocumentCreated',
                    widget = CalendarWidget(
                            label="Date Document Created",
                            show_hm=0,
                  ),

        schemata="Related Documents",
    ),

    FileField(
        name='fileDocument',
        widget=FileWidget(
            label="Upload",
            label_msgid='ZWContentTypes_label_fileDocument',
            i18n_domain='ZWContentTypes',
        ),
        storage=AttributeStorage(),
        schemata="Related Documents",
        searchable=True
    ),

),
)

##code-section after-local-schema #fill in your manual code here
##/code-section after-local-schema

d3Material_schema = BaseSchema.copy() + \
    schema.copy()
d3Material_schema['title'].schemata='Associate 3D material to Material Card'
d3Material_schema['id'].schemata='Associate 3D material to Material Card'
d3Material_schema['title'].widget.label = 'Material Name'
##code-section after-schema #fill in your manual code here
##/code-section after-schema

class d3Material(BaseContent):
    """
    """
    security = ClassSecurityInfo()
    __implements__ = (getattr(BaseContent,'__implements__',()),)

    # This name appears in the 'add' box
    archetype_name = 'Material 3D Card'

    meta_type = 'd3Material'
    portal_type = 'd3Material'
    allowed_content_types = []
    filter_content_types = 0
    global_allow = 1
    #content_icon = 'd3Material.gif'
    immediate_view = 'base_view'
    default_view = 'base_view'
    suppl_views = ()
    typeDescription = "d3Material"
    typeDescMsgId = 'description_edit_d3material'

    _at_rename_after_creation = True

    schema = d3Material_schema

    ##code-section class-header #fill in your manual code here
    ##/code-section class-header

    # Methods
    security.declarePublic('get_material_cards_for_item')
    def get_material_cards_for_item(self):
        """ Get MATERIALCARDs selected for this Item. 
            I am collecting all the material cards for components in that material, may be we will work on some particular BOM. 
            But for now, it is Component based
        """
        catalog = getToolByName(self, 'portal_catalog')
        results  = catalog(portal_type='Component',path=('/').join(self.getItemPhysicalPath()))
        dictRes={}
        for result in results:
          obj = result.getObject() 
          material = obj.getMATERIALCARD_ID()
          if material:
            dictRes[material.UID()]=material.pretty_title_or_id()
        vocab = DisplayList()
        for r in dictRes:
            vocab.add(r, dictRes[r])
        return vocab.sortedByValue()

registerType(d3Material, PROJECTNAME)
# end of class d3Material

##code-section module-footer #fill in your manual code here
##/code-section module-footer




