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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# modules/studentact/current_situation_interface-vOK.py

import streamlit as st
import logging
from ..utils.widget_utils import generate_unique_key
import matplotlib.pyplot as plt
import numpy as np
from ..database.current_situation_mongo_db import store_current_situation_result

# Importaciones locales
from translations import get_translations 

from .current_situation_analysis import (
    analyze_text_dimensions, 
    analyze_clarity,
    analyze_vocabulary_diversity, 
    analyze_cohesion,
    analyze_structure,
    get_dependency_depths, 
    normalize_score, 
    generate_sentence_graphs, 
    generate_word_connections, 
    generate_connection_paths,
    create_vocabulary_network, 
    create_syntax_complexity_graph, 
    create_cohesion_heatmap,
    generate_recommendations
)

# Configuración del estilo de matplotlib para el gráfico de radar
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['axes.grid'] = True
plt.rcParams['axes.spines.top'] = False
plt.rcParams['axes.spines.right'] = False

logger = logging.getLogger(__name__)
####################################

TEXT_TYPES = {
    'academic_article': {
        'name': 'Artículo Académico',
        'thresholds': {
            'vocabulary': {'min': 0.70, 'target': 0.85},
            'structure': {'min': 0.75, 'target': 0.90},
            'cohesion': {'min': 0.65, 'target': 0.80},
            'clarity': {'min': 0.70, 'target': 0.85}
        }
    },
    'student_essay': {
        'name': 'Trabajo Universitario',
        'thresholds': {
            'vocabulary': {'min': 0.60, 'target': 0.75},
            'structure': {'min': 0.65, 'target': 0.80},
            'cohesion': {'min': 0.55, 'target': 0.70},
            'clarity': {'min': 0.60, 'target': 0.75}
        }
    },
    'general_communication': {
        'name': 'Comunicación General',
        'thresholds': {
            'vocabulary': {'min': 0.50, 'target': 0.65},
            'structure': {'min': 0.55, 'target': 0.70},
            'cohesion': {'min': 0.45, 'target': 0.60},
            'clarity': {'min': 0.50, 'target': 0.65}
        }
    }
}
####################################

def display_current_situation_interface(lang_code, nlp_models, t):
    """

    Interfaz simplificada con gráfico de radar para visualizar métricas.

    """
    # Inicializar estados si no existen
    if 'text_input' not in st.session_state:
        st.session_state.text_input = ""
    if 'text_area' not in st.session_state:  # Añadir inicialización de text_area
        st.session_state.text_area = ""
    if 'show_results' not in st.session_state:
        st.session_state.show_results = False
    if 'current_doc' not in st.session_state:
        st.session_state.current_doc = None
    if 'current_metrics' not in st.session_state:
        st.session_state.current_metrics = None
        
    try:
        # Container principal con dos columnas
        with st.container():
            input_col, results_col = st.columns([1,2])
            
            with input_col:
                # Text area con manejo de estado
                text_input = st.text_area(
                    t.get('input_prompt', "Escribe o pega tu texto aquí:"),
                    height=400,
                    key="text_area",
                    value=st.session_state.text_input,
                    help="Este texto será analizado para darte recomendaciones personalizadas"
                )
                
                # Función para manejar cambios de texto
                if text_input != st.session_state.text_input:
                    st.session_state.text_input = text_input
                    st.session_state.show_results = False
                
                if st.button(
                    t.get('analyze_button', "Analizar mi escritura"),
                    type="primary",
                    disabled=not text_input.strip(),
                    use_container_width=True,
                ):
                    try:
                        with st.spinner(t.get('processing', "Analizando...")):
                            doc = nlp_models[lang_code](text_input)
                            metrics = analyze_text_dimensions(doc)
                            
                            storage_success = store_current_situation_result(
                                username=st.session_state.username,
                                text=text_input,
                                metrics=metrics,
                                feedback=None
                            )
                            
                            if not storage_success:
                                logger.warning("No se pudo guardar el análisis en la base de datos")
                            
                            st.session_state.current_doc = doc
                            st.session_state.current_metrics = metrics
                            st.session_state.show_results = True
                            
                    except Exception as e:
                        logger.error(f"Error en análisis: {str(e)}")
                        st.error(t.get('analysis_error', "Error al analizar el texto"))
            
            # Mostrar resultados en la columna derecha
            with results_col:
                if st.session_state.show_results and st.session_state.current_metrics is not None:
                    # Primero los radio buttons para tipo de texto
                    st.markdown("### Tipo de texto")
                    text_type = st.radio(
                        "",
                        options=list(TEXT_TYPES.keys()),
                        format_func=lambda x: TEXT_TYPES[x]['name'],
                        horizontal=True,
                        key="text_type_radio",
                        help="Selecciona el tipo de texto para ajustar los criterios de evaluación"
                    )
                    
                    st.session_state.current_text_type = text_type
                    
                    # Luego mostrar los resultados
                    display_results(
                        metrics=st.session_state.current_metrics,
                        text_type=text_type
                    )

    except Exception as e:
        logger.error(f"Error en interfaz principal: {str(e)}")
        st.error("Ocurrió un error al cargar la interfaz")

