# coding: utf-8
'''
Created on 27.07.2015
☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦
@author: tr0glo)|(I╠╣
☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦☦
'''
import json
from ru.curs.celesta.showcase.utils import XMLJSONConverter
from fileRepository.functions import getRelativePathById
try:
    from ru.curs.showcase.core.jython import JythonDTO
except:
    from ru.curs.celesta.showcase import JythonDTO
    
from qt._qt_orm import QtiQuestionCursor, QtiVariantQuestionCursor,\
    QtiResourceCursor
from qt.functions import getImageById    

def cardData(context, main=None, add=None, filterinfo=None, session=None, elementId=None):
    u'''Карточка запуска скрипта'''
    variantQuestionId = main
    
    QtiVariantQuestion = QtiVariantQuestionCursor(context)
    QtiVariantQuestion.get(variantQuestionId)
    questionID = QtiVariantQuestion.QuestionID
    QtiVariantQuestion.close()
    
    QtiQuestion = QtiQuestionCursor(context)
    QtiQuestion.get(questionID)    
    questionHtml = QtiQuestion.QuestionHtml if QtiQuestion.QuestionHtml is not None else ''
#     QtiResource = QtiResourceCursor(context)
    questionImg = '/'.join(getRelativePathById(context, str(QtiQuestion.QuestionImg), file_version_id=None))    
#     QtiResource.close()
    QtiQuestion.close()
      
    xformsdata = {"schema":{"@xmlns":"",
                    "description": {
                                "questionType":'7',
                                "question":
                                    {"#text": questionHtml},
                                "question_img_file": questionImg}}}
                   
    xformssettings = {"properties": ""}
     
    data = XMLJSONConverter.jsonToXml(json.dumps(xformsdata))
    data = data.replace('&nbsp;','&amp;nbsp;')
    settings = XMLJSONConverter.jsonToXml(json.dumps(xformssettings))

    return JythonDTO(data, settings)
    