import cqi
from app import socketio
from app.decorators import socketio_login_required
from . import NAMESPACE as ns
from .utils import cqi_over_socketio


@socketio.on('cqi.corpora.corpus.structural_attributes.get', namespace=ns)
@socketio_login_required
@cqi_over_socketio
def cqi_corpora_corpus_structural_attributes_get(cqi_client: cqi.CQiClient, corpus_name: str, structural_attribute_name: str):  # noqa
    cqi_corpus = cqi_client.corpora.get(corpus_name)
    cqi_structural_attribute = cqi_corpus.structural_attributes.get(structural_attribute_name)  # noqa
    payload = {**cqi_structural_attribute.attrs}
    return {'code': 200, 'msg': 'OK', 'payload': payload}


@socketio.on('cqi.corpora.corpus.structural_attributes.list', namespace=ns)
@socketio_login_required
@cqi_over_socketio
def cqi_corpora_corpus_structural_attributes_list(cqi_client: cqi.CQiClient, corpus_name: str):  # noqa
    cqi_corpus = cqi_client.corpora.get(corpus_name)
    payload = [{**x.attrs} for x in cqi_corpus.structural_attributes.list()]
    return {'code': 200, 'msg': 'OK', 'payload': payload}