###################################3333

'''

def display_results(metrics, text_type=None):

    """

    Muestra los resultados del análisis: métricas verticalmente y gráfico radar.

    """

    try:

        # Usar valor por defecto si no se especifica tipo

        text_type = text_type or 'student_essay'

        

        # Obtener umbrales según el tipo de texto

        thresholds = TEXT_TYPES[text_type]['thresholds']



        # Crear dos columnas para las métricas y el gráfico

        metrics_col, graph_col = st.columns([1, 1.5])

        

        # Columna de métricas

        with metrics_col:

            metrics_config = [

                {

                    'label': "Vocabulario",

                    'key': 'vocabulary',

                    'value': metrics['vocabulary']['normalized_score'],

                    'help': "Riqueza y variedad del vocabulario",

                    'thresholds': thresholds['vocabulary']

                },

                {

                    'label': "Estructura",

                    'key': 'structure',

                    'value': metrics['structure']['normalized_score'],

                    'help': "Organización y complejidad de oraciones",

                    'thresholds': thresholds['structure']

                },

                {

                    'label': "Cohesión",

                    'key': 'cohesion',

                    'value': metrics['cohesion']['normalized_score'],

                    'help': "Conexión y fluidez entre ideas",

                    'thresholds': thresholds['cohesion']

                },

                {

                    'label': "Claridad",

                    'key': 'clarity',

                    'value': metrics['clarity']['normalized_score'],

                    'help': "Facilidad de comprensión del texto",

                    'thresholds': thresholds['clarity']

                }

            ]



            # Mostrar métricas

            for metric in metrics_config:

                value = metric['value']

                if value < metric['thresholds']['min']:

                    status = "⚠️ Por mejorar"

                    color = "inverse"

                elif value < metric['thresholds']['target']:

                    status = "📈 Aceptable"

                    color = "off"

                else:

                    status = "✅ Óptimo"

                    color = "normal"

                

                st.metric(

                    metric['label'],

                    f"{value:.2f}",

                    f"{status} (Meta: {metric['thresholds']['target']:.2f})",

                    delta_color=color,

                    help=metric['help']

                )

                st.markdown("<div style='margin-bottom: 0.5rem;'></div>", unsafe_allow_html=True)



        # Gráfico radar en la columna derecha

        with graph_col:

            display_radar_chart(metrics_config, thresholds)



    except Exception as e:

        logger.error(f"Error mostrando resultados: {str(e)}")

        st.error("Error al mostrar los resultados")

'''

