import transaction
import os.path
from Products.AntriNamai import config
from Products.CMFCore.utils import getToolByName

PRODUCT_DEPENDENCIES = ('RichDocument',
                        'Ploneboard',
                        'Quills',
                        'ImageRepository',
                        'ZWiki')
EXTENSION_PROFILES = ('Products.AntriNamai:default',)

def addBlog(portal):
    """Create School blog."""
    folder_id = 'blog'

    if folder_id not in portal.objectIds():
        portal.invokeFactory('Folder', folder_id, title = "Dokumentai", description = "Bendri mokyklos dokumentai")
    folder = portal._getOb(folder_id)
    folder.setConstrainTypesMode(1)
    folder.setLocallyAllowedTypes(["Folder", "RichDocument", "File"])
    folder.reindexObject() 
    
def addGallery(portal):
    """Add School gallery."""
    folder_id = 'gallery'
    if folder_id not in portal.objectIds():
        portal.invokeFactory('ImageRepository', folder_id, title = "Galerija", description = "Mokyklos nuotraukų galerija.")
    folder = portal._getOb(folder_id)
    folder.setConstrainTypesMode(1)
    folder.setLocallyAllowedTypes(["Image"])
    
def addZWiki(portal):
    """Add ZWiki."""
    folder_id = 'wiki'
    forum = portal.manage_addProduct['ZWiki']
    forum.manage_addWiki(folder_id, 'Wiki')
    
    page_file = open(os.path.join(config.COPY_PATH, 'wiki_default'), 'r')
    page_text = page_file.read()
    
    wiki = portal._getOb(folder_id)
    front_page = wiki._getOb('FrontPage')
    front_page.setTitle('Wiki praginis puslapis')
    front_page.setText(page_text)
    
def addBlog(portal):
    """Add Quills blog."""
    folder_id = 'blog'
    if folder_id not in portal.objectIds():
        portal.invokeFactory('Weblog', folder_id, title = "BLOGas", description = "Mokyklos BLOGas")

    # Adding BlogEntry
    blog_entry_file = open(os.path.join(config.COPY_PATH, 'blog_default_entry'), 'r')
    blog_text = blog_entry_file.read()
    blog = portal._getOb(folder_id)
    blog.invokeFactory('WeblogEntry', 'kas-tai-blogas')
    blog_entry = blog._getOb('kas-tai-blogas')
    blog_entry.setTitle('Kas tai BLOGas')
    blog_entry.setExcerpt('BLOG - internetinis dienoraštis arba automatiškai formatuojami naujienų puslapiai.')
    blog_entry.setText(blog_text)

def install(self, reinstall=False):
    """Install a set of products (which themselves may either use Install.py
    or GenericSetup extension profiles for their configuration) and then
    install a set of extension profiles.
    
    One of the extension profiles we install is that of this product. This
    works because an Install.py installation script (such as this one) takes
    precedence over extension profiles for the same product in 
    portal_quickinstaller. 
    
    We do this because it is not possible to install other products during
    the execution of an extension profile (i.e. we cannot do this during
    the importVarious step for this profile).
    """
    portal_quickinstaller = getToolByName(self, 'portal_quickinstaller')
    portal_setup = getToolByName(self, 'portal_setup')
    portal = getToolByName(self, 'portal_url').getPortalObject()

    for product in PRODUCT_DEPENDENCIES:
        if reinstall and portal_quickinstaller.isProductInstalled(product):
            portal_quickinstaller.reinstallProducts([product])
            transaction.savepoint()
        elif not portal_quickinstaller.isProductInstalled(product):
            portal_quickinstaller.installProduct(product)
            transaction.savepoint()

    for extension_id in EXTENSION_PROFILES:
        portal_setup.runAllImportStepsFromProfile('profile-%s' % extension_id, purge_old=False)
        product_name = extension_id.split(':')[0]
        portal_quickinstaller.notifyInstalled(product_name)
        transaction.savepoint()
    
    addGallery(portal)
    addZWiki(portal)
    addBlog(portal)

