u'''
Created on Aug 8, 2013

@author: Mark V Systems Limited
(c) Copyright 2013 Mark V Systems Limited, All rights reserved.
'''
from arelle import ModelObject, ModelDtsObject, XbrlConst, XmlUtil, ViewFile
from arelle.ModelDtsObject import ModelRelationship
from arelle.ViewUtil import viewReferences
import os

def viewRoleTypes(modelXbrl, outfile, header, isArcrole=False, lang=None):
    modelXbrl.modelManager.showStatus(_(u"viewing arcrole types") if isArcrole else _(u"viewing role types"))
    view = ViewRoleTypes(modelXbrl, outfile, header, isArcrole, lang)
    view.view()
    view.close()
    
class ViewRoleTypes(ViewFile.View):
    def __init__(self, modelXbrl, outfile, header, isArcrole, lang):
        super(ViewRoleTypes, self).__init__(modelXbrl, outfile, header, lang)
        self.isArcrole = isArcrole
        
    def view(self):
        # determine relationships indent depth for dimensions linkbases
        # set up treeView widget and tabbed pane
        if self.isArcrole:
            heading = [u"Arcrole URI", u"Definition", u"Cycles Allowed", u"Used On"]
            xmlRowElementName = u"arcroleType"
            uriAttr = u"arcroleURI"
        else:
            heading = [u"Role URI", u"Definition", u"Used On"]
            xmlRowElementName = u"roleType"
            uriAttr = u"roleURI"
        self.addRow(heading, asHeader=True) # must do after determining tree depth
            
        roletypes = self.modelXbrl.arcroleTypes if self.isArcrole else self.modelXbrl.roleTypes
        for roleUri in sorted(roletypes.keys()):
            for modelRoleType in roletypes[roleUri]:
                attr = {u"definedIn": modelRoleType.modelDocument.basename}
                cols = [roleUri, modelRoleType.genLabel(lang=self.lang, strip=True) or modelRoleType.definition or u'']
                if self.isArcrole:
                    cols.append(modelRoleType.cyclesAllowed)
                cols.append(u', '.join(unicode(usedOn)
                                      for usedOn in modelRoleType.usedOns))
                     
                self.addRow(cols, treeIndent=0, xmlRowElementName=xmlRowElementName, xmlRowEltAttr=attr)