######################################
######################################
def display_results(metrics, text_type=None):
    """

    Muestra los resultados del análisis: métricas verticalmente y gráfico radar.

    """
    try:
        # Usar valor por defecto si no se especifica tipo
        text_type = text_type or 'student_essay'
        
        # Obtener umbrales según el tipo de texto
        thresholds = TEXT_TYPES[text_type]['thresholds']

        # Crear dos columnas para las métricas y el gráfico
        metrics_col, graph_col = st.columns([1, 1.5])
        
        # Columna de métricas
        with metrics_col:
            metrics_config = [
                {
                    'label': "Vocabulario",
                    'key': 'vocabulary',
                    'value': metrics['vocabulary']['normalized_score'],
                    'help': "Riqueza y variedad del vocabulario",
                    'thresholds': thresholds['vocabulary']
                },
                {
                    'label': "Estructura",
                    'key': 'structure',
                    'value': metrics['structure']['normalized_score'],
                    'help': "Organización y complejidad de oraciones",
                    'thresholds': thresholds['structure']
                },
                {
                    'label': "Cohesión",
                    'key': 'cohesion',
                    'value': metrics['cohesion']['normalized_score'],
                    'help': "Conexión y fluidez entre ideas",
                    'thresholds': thresholds['cohesion']
                },
                {
                    'label': "Claridad",
                    'key': 'clarity',
                    'value': metrics['clarity']['normalized_score'],
                    'help': "Facilidad de comprensión del texto",
                    'thresholds': thresholds['clarity']
                }
            ]

            # Mostrar métricas
            for metric in metrics_config:
                value = metric['value']
                if value < metric['thresholds']['min']:
                    status = "⚠️ Por mejorar"
                    color = "inverse"
                elif value < metric['thresholds']['target']:
                    status = "📈 Aceptable"
                    color = "off"
                else:
                    status = "✅ Óptimo"
                    color = "normal"
                
                st.metric(
                    metric['label'],
                    f"{value:.2f}",
                    f"{status} (Meta: {metric['thresholds']['target']:.2f})",
                    delta_color=color,
                    help=metric['help']
                )
                st.markdown("<div style='margin-bottom: 0.5rem;'></div>", unsafe_allow_html=True)

        # Gráfico radar en la columna derecha
        with graph_col:
            display_radar_chart(metrics_config, thresholds)

        recommendations = generate_recommendations(
            metrics=metrics,
            text_type=text_type,
            lang_code=st.session_state.lang_code
        )
        
        # Separador visual
        st.markdown("---")
        
        # Título para la sección de recomendaciones
        st.subheader("Recomendaciones para mejorar tu escritura")
        
        # Mostrar las recomendaciones
        display_recommendations(recommendations, get_translations(st.session_state.lang_code))

    except Exception as e:
        logger.error(f"Error mostrando resultados: {str(e)}")
        st.error("Error al mostrar los resultados")


######################################
######################################
def display_radar_chart(metrics_config, thresholds):
    """

    Muestra el gráfico radar con los resultados.

    """
    try:
        # Preparar datos para el gráfico
        categories = [m['label'] for m in metrics_config]
        values_user = [m['value'] for m in metrics_config]
        min_values = [m['thresholds']['min'] for m in metrics_config]
        target_values = [m['thresholds']['target'] for m in metrics_config]

        # Crear y configurar gráfico
        fig = plt.figure(figsize=(8, 8))
        ax = fig.add_subplot(111, projection='polar')

        # Configurar radar
        angles = [n / float(len(categories)) * 2 * np.pi for n in range(len(categories))]
        angles += angles[:1]
        values_user += values_user[:1]
        min_values += min_values[:1]
        target_values += target_values[:1]

        # Configurar ejes
        ax.set_xticks(angles[:-1])
        ax.set_xticklabels(categories, fontsize=10)
        circle_ticks = np.arange(0, 1.1, 0.2)
        ax.set_yticks(circle_ticks)
        ax.set_yticklabels([f'{tick:.1f}' for tick in circle_ticks], fontsize=8)
        ax.set_ylim(0, 1)

        # Dibujar áreas de umbrales
        ax.plot(angles, min_values, '#e74c3c', linestyle='--', linewidth=1, label='Mínimo', alpha=0.5)
        ax.plot(angles, target_values, '#2ecc71', linestyle='--', linewidth=1, label='Meta', alpha=0.5)
        ax.fill_between(angles, target_values, [1]*len(angles), color='#2ecc71', alpha=0.1)
        ax.fill_between(angles, [0]*len(angles), min_values, color='#e74c3c', alpha=0.1)

        # Dibujar valores del usuario
        ax.plot(angles, values_user, '#3498db', linewidth=2, label='Tu escritura')
        ax.fill(angles, values_user, '#3498db', alpha=0.2)

        # Ajustar leyenda
        ax.legend(
            loc='upper right',
            bbox_to_anchor=(1.3, 1.1),  # Cambiado de (0.1, 0.1) a (1.3, 1.1)
            fontsize=10,
            frameon=True,
            facecolor='white',
            edgecolor='none',
            shadow=True
        )

        plt.tight_layout()
        st.pyplot(fig)
        plt.close()

    except Exception as e:
        logger.error(f"Error mostrando gráfico radar: {str(e)}")
        st.error("Error al mostrar el gráfico")

