# coding: utf-8

import json

from ru.curs.celesta.showcase.utils import XMLJSONConverter
# from nci import generalSettings
# from security.functions import userHasPermission
from nci._nci_orm import personCursor
from security import settings


def webtext(context, main=None, add=None, filterinfo=None, session=None, elementId=None):
    session_json = json.loads(session)["sessioncontext"]
    person = session_json["fullusername"]
    if settings.loginIsSubject() and settings.isEmployees():
        personCur = personCursor(context)
        if personCur.tryGet(session_json['username']):
            person = personCur.fullname
        
#     userdata = session_json["userdata"]
#     sid = session_json["sid"]

    data = { "div": {
                "@class": "wrap",
                "#sorted": [
                    {"nav": {
                        "@class": "ed-menu dark",
                        "@style": "float: left",
                        "ul": [
                            {"@class": "",
                             "li": [
                                {"@class": "",
                                 'a': {
                                    "@id": 'test_system',
                                    "@href": '.',
                                    "@class": "",
                                    'span': {
                                        "#text": u'СИСТЕМА ТЕСТИРОВАНИЯ'}}}]}]}},
                    {"nav": {
                        "@class": "ed-menu dark",
                        "@style": "float: right",
                        "ul": [
                            {'li': 
                                {'a': {
                                    '@id': "logoutLink", 
                                    '@onclick': "showcaseLogout()",  
                                    '@title': u'Выход из системы',
                                    '@style': 'cursor: pointer;',
                                    'img': {
                                        '@width': "24px",
                                        '@height': "24px",
                                        '@src': 'solutions/default/resources/icons/ic_exit_to_app_white_24dp.png'}}}}]}},
                    {'div': {
                        "@class": "user",
                        'img': {
                            '@width': "24px",
                            '@height': "24px",
                            '@src': 'solutions/default/resources/icons/ic_account_box_white_24dp.png',
                            'span': {
                                '@class': "person",
                                "#text": person}}}}]}}
 
    webtextdata = XMLJSONConverter.jsonToXml(json.dumps(data))

    return webtextdata
