# coding: utf-8

import json

from security.functions import userHasPermission
from ru.curs.celesta.showcase.utils import XMLJSONConverter
from edu._edu_orm import course_cycle_qti_packageCursor
from author._author_orm import eor_lecturesCursor
from uuid import uuid4
try:
    from ru.curs.showcase.core.jython import JythonDTO
    from ru.curs.showcase.app.api import MessageType, UserMessage
except:
    from ru.curs.celesta.showcase import JythonDTO


def cardData(context, main=None, add=None, filterinfo=None, session=None, elementId=None):
    u'''Карточка работы с лекцией'''
    try:
        lectureId = json.loads(session)['sessioncontext']['related']['gridContext']['currentRecordId']
    except:
        lectureId = None
    lectureName = None
    lectureRef = None
    
    if add == 'edit':
        lectureCur = eor_lecturesCursor(context)
        if lectureCur.tryGet(lectureId):
            lectureName = lectureCur.lecture_name
            lectureRef = lectureCur.lecture_ref
        lectureCur.close()
        
    xformsdata = {"schema":
                    {"@xmlns": "",
                       "lecture": {
                           "context": add,
                           "id": lectureId, 
                           "name": lectureName,
                           "ref": lectureRef}}}
  
    xformssettings = {
        'properties': {
            'event': [
                {
                    '@name': 'single_click',
                    '@linkId': '1',
                    'action': {
                        '@keep_user_settings': 'false',
                        '#sorted': [
                          {'main_context': 'current'},
                          {'datapanel': {
                              '@type': 'current',
                              '@tab': 'current',
                              'element': {
                                  '@id': 'lecturesGrid',
                                  'add_context': ''
                              }
                          }}]
                    }
                },
                {
                    '@name': 'single_click',
                    '@linkId': '2',
                    'action': {
                        '@keep_user_settings': 'true',
                        '#sorted': [
                          {'main_context': 'current'},
                          {'datapanel': {
                              '@type': 'current',
                              '@tab': 'current',
                              'element': {
                                  '@id': 'lecturesGrid',
                                  'add_context': ''
                              }
                          }}]
                    }
                }
            ]
        }
    }

    return JythonDTO(XMLJSONConverter.jsonToXml(json.dumps(xformsdata)),
                     XMLJSONConverter.jsonToXml(json.dumps(xformssettings)))


def cardSave(context, main=None, add=None, filterinfo=None, session=None, elementId=None, xformsdata=None):    
    lectureData = json.loads(xformsdata)['schema']['lecture']
    lectureAction = lectureData['context']    
    lectureCur = eor_lecturesCursor(context)
    
    if lectureAction == 'add':
        lectureCur.lecture_id = unicode(uuid4())
        lectureCur.lecture_name = lectureData['name']
        lectureCur.lecture_ref = lectureData['ref']
        lectureCur.insert()        
    elif lectureAction == 'edit':
        lectureId = json.loads(session)['sessioncontext']['related']['gridContext']['currentRecordId']
        if lectureCur.tryGet(lectureId):
            lectureCur.lecture_name = lectureData['name']
            lectureCur.lecture_ref = lectureData['ref']
            lectureCur.update()
    elif lectureAction == 'delete':
        lectureId = json.loads(session)['sessioncontext']['related']['gridContext']['currentRecordId']
        if lectureCur.tryGet(lectureId):
            lectureCur.delete()
    
    lectureCur.close()