#####################################################
def display_recommendations(recommendations, t):
    """

    Muestra las recomendaciones con un diseño de tarjetas.

    """
    # Definir colores para cada categoría
    colors = {
        'vocabulary': '#2E86C1',  # Azul
        'structure': '#28B463',   # Verde
        'cohesion': '#F39C12',    # Naranja
        'clarity': '#9B59B6',     # Púrpura
        'priority': '#E74C3C'     # Rojo para la categoría prioritaria
    }
    
    # Iconos para cada categoría
    icons = {
        'vocabulary': '📚',
        'structure': '🏗️',
        'cohesion': '🔄',
        'clarity': '💡',
        'priority': '⭐'
    }
    
    # Obtener traducciones para cada dimensión
    dimension_names = {
        'vocabulary': t.get('SITUATION_ANALYSIS', {}).get('vocabulary', "Vocabulario"),
        'structure': t.get('SITUATION_ANALYSIS', {}).get('structure', "Estructura"),
        'cohesion': t.get('SITUATION_ANALYSIS', {}).get('cohesion', "Cohesión"),
        'clarity': t.get('SITUATION_ANALYSIS', {}).get('clarity', "Claridad"),
        'priority': t.get('SITUATION_ANALYSIS', {}).get('priority', "Prioridad")
    }
    
    # Título de la sección prioritaria
    priority_focus = t.get('SITUATION_ANALYSIS', {}).get('priority_focus', 'Área prioritaria para mejorar')
    st.markdown(f"### {icons['priority']} {priority_focus}")
    
    # Determinar área prioritaria (la que tiene menor puntuación)
    priority_area = recommendations.get('priority', 'vocabulary')
    priority_title = dimension_names.get(priority_area, "Área prioritaria")
    
    # Determinar el contenido para mostrar
    if isinstance(recommendations[priority_area], dict) and 'title' in recommendations[priority_area]:
        priority_title = recommendations[priority_area]['title']
        priority_content = recommendations[priority_area]['content']
    else:
        priority_content = recommendations[priority_area]
    
    # Mostrar la recomendación prioritaria con un estilo destacado
    with st.container():
        st.markdown(
            f"""

            <div style="border:2px solid {colors['priority']}; border-radius:5px; padding:15px; margin-bottom:20px;">

                <h4 style="color:{colors['priority']};">{priority_title}</h4>

                <p>{priority_content}</p>

            </div>

            """, 
            unsafe_allow_html=True
        )
    
    # Crear dos columnas para las tarjetas de recomendaciones restantes
    col1, col2 = st.columns(2)
    
    # Distribuir las recomendaciones en las columnas
    categories = ['vocabulary', 'structure', 'cohesion', 'clarity']
    for i, category in enumerate(categories):
        # Saltar si esta categoría ya es la prioritaria
        if category == priority_area:
            continue
            
        # Determinar título y contenido
        if isinstance(recommendations[category], dict) and 'title' in recommendations[category]:
            category_title = recommendations[category]['title']
            category_content = recommendations[category]['content']
        else:
            category_title = dimension_names.get(category, category)
            category_content = recommendations[category]
        
        # Alternar entre columnas
        with col1 if i % 2 == 0 else col2:
            # Crear tarjeta para cada recomendación
            st.markdown(
                f"""

                <div style="border:1px solid {colors[category]}; border-radius:5px; padding:10px; margin-bottom:15px;">

                    <h4 style="color:{colors[category]};">{icons[category]} {category_title}</h4>

                    <p>{category_content}</p>

                </div>

                """, 
                unsafe_allow_html=True
            )