File size: 8,050 Bytes
3b08f11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
"""Tab 1: Room Assessment.

Single room input with dimensions and assessment context.
MVP Simplification: No multi-room support.
"""

import gradio as gr
from typing import Any

from ui.state import SessionState
from ui.constants import CEILING_HEIGHT_PRESETS


# Facility classification options
FACILITY_OPTIONS = [
    ("Operational", "operational"),
    ("Non-Operational", "non-operational"),
    ("Public/Childcare", "public-childcare"),
]

# Construction era options
CONSTRUCTION_ERA_OPTIONS = [
    ("Pre-1980 (potential LBP/ACM)", "pre-1980"),
    ("1980-2000", "1980-2000"),
    ("Post-2000", "post-2000"),
]


def create_tab() -> dict[str, Any]:
    """Create Tab 1 UI components.

    Returns:
        Dictionary of component references for event wiring.
    """
    gr.Markdown("### Room Assessment")
    gr.Markdown("*Enter room details and assessment context.*")

    # Room identification
    room_name = gr.Textbox(
        label="Room/Area Name *",
        placeholder="e.g., Warehouse Bay A, Office 101",
        elem_id="room_name",
    )

    # Dimensions
    gr.Markdown("#### Dimensions")
    with gr.Row():
        room_length = gr.Number(
            label="Length (ft) *",
            minimum=1,
            value=None,
            elem_id="room_length",
        )
        room_width = gr.Number(
            label="Width (ft) *",
            minimum=1,
            value=None,
            elem_id="room_width",
        )

    with gr.Row():
        room_height_preset = gr.Dropdown(
            label="Ceiling Height *",
            choices=CEILING_HEIGHT_PRESETS,
            elem_id="room_height_preset",
            info="Select preset or choose Custom",
        )
        room_height_custom = gr.Number(
            label="Custom Height (ft)",
            minimum=1,
            value=None,
            visible=False,
            elem_id="room_height_custom",
        )

    # Calculated displays
    with gr.Row():
        floor_area = gr.Textbox(
            label="Floor Area (SF)",
            value="0",
            interactive=False,
        )
        room_volume = gr.Textbox(
            label="Volume (CF)",
            value="0",
            interactive=False,
        )

    # Assessment context (moved from Project tab)
    gr.Markdown("#### Assessment Context")

    facility_classification = gr.Radio(
        label="Facility Classification *",
        choices=FACILITY_OPTIONS,
        value="non-operational",
        elem_id="facility_classification",
        info="Affects clearance thresholds",
    )

    construction_era = gr.Radio(
        label="Construction Era *",
        choices=CONSTRUCTION_ERA_OPTIONS,
        value="post-2000",
        elem_id="construction_era",
        info="Pre-1980 triggers LBP/ACM flags",
    )

    # Validation status
    validation_status = gr.HTML(
        value="",
        elem_id="tab1_validation",
    )

    validate_btn = gr.Button(
        "Continue to Images →",
        variant="primary",
    )

    return {
        "room_name": room_name,
        "room_length": room_length,
        "room_width": room_width,
        "room_height_preset": room_height_preset,
        "room_height_custom": room_height_custom,
        "floor_area": floor_area,
        "room_volume": room_volume,
        "facility_classification": facility_classification,
        "construction_era": construction_era,
        "validation_status": validation_status,
        "validate_btn": validate_btn,
    }


def on_height_preset_change(preset_value: int | None) -> dict:
    """Show/hide custom height input based on preset selection.

    Args:
        preset_value: The selected preset value, or None for "Custom".

    Returns:
        Gradio update dict for custom height visibility.
    """
    return gr.update(visible=(preset_value is None))


def update_calculated_values(
    length: float | None,
    width: float | None,
    height_preset: int | None,
    height_custom: float | None,
) -> tuple[str, str]:
    """Calculate and return floor area and volume.

    Returns:
        Tuple of (floor_area_str, volume_str).
    """
    # Get effective values
    length_val = float(length) if length and length > 0 else 0
    width_val = float(width) if width and width > 0 else 0

    if height_preset is not None:
        height_val = float(height_preset)
    elif height_custom is not None and height_custom > 0:
        height_val = float(height_custom)
    else:
        height_val = 0

    # Calculate
    area = length_val * width_val
    volume = area * height_val

    return f"{area:,.0f}", f"{volume:,.0f}"


def save_room_to_session(
    session: SessionState,
    name: str,
    length: float | None,
    width: float | None,
    height_preset: int | None,
    height_custom: float | None,
    facility_classification: str,
    construction_era: str,
) -> SessionState:
    """Save room data to session (called on field changes).

    Returns:
        Updated session.
    """
    # Determine actual ceiling height
    if height_preset is not None:
        height = float(height_preset)
    elif height_custom is not None and height_custom > 0:
        height = float(height_custom)
    else:
        height = 0

    # Update session room data
    session.room.name = name.strip() if name else ""
    session.room.length_ft = float(length) if length and length > 0 else 0
    session.room.width_ft = float(width) if width and width > 0 else 0
    session.room.ceiling_height_ft = height
    session.room.facility_classification = facility_classification
    session.room.construction_era = construction_era
    session.update_timestamp()

    return session


def validate_and_continue(session: SessionState) -> tuple[SessionState, str, int]:
    """Validate Tab 1 and proceed to Tab 2.

    Returns:
        Tuple of (session, validation_html, next_tab_index).
    """
    is_valid, errors = session.validate_tab1()

    if is_valid:
        session.tab1_complete = True
        session.update_timestamp()
        html = """
        <div style="background: #e8f5e9; border: 1px solid #66bb6a; border-radius: 4px; padding: 10px;">
            <span style="color: #2e7d32;">✓ Room details complete. Proceeding to Images tab...</span>
        </div>
        """
        return session, html, gr.update(selected=1)  # Go to tab index 1 (Images)
    else:
        session.tab1_complete = False
        error_items = "".join(f"<li>{e}</li>" for e in errors)
        html = f"""
        <div style="background: #ffebee; border: 1px solid #ef5350; border-radius: 4px; padding: 10px;">
            <strong style="color: #c62828;">Please fix the following:</strong>
            <ul style="margin: 5px 0 0 0; padding-left: 20px; color: #c62828;">
                {error_items}
            </ul>
        </div>
        """
        return session, html, gr.update(selected=0)  # Stay on current tab


def load_from_session(session: SessionState) -> tuple[str, float | None, float | None, int | None, float | None, str, str, str, str]:
    """Load room data from session.

    Returns:
        Tuple of (name, length, width, height_preset, height_custom,
                  floor_area, volume, facility_classification, construction_era).
    """
    r = session.room

    # Determine if height matches a preset or is custom
    height_preset = None
    height_custom = None

    # Check if ceiling height matches a preset value
    preset_values = [p[1] for p in CEILING_HEIGHT_PRESETS if p[1] is not None]
    if r.ceiling_height_ft in preset_values:
        height_preset = int(r.ceiling_height_ft)
    elif r.ceiling_height_ft > 0:
        height_custom = r.ceiling_height_ft

    # Calculate stats
    area = r.length_ft * r.width_ft
    volume = area * r.ceiling_height_ft

    return (
        r.name,
        r.length_ft if r.length_ft > 0 else None,
        r.width_ft if r.width_ft > 0 else None,
        height_preset,
        height_custom,
        f"{area:,.0f}",
        f"{volume:,.0f}",
        r.facility_classification,
        r.construction_era,
    )