from AccessControl import ClassSecurityInfo
from Products.ATContentTypes.content import schemata
from Products.Archetypes import atapi
from Products.Archetypes.ArchetypeTool import registerType
from Products.CMFCore import permissions
from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.utils import safe_unicode
from bika.lims.browser import BrowserView
from bika.lims.browser.bika_listing import BikaListingView
from bika.lims.config import PROJECTNAME
from plone.app.layout.globals.interfaces import IViewView
from bika.lims import bikaMessageFactory as _
from bika.lims.utils import t
from Products.Archetypes import PloneMessageFactory as _p
from bika.lims.interfaces import IStorageLocations
from bika.lims.content.bikaschema import BikaFolderSchema
from plone.app.content.browser.interfaces import IFolderContentsView
from plone.app.folder.folder import ATFolder, ATFolderSchema
from zope.interface.declarations import implements
from Products.CMFCore.utils import getToolByName
import json
import plone

class StorageLocationsView(BikaListingView):
    implements(IFolderContentsView, IViewView)

    def __init__(self, context, request):
        super(StorageLocationsView, self).__init__(context, request)
        self.catalog = 'bika_setup_catalog'
        self.contentFilter = {'portal_type': 'StorageLocation',
                              'sort_on': 'sortable_title'}
        self.context_actions = {_('Add'):
                            {'url': 'createObject?type_name=StorageLocation',
                             'icon': '++resource++bika.lims.images/add.png'}}
        self.title = self.context.translate(_("Storage Locations"))
        self.icon = self.portal_url + "/++resource++bika.lims.images/storagelocation_big.png"
        self.description = ""
        self.show_sort_column = False
        self.show_select_row = False
        self.show_select_column = True
        self.pagesize = 25

        self.columns = {
            'Title': {'title': _('Storage Location'),
                      'index':'sortable_title'},
            'Room': {'title': _('Room'),
                     'toggle': True},
            'Type': {'title': _('Storage Type'),
                     'toggle': True},
            'Hierarchy': {'title': _('Hierarchy'),
                          'toggle': True},
            'Sample': {'title': _('Sample'),
                          'toggle': True},
        }

        # ________________________________ #
        #    HOCINE ADD POSITION WORKFLOW  #
        # ________________________________ #
        self.review_states = [
            {'id':'default',
             'title': _('Active'),
             'contentFilter': {'inactive_state': 'active',
                               'sort_on': 'created',
                               'sort_order': 'ascending'},
             'transitions': [{'id':'deactivate'},
                             {'id': 'reserve'},
                             {'id': 'occupy'},],
             'columns': ['Title',
                         'Room',
                         'Type',
                         'Hierarchy',
                         'Sample',
                        ]},
            {'id':'inactive',
             'title': _('Dormant'),
             'contentFilter': {'inactive_state': 'inactive'},
             'transitions': [{'id':'activate'}, ],
             'columns': ['Title',
                         'Room',
                         'Type',
                         'Hierarchy',
                         'Sample',
                        ]},
            {'id': 'position_free',
             'title': _('Free'),
             'contentFilter': {'review_state': 'position_free',
                               'sort_on': 'created',
                               'sort_order': 'reverse'},
             'transitions': [{'id': 'deactivate'},
                             {'id': 'reserve'},
                             {'id': 'occupy'}],
             'columns': ['Title',
                         'Room',
                         'Type',
                         'Hierarchy',
                         ]},
            {'id': 'position_reserved',
             'title': _('Reserved'),
             'contentFilter': {'review_state': 'position_reserved',
                               'sort_on': 'created',
                               'sort_order': 'reverse'},
             'transitions': [{'id': 'deactivate'},
                             {'id': 'free'},
                             {'id': 'occupy'}],
             'columns': ['Title',
                         'Room',
                         'Type',
                         'Hierarchy',
                         ]},
            {'id': 'position_occupied',
             'title': _('Occupied'),
             'contentFilter': {'review_state': 'position_occupied',
                               'sort_on': 'created',
                               'sort_order': 'reverse'},
             'transitions': [{'id': 'deactivate'},
                             {'id': 'free'},
                             {'id': 'reserve'},],
             'columns': ['Title',
                         'Room',
                         'Type',
                         'Hierarchy',
                         'Sample',
                         ]},
            {'id':'all',