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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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
"""
Component selection interface for HVAC Load Calculator.
This module provides the UI components for selecting building components.
"""

import streamlit as st
import pandas as pd
import numpy as np
from typing import Dict, List, Any, Optional, Tuple
import json
import os
import uuid

# Import data models
from data.building_components import Wall, Roof, Floor, Window, Door, Orientation, ComponentType
from utils.component_library import ComponentLibrary
from utils.u_value_calculator import UValueCalculator


class ComponentSelection:
    """Class for component selection interface."""
    
    def __init__(self):
        """Initialize component selection interface."""
        self.component_library = ComponentLibrary()
        self.u_value_calculator = UValueCalculator()
    
    def display(self) -> None:
        """Display component selection interface in Streamlit."""
        # Check for pending actions in session state and process them
        self._process_pending_actions(st.session_state)
        self.display_component_selection(st.session_state)
    
    def _process_pending_actions(self, session_state: Dict[str, Any]) -> None:
        """
        Process any pending actions stored in session state.
        
        Args:
            session_state: Streamlit session state
        """
        # Ensure components structure exists
        if "components" not in session_state:
            session_state["components"] = {
                'walls': [],
                'roofs': [],
                'floors': [],
                'windows': [],
                'doors': []
            }
            
        # Initialize pending actions if not exists
        if "pending_actions" not in session_state:
            session_state["pending_actions"] = {}
            
        # Ensure all required pending action keys exist
        required_keys = [
            "add_wall", "delete_wall",
            "add_roof", "delete_roof",
            "add_floor", "delete_floor",
            "add_window", "delete_window",
            "add_door", "delete_door"
        ]
        
        for key in required_keys:
            if key not in session_state["pending_actions"]:
                session_state["pending_actions"][key] = None
        
        # Process add wall action
        if session_state["pending_actions"].get("add_wall") is not None:
            new_wall = session_state["pending_actions"]["add_wall"]
            session_state["components"]["walls"].append(new_wall)
            session_state["pending_actions"]["add_wall"] = None
        
        # Process delete wall action
        if session_state["pending_actions"].get("delete_wall") is not None:
            wall_index = session_state["pending_actions"]["delete_wall"]
            if 0 <= wall_index < len(session_state["components"]["walls"]):
                session_state["components"]["walls"].pop(wall_index)
            session_state["pending_actions"]["delete_wall"] = None
        
        # Process add roof action
        if session_state["pending_actions"].get("add_roof") is not None:
            new_roof = session_state["pending_actions"]["add_roof"]
            session_state["components"]["roofs"].append(new_roof)
            session_state["pending_actions"]["add_roof"] = None
        
        # Process delete roof action
        if session_state["pending_actions"].get("delete_roof") is not None:
            roof_index = session_state["pending_actions"]["delete_roof"]
            if 0 <= roof_index < len(session_state["components"]["roofs"]):
                session_state["components"]["roofs"].pop(roof_index)
            session_state["pending_actions"]["delete_roof"] = None
        
        # Process add floor action
        if session_state["pending_actions"].get("add_floor") is not None:
            new_floor = session_state["pending_actions"]["add_floor"]
            session_state["components"]["floors"].append(new_floor)
            session_state["pending_actions"]["add_floor"] = None
        
        # Process delete floor action
        if session_state["pending_actions"].get("delete_floor") is not None:
            floor_index = session_state["pending_actions"]["delete_floor"]
            if 0 <= floor_index < len(session_state["components"]["floors"]):
                session_state["components"]["floors"].pop(floor_index)
            session_state["pending_actions"]["delete_floor"] = None
        
        # Process add window action
        if session_state["pending_actions"].get("add_window") is not None:
            new_window = session_state["pending_actions"]["add_window"]
            session_state["components"]["windows"].append(new_window)
            session_state["pending_actions"]["add_window"] = None
        
        # Process delete window action
        if session_state["pending_actions"].get("delete_window") is not None:
            window_index = session_state["pending_actions"]["delete_window"]
            if 0 <= window_index < len(session_state["components"]["windows"]):
                session_state["components"]["windows"].pop(window_index)
            session_state["pending_actions"]["delete_window"] = None
        
        # Process add door action
        if session_state["pending_actions"].get("add_door") is not None:
            new_door = session_state["pending_actions"]["add_door"]
            session_state["components"]["doors"].append(new_door)
            session_state["pending_actions"]["add_door"] = None
        
        # Process delete door action
        if session_state["pending_actions"].get("delete_door") is not None:
            door_index = session_state["pending_actions"]["delete_door"]
            if 0 <= door_index < len(session_state["components"]["doors"]):
                session_state["components"]["doors"].pop(door_index)
            session_state["pending_actions"]["delete_door"] = None
    
    def display_component_selection(self, session_state: Dict[str, Any]) -> None:
        """
        Display component selection interface in Streamlit.
        
        Args:
            session_state: Streamlit session state for storing form data
        """
        st.header("Building Components")
        
        # Initialize components in session state if not exists
        if "components" not in session_state:
            session_state["components"] = {
                "walls": [],
                "roofs": [],
                "floors": [],
                "windows": [],
                "doors": []
            }
            
        # Initialize form submission flags if not exists
        if "form_submitted" not in session_state:
            session_state["form_submitted"] = {
                "wall_form": False,
                "roof_form": False,
                "floor_form": False,
                "window_form": False,
                "door_form": False
            }
        
        # Create tabs for different component types
        tab1, tab2, tab3, tab4, tab5 = st.tabs([
            "Walls", 
            "Roofs", 
            "Floors", 
            "Windows",
            "Doors"
        ])
        
        with tab1:
            self._display_wall_selection(session_state)
        
        with tab2:
            self._display_roof_selection(session_state)
        
        with tab3:
            self._display_floor_selection(session_state)
        
        with tab4:
            self._display_window_selection(session_state)
        
        with tab5:
            self._display_door_selection(session_state)
    
    def _display_wall_selection(self, session_state: Dict[str, Any]) -> None:
        """
        Display wall selection interface.
        
        Args:
            session_state: Streamlit session state for storing form data
        """
        st.subheader("Walls")
        
        # Display existing walls
        if session_state["components"]["walls"]:
            st.write("Existing Walls:")
            
            # Create a table of existing walls
            wall_data = []
            for i, wall in enumerate(session_state["components"]["walls"]):
                wall_data.append({
                    "ID": i + 1,
                    "Name": wall.name,
                    "Type": wall.wall_type,
                    "U-Value (W/m²K)": f"{wall.u_value:.3f}",
                    "Area (m²)": f"{wall.area:.2f}",
                    "Orientation": wall.orientation.value
                })
            
            wall_df = pd.DataFrame(wall_data)
            st.dataframe(wall_df)
            
            # Add edit and delete buttons for each wall
            col1, col2 = st.columns(2)
            with col1:
                wall_to_edit = st.selectbox(
                    "Select wall to edit:",
                    options=range(1, len(session_state["components"]["walls"]) + 1),
                    format_func=lambda x: f"Wall #{x}: {session_state['components']['walls'][x-1].name}"
                )
            
            with col2:
                edit_col, delete_col = st.columns(2)
                with edit_col:
                    if st.button("Edit Wall", key="edit_wall"):
                        # Set the wall to edit in session state
                        session_state["wall_to_edit"] = wall_to_edit - 1
                
                with delete_col:
                    if st.button("Delete Wall", key="delete_wall"):
                        # Queue the delete action in pending_actions instead of doing it immediately
                        session_state["pending_actions"]["delete_wall"] = wall_to_edit - 1
        
        # Add new wall form
        st.write("Add New Wall:")
        
        # Check if we're editing an existing wall
        editing_wall = "wall_to_edit" in session_state
        wall_to_edit = session_state.get("wall_to_edit", None)
        
        # Get the wall to edit if we're editing
        wall = None
        if editing_wall and wall_to_edit is not None:
            wall = session_state["components"]["walls"][wall_to_edit]
        
        # Wall form
        with st.form(key="wall_form"):
            # Wall name
            wall_name = st.text_input(
                "Wall Name:",
                value=wall.name if wall else ""
            )
            
            # Wall type selection
            col1, col2 = st.columns(2)
            
            with col1:
                # Get preset wall types
                preset_walls = self.component_library.get_preset_components_by_type(ComponentType.WALL)
                preset_wall_names = [wall.name for wall in preset_walls]
                
                wall_type = st.selectbox(
                    "Wall Type:",
                    options=["Custom"] + preset_wall_names,
                    index=preset_wall_names.index(wall.wall_type) + 1 if wall and wall.wall_type in preset_wall_names else 0
                )
            
            # U-value
            with col2:
                if wall_type == "Custom":
                    u_value = st.number_input(
                        "U-Value (W/m²K):",
                        min_value=0.0,
                        max_value=10.0,
                        value=wall.u_value if wall else 0.5,
                        step=0.01
                    )
                else:
                    # Get U-value from preset wall
                    selected_wall = next((w for w in preset_walls if w.name == wall_type), None)
                    u_value = selected_wall.u_value if selected_wall else 0.5
                    st.write(f"U-Value: {u_value:.3f} W/m²K")
            
            # Wall dimensions
            col1, col2 = st.columns(2)
            
            with col1:
                wall_height = st.number_input(
                    "Wall Height (m):",
                    min_value=0.1,
                    max_value=50.0,
                    value=3.0 if not wall else (wall.area / 10.0),  # Assuming width of 10m if editing
                    step=0.1
                )
            
            with col2:
                wall_width = st.number_input(
                    "Wall Width (m):",
                    min_value=0.1,
                    max_value=100.0,
                    value=10.0 if not wall else (wall.area / wall_height),
                    step=0.1
                )
            
            # Calculate wall area
            wall_area = wall_height * wall_width
            st.write(f"Wall Area: {wall_area:.2f} m²")
            
            # Wall orientation
            orientation_options = [
                Orientation.NORTH,
                Orientation.NORTHEAST,
                Orientation.EAST,
                Orientation.SOUTHEAST,
                Orientation.SOUTH,
                Orientation.SOUTHWEST,
                Orientation.WEST,
                Orientation.NORTHWEST
            ]
            
            orientation = st.selectbox(
                "Orientation:",
                options=orientation_options,
                index=orientation_options.index(wall.orientation) if wall else 0,
                format_func=lambda x: x.value
            )
            
            # Wall color
            color_options = ["Light", "Medium", "Dark"]
            color = st.selectbox(
                "Color:",
                options=color_options,
                index=color_options.index(wall.color) if wall and wall.color in color_options else 1
            )
            
            # Submit button
            submit_label = "Update Wall" if editing_wall else "Add Wall"
            submit = st.form_submit_button(submit_label)
            
            if submit:
                # Create or update wall
                if wall_type == "Custom":
                    # Create custom wall
                    new_wall = Wall(
                        id=f"custom_wall_{str(uuid.uuid4())[:8]}",
                        name=wall_name,
                        component_type=ComponentType.WALL,
                        u_value=u_value,
                        area=wall_area,
                        orientation=orientation,
                        color=color,
                        wall_type="Custom",
                        wall_group="Custom"
                    )
                else:
                    # Clone preset wall and update properties
                    selected_wall = next((w for w in preset_walls if w.name == wall_type), None)
                    if selected_wall:
                        new_wall = Wall(
                            id=f"custom_wall_{str(uuid.uuid4())[:8]}",
                            name=wall_name,
                            component_type=ComponentType.WALL,
                            u_value=selected_wall.u_value,
                            area=wall_area,
                            orientation=orientation,
                            color=color,
                            wall_type=selected_wall.wall_type,
                            wall_group=selected_wall.wall_group,
                            material_layers=selected_wall.material_layers
                        )
                
                # Add or update wall in session state
                if editing_wall:
                    session_state["components"]["walls"][wall_to_edit] = new_wall
                    del session_state["wall_to_edit"]
                else:
                    # Queue the add action in pending_actions instead of doing it immediately
                    session_state["pending_actions"]["add_wall"] = new_wall
    
    def _display_roof_selection(self, session_state: Dict[str, Any]) -> None:
        """
        Display roof selection interface.
        
        Args:
            session_state: Streamlit session state for storing form data
        """
        st.subheader("Roofs")
        
        # Display existing roofs
        if session_state["components"]["roofs"]:
            st.write("Existing Roofs:")
            
            # Create a table of existing roofs
            roof_data = []
            for i, roof in enumerate(session_state["components"]["roofs"]):
                roof_data.append({
                    "ID": i + 1,
                    "Name": roof.name,
                    "Type": roof.roof_type,
                    "U-Value (W/m²K)": f"{roof.u_value:.3f}",
                    "Area (m²)": f"{roof.area:.2f}",
                    "Pitch (°)": f"{roof.pitch:.1f}"
                })
            
            roof_df = pd.DataFrame(roof_data)
            st.dataframe(roof_df)
            
            # Add edit and delete buttons for each roof
            col1, col2 = st.columns(2)
            with col1:
                roof_to_edit = st.selectbox(
                    "Select roof to edit:",
                    options=range(1, len(session_state["components"]["roofs"]) + 1),
                    format_func=lambda x: f"Roof #{x}: {session_state['components']['roofs'][x-1].name}"
                )
            
            with col2:
                edit_col, delete_col = st.columns(2)
                with edit_col:
                    if st.button("Edit Roof", key="edit_roof"):
                        # Set the roof to edit in session state
                        session_state["roof_to_edit"] = roof_to_edit - 1
                
                with delete_col:
                    if st.button("Delete Roof", key="delete_roof"):
                        # Queue the delete action in pending_actions instead of doing it immediately
                        session_state["pending_actions"]["delete_roof"] = roof_to_edit - 1
        
        # Add new roof form
        st.write("Add New Roof:")
        
        # Check if we're editing an existing roof
        editing_roof = "roof_to_edit" in session_state
        roof_to_edit = session_state.get("roof_to_edit", None)
        
        # Get the roof to edit if we're editing
        roof = None
        if editing_roof and roof_to_edit is not None:
            roof = session_state["components"]["roofs"][roof_to_edit]
        
        # Roof form
        with st.form(key="roof_form"):
            # Roof name
            roof_name = st.text_input(
                "Roof Name:",
                value=roof.name if roof else ""
            )
            
            # Roof type selection
            col1, col2 = st.columns(2)
            
            with col1:
                # Get preset roof types
                preset_roofs = self.component_library.get_preset_components_by_type(ComponentType.ROOF)
                preset_roof_names = [roof.name for roof in preset_roofs]
                
                roof_type = st.selectbox(
                    "Roof Type:",
                    options=["Custom"] + preset_roof_names,
                    index=preset_roof_names.index(roof.roof_type) + 1 if roof and roof.roof_type in preset_roof_names else 0
                )
            
            # U-value
            with col2:
                if roof_type == "Custom":
                    u_value = st.number_input(
                        "U-Value (W/m²K):",
                        min_value=0.0,
                        max_value=10.0,
                        value=roof.u_value if roof else 0.25,
                        step=0.01
                    )
                else:
                    # Get U-value from preset roof
                    selected_roof = next((r for r in preset_roofs if r.name == roof_type), None)
                    u_value = selected_roof.u_value if selected_roof else 0.25
                    st.write(f"U-Value: {u_value:.3f} W/m²K")
            
            # Roof dimensions
            col1, col2 = st.columns(2)
            
            with col1:
                roof_length = st.number_input(
                    "Roof Length (m):",
                    min_value=0.1,
                    max_value=100.0,
                    value=20.0 if not roof else (roof.area / 10.0),  # Assuming width of 10m if editing
                    step=0.1
                )
            
            with col2:
                roof_width = st.number_input(
                    "Roof Width (m):",
                    min_value=0.1,
                    max_value=100.0,
                    value=10.0 if not roof else (roof.area / roof_length),
                    step=0.1
                )
            
            # Calculate roof area
            roof_area = roof_length * roof_width
            st.write(f"Roof Area: {roof_area:.2f} m²")
            
            # Roof pitch
            roof_pitch = st.slider(
                "Roof Pitch (°):",
                min_value=0.0,
                max_value=60.0,
                value=roof.pitch if roof else 0.0,
                step=1.0
            )
            
            # Suspended ceiling
            has_suspended_ceiling = st.checkbox(
                "Has Suspended Ceiling",
                value=roof.has_suspended_ceiling if roof else False
            )
            
            ceiling_plenum_height = 0.0
            if has_suspended_ceiling:
                ceiling_plenum_height = st.number_input(
                    "Ceiling Plenum Height (m):",
                    min_value=0.1,
                    max_value=5.0,
                    value=roof.ceiling_plenum_height if roof and roof.has_suspended_ceiling else 0.5,
                    step=0.1
                )
            
            # Submit button
            submit_label = "Update Roof" if editing_roof else "Add Roof"
            submit = st.form_submit_button(submit_label)
            
            if submit and not session_state["form_submitted"]["roof_form"]:
                # Set the form submission flag to prevent infinite loops
                session_state["form_submitted"]["roof_form"] = True

                # Create or update roof
                if roof_type == "Custom":
                    # Create custom roof
                    new_roof = Roof(
                        id=f"custom_roof_{str(uuid.uuid4())[:8]}",
                        name=roof_name,
                        component_type=ComponentType.ROOF,
                        u_value=u_value,
                        area=roof_area,
                        orientation=Orientation.HORIZONTAL,
                        roof_type="Custom",
                        roof_group="Custom",
                        pitch=roof_pitch,
                        has_suspended_ceiling=has_suspended_ceiling,
                        ceiling_plenum_height=ceiling_plenum_height
                    )
                else:
                    # Clone preset roof and update properties
                    selected_roof = next((r for r in preset_roofs if r.name == roof_type), None)
                    if selected_roof:
                        new_roof = Roof(
                            id=f"custom_roof_{str(uuid.uuid4())[:8]}",
                            name=roof_name,
                            component_type=ComponentType.ROOF,
                            u_value=selected_roof.u_value,
                            area=roof_area,
                            orientation=Orientation.HORIZONTAL,
                            roof_type=selected_roof.roof_type,
                            roof_group=selected_roof.roof_group,
                            pitch=roof_pitch,
                            has_suspended_ceiling=has_suspended_ceiling,
                            ceiling_plenum_height=ceiling_plenum_height,
                            material_layers=selected_roof.material_layers
                        )
                
                # Add or update roof in session state
                if editing_roof:
                    session_state["components"]["roofs"][roof_to_edit] = new_roof
                    del session_state["roof_to_edit"]
                else:
                    session_state["components"]["roofs"].append(new_roof)
                
                st.experimental_rerun()
    
    def _display_floor_selection(self, session_state: Dict[str, Any]) -> None:
        """
        Display floor selection interface.
        
        Args:
            session_state: Streamlit session state for storing form data
        """
        st.subheader("Floors")
        
        # Display existing floors
        if session_state["components"]["floors"]:
            st.write("Existing Floors:")
            
            # Create a table of existing floors
            floor_data = []
            for i, floor in enumerate(session_state["components"]["floors"]):
                floor_data.append({
                    "ID": i + 1,
                    "Name": floor.name,
                    "Type": floor.floor_type,
                    "U-Value (W/m²K)": f"{floor.u_value:.3f}",
                    "Area (m²)": f"{floor.area:.2f}",
                    "Ground Contact": "Yes" if floor.is_ground_contact else "No"
                })
            
            floor_df = pd.DataFrame(floor_data)
            st.dataframe(floor_df)
            
            # Add edit and delete buttons for each floor
            col1, col2 = st.columns(2)
            with col1:
                floor_to_edit = st.selectbox(
                    "Select floor to edit:",
                    options=range(1, len(session_state["components"]["floors"]) + 1),
                    format_func=lambda x: f"Floor #{x}: {session_state['components']['floors'][x-1].name}"
                )
            
            with col2:
                edit_col, delete_col = st.columns(2)
                with edit_col:
                    if st.button("Edit Floor", key="edit_floor"):
                        # Set the floor to edit in session state
                        session_state["floor_to_edit"] = floor_to_edit - 1
                
                with delete_col:
                    if st.button("Delete Floor", key="delete_floor"):
                        # Queue the delete action in pending_actions instead of doing it immediately
                        session_state["pending_actions"]["delete_floor"] = floor_to_edit - 1
        
        # Add new floor form
        st.write("Add New Floor:")
        
        # Check if we're editing an existing floor
        editing_floor = "floor_to_edit" in session_state
        floor_to_edit = session_state.get("floor_to_edit", None)
        
        # Get the floor to edit if we're editing
        floor = None
        if editing_floor and floor_to_edit is not None:
            floor = session_state["components"]["floors"][floor_to_edit]
        
        # Floor form
        with st.form(key="floor_form"):
            # Floor name
            floor_name = st.text_input(
                "Floor Name:",
                value=floor.name if floor else ""
            )
            
            # Floor type selection
            col1, col2 = st.columns(2)
            
            with col1:
                # Get preset floor types
                preset_floors = self.component_library.get_preset_components_by_type(ComponentType.FLOOR)
                preset_floor_names = [floor.name for floor in preset_floors]
                
                floor_type = st.selectbox(
                    "Floor Type:",
                    options=["Custom"] + preset_floor_names,
                    index=preset_floor_names.index(floor.floor_type) + 1 if floor and floor.floor_type in preset_floor_names else 0
                )
            
            # U-value
            with col2:
                if floor_type == "Custom":
                    u_value = st.number_input(
                        "U-Value (W/m²K):",
                        min_value=0.0,
                        max_value=10.0,
                        value=floor.u_value if floor else 0.25,
                        step=0.01
                    )
                else:
                    # Get U-value from preset floor
                    selected_floor = next((f for f in preset_floors if f.name == floor_type), None)
                    u_value = selected_floor.u_value if selected_floor else 0.25
                    st.write(f"U-Value: {u_value:.3f} W/m²K")
            
            # Floor dimensions
            col1, col2 = st.columns(2)
            
            with col1:
                floor_length = st.number_input(
                    "Floor Length (m):",
                    min_value=0.1,
                    max_value=100.0,
                    value=20.0 if not floor else (floor.area / 10.0),  # Assuming width of 10m if editing
                    step=0.1
                )
            
            with col2:
                floor_width = st.number_input(
                    "Floor Width (m):",
                    min_value=0.1,
                    max_value=100.0,
                    value=10.0 if not floor else (floor.area / floor_length),
                    step=0.1
                )
            
            # Calculate floor area
            floor_area = floor_length * floor_width
            st.write(f"Floor Area: {floor_area:.2f} m²")
            
            # Ground contact
            is_ground_contact = st.checkbox(
                "Is in Contact with Ground",
                value=floor.is_ground_contact if floor else False
            )
            
            perimeter_length = 0.0
            if is_ground_contact:
                perimeter_length = st.number_input(
                    "Perimeter Length (m):",
                    min_value=0.1,
                    max_value=1000.0,
                    value=floor.perimeter_length if floor and floor.is_ground_contact else 2 * (floor_length + floor_width),
                    step=0.1
                )
            
            # Submit button
            submit_label = "Update Floor" if editing_floor else "Add Floor"
            submit = st.form_submit_button(submit_label)
            
            if submit and not session_state["form_submitted"]["floor_form"]:
                # Set the form submission flag to prevent infinite loops
                session_state["form_submitted"]["floor_form"] = True

                # Create or update floor
                if floor_type == "Custom":
                    # Create custom floor
                    new_floor = Floor(
                        id=f"custom_floor_{str(uuid.uuid4())[:8]}",
                        name=floor_name,
                        component_type=ComponentType.FLOOR,
                        u_value=u_value,
                        area=floor_area,
                        orientation=Orientation.HORIZONTAL,
                        floor_type="Custom",
                        is_ground_contact=is_ground_contact,
                        perimeter_length=perimeter_length
                    )
                else:
                    # Clone preset floor and update properties
                    selected_floor = next((f for f in preset_floors if f.name == floor_type), None)
                    if selected_floor:
                        new_floor = Floor(
                            id=f"custom_floor_{str(uuid.uuid4())[:8]}",
                            name=floor_name,
                            component_type=ComponentType.FLOOR,
                            u_value=selected_floor.u_value,
                            area=floor_area,
                            orientation=Orientation.HORIZONTAL,
                            floor_type=selected_floor.floor_type,
                            is_ground_contact=is_ground_contact,
                            perimeter_length=perimeter_length,
                            material_layers=selected_floor.material_layers
                        )
                
                # Add or update floor in session state
                if editing_floor:
                    session_state["components"]["floors"][floor_to_edit] = new_floor
                    del session_state["floor_to_edit"]
                else:
                    # Queue the add action in pending_actions instead of doing it immediately
                    session_state["pending_actions"]["add_floor"] = new_floor
    
    def _display_window_selection(self, session_state: Dict[str, Any]) -> None:
        """
        Display window selection interface.
        
        Args:
            session_state: Streamlit session state for storing form data
        """
        st.subheader("Windows")
        
        # Display existing windows
        if session_state["components"]["windows"]:
            st.write("Existing Windows:")
            
            # Create a table of existing windows
            window_data = []
            for i, window in enumerate(session_state["components"]["windows"]):
                window_data.append({
                    "ID": i + 1,
                    "Name": window.name,
                    "Type": window.window_type,
                    "U-Value (W/m²K)": f"{window.u_value:.3f}",
                    "SHGC": f"{window.shgc:.2f}",
                    "Area (m²)": f"{window.area:.2f}",
                    "Orientation": window.orientation.value
                })
            
            window_df = pd.DataFrame(window_data)
            st.dataframe(window_df)
            
            # Add edit and delete buttons for each window
            col1, col2 = st.columns(2)
            with col1:
                window_to_edit = st.selectbox(
                    "Select window to edit:",
                    options=range(1, len(session_state["components"]["windows"]) + 1),
                    format_func=lambda x: f"Window #{x}: {session_state['components']['windows'][x-1].name}"
                )
            
            with col2:
                edit_col, delete_col = st.columns(2)
                with edit_col:
                    if st.button("Edit Window", key="edit_window"):
                        # Set the window to edit in session state
                        session_state["window_to_edit"] = window_to_edit - 1
                
                with delete_col:
                    if st.button("Delete Window", key="delete_window"):
                        # Queue the delete action in pending_actions instead of doing it immediately
                        session_state["pending_actions"]["delete_window"] = window_to_edit - 1
        
        # Add new window form
        st.write("Add New Window:")
        
        # Check if we're editing an existing window
        editing_window = "window_to_edit" in session_state
        window_to_edit = session_state.get("window_to_edit", None)
        
        # Get the window to edit if we're editing
        window = None
        if editing_window and window_to_edit is not None:
            window = session_state["components"]["windows"][window_to_edit]
        
        # Window form
        with st.form(key="window_form"):
            # Window name
            window_name = st.text_input(
                "Window Name:",
                value=window.name if window else ""
            )
            
            # Window type selection
            col1, col2 = st.columns(2)
            
            with col1:
                # Get preset window types
                preset_windows = self.component_library.get_preset_components_by_type(ComponentType.WINDOW)
                preset_window_names = [window.name for window in preset_windows]
                
                window_type = st.selectbox(
                    "Window Type:",
                    options=["Custom"] + preset_window_names,
                    index=preset_window_names.index(window.window_type) + 1 if window and window.window_type in preset_window_names else 0
                )
            
            # U-value and SHGC
            with col2:
                if window_type == "Custom":
                    u_value = st.number_input(
                        "U-Value (W/m²K):",
                        min_value=0.0,
                        max_value=10.0,
                        value=window.u_value if window else 1.5,
                        step=0.01
                    )
                    
                    shgc = st.number_input(
                        "Solar Heat Gain Coefficient (SHGC):",
                        min_value=0.0,
                        max_value=1.0,
                        value=window.shgc if window else 0.7,
                        step=0.01
                    )
                    
                    vt = st.number_input(
                        "Visible Transmittance (VT):",
                        min_value=0.0,
                        max_value=1.0,
                        value=window.vt if window else 0.7,
                        step=0.01
                    )
                else:
                    # Get properties from preset window
                    selected_window = next((w for w in preset_windows if w.name == window_type), None)
                    if selected_window:
                        u_value = selected_window.u_value
                        shgc = selected_window.shgc
                        vt = selected_window.vt
                        st.write(f"U-Value: {u_value:.3f} W/m²K")
                        st.write(f"SHGC: {shgc:.2f}")
                        st.write(f"VT: {vt:.2f}")
            
            # Window dimensions
            col1, col2 = st.columns(2)
            
            with col1:
                window_height = st.number_input(
                    "Window Height (m):",
                    min_value=0.1,
                    max_value=10.0,
                    value=1.5 if not window else (window.area / 2.0),  # Assuming width of 2m if editing
                    step=0.1
                )
            
            with col2:
                window_width = st.number_input(
                    "Window Width (m):",
                    min_value=0.1,
                    max_value=10.0,
                    value=2.0 if not window else (window.area / window_height),
                    step=0.1
                )
            
            # Calculate window area
            window_area = window_height * window_width
            st.write(f"Window Area: {window_area:.2f} m²")
            
            # Window orientation
            orientation_options = [
                Orientation.NORTH,
                Orientation.NORTHEAST,
                Orientation.EAST,
                Orientation.SOUTHEAST,
                Orientation.SOUTH,
                Orientation.SOUTHWEST,
                Orientation.WEST,
                Orientation.NORTHWEST
            ]
            
            orientation = st.selectbox(
                "Orientation:",
                options=orientation_options,
                index=orientation_options.index(window.orientation) if window and window.orientation in orientation_options else 0,
                format_func=lambda x: x.value
            )
            
            # Shading
            has_shading = st.checkbox(
                "Has Shading",
                value=window.has_shading if window else False
            )
            
            shading_type = None
            shading_coefficient = 1.0
            
            if has_shading:
                shading_options = ["Internal", "External", "Between-glass"]
                shading_type = st.selectbox(
                    "Shading Type:",
                    options=shading_options,
                    index=shading_options.index(window.shading_type) if window and window.shading_type in shading_options else 0
                )
                
                shading_coefficient = st.slider(
                    "Shading Coefficient:",
                    min_value=0.1,
                    max_value=1.0,
                    value=window.shading_coefficient if window and window.has_shading else 0.7,
                    step=0.1
                )
            
            # Wall assignment
            if session_state["components"]["walls"]:
                wall_options = [wall.name for wall in session_state["components"]["walls"]]
                assigned_wall = st.selectbox(
                    "Assign to Wall:",
                    options=["None"] + wall_options,
                    index=0
                )
            else:
                assigned_wall = "None"
                st.warning("No walls available for assignment. Please add walls first.")
            
            # Submit button
            submit_label = "Update Window" if editing_window else "Add Window"
            submit = st.form_submit_button(submit_label)
            
            if submit and not session_state["form_submitted"]["window_form"]:
                # Set the form submission flag to prevent infinite loops
                session_state["form_submitted"]["window_form"] = True

                # Create or update window
                if window_type == "Custom":
                    # Create custom window
                    new_window = Window(
                        id=f"custom_window_{str(uuid.uuid4())[:8]}",
                        name=window_name,
                        component_type=ComponentType.WINDOW,
                        u_value=u_value,
                        area=window_area,
                        orientation=orientation,
                        shgc=shgc,
                        vt=vt,
                        window_type="Custom",
                        glazing_layers=2,
                        gas_fill="Air",
                        low_e_coating=False,
                        has_shading=has_shading,
                        shading_type=shading_type,
                        shading_coefficient=shading_coefficient
                    )
                else:
                    # Clone preset window and update properties
                    selected_window = next((w for w in preset_windows if w.name == window_type), None)
                    if selected_window:
                        new_window = Window(
                            id=f"custom_window_{str(uuid.uuid4())[:8]}",
                            name=window_name,
                            component_type=ComponentType.WINDOW,
                            u_value=selected_window.u_value,
                            area=window_area,
                            orientation=orientation,
                            shgc=selected_window.shgc,
                            vt=selected_window.vt,
                            window_type=selected_window.window_type,
                            glazing_layers=selected_window.glazing_layers,
                            gas_fill=selected_window.gas_fill,
                            low_e_coating=selected_window.low_e_coating,
                            has_shading=has_shading,
                            shading_type=shading_type,
                            shading_coefficient=shading_coefficient
                        )
                
                # Add or update window in session state
                if editing_window:
                    session_state["components"]["windows"][window_to_edit] = new_window
                    del session_state["window_to_edit"]
                else:
                    # Queue the add action in pending_actions instead of doing it immediately
                    session_state["pending_actions"]["add_window"] = new_window
                
                # Assign window to wall if selected
                if assigned_wall != "None":
                    # Find the wall by name
                    for wall in session_state["components"]["walls"]:
                        if wall.name == assigned_wall:
                            # Add window ID to wall's windows list
                            if new_window.id not in wall.windows:
                                wall.windows.append(new_window.id)
                            
                            # Update wall's net area
                            if wall.gross_area is None:
                                wall.gross_area = wall.area
                            
                            wall.net_area = wall.gross_area - sum(
                                w.area for w in session_state["components"]["windows"] 
                                if w.id in wall.windows
                            ) - sum(
                                d.area for d in session_state["components"]["doors"] 
                                if d.id in wall.doors
                            )
                            
                            wall.area = wall.net_area
                            break
    
    def _display_door_selection(self, session_state: Dict[str, Any]) -> None:
        """
        Display door selection interface.
        
        Args:
            session_state: Streamlit session state for storing form data
        """
        st.subheader("Doors")
        
        # Display existing doors
        if session_state["components"]["doors"]:
            st.write("Existing Doors:")
            
            # Create a table of existing doors
            door_data = []
            for i, door in enumerate(session_state["components"]["doors"]):
                door_data.append({
                    "ID": i + 1,
                    "Name": door.name,
                    "Type": door.door_type,
                    "U-Value (W/m²K)": f"{door.u_value:.3f}",
                    "Area (m²)": f"{door.area:.2f}",
                    "Glazing %": f"{door.glazing_percentage:.0f}%",
                    "Orientation": door.orientation.value
                })
            
            door_df = pd.DataFrame(door_data)
            st.dataframe(door_df)
            
            # Add edit and delete buttons for each door
            col1, col2 = st.columns(2)
            with col1:
                door_to_edit = st.selectbox(
                    "Select door to edit:",
                    options=range(1, len(session_state["components"]["doors"]) + 1),
                    format_func=lambda x: f"Door #{x}: {session_state['components']['doors'][x-1].name}"
                )
            
            with col2:
                edit_col, delete_col = st.columns(2)
                with edit_col:
                    if st.button("Edit Door", key="edit_door"):
                        # Set the door to edit in session state
                        session_state["door_to_edit"] = door_to_edit - 1
                
                with delete_col:
                    if st.button("Delete Door", key="delete_door"):
                        # Queue the delete action in pending_actions instead of doing it immediately
                        session_state["pending_actions"]["delete_door"] = door_to_edit - 1
        
        # Add new door form
        st.write("Add New Door:")
        
        # Check if we're editing an existing door
        editing_door = "door_to_edit" in session_state
        door_to_edit = session_state.get("door_to_edit", None)
        
        # Get the door to edit if we're editing
        door = None
        if editing_door and door_to_edit is not None:
            door = session_state["components"]["doors"][door_to_edit]
        
        # Door form
        with st.form(key="door_form"):
            # Door name
            door_name = st.text_input(
                "Door Name:",
                value=door.name if door else ""
            )
            
            # Door type selection
            col1, col2 = st.columns(2)
            
            with col1:
                # Get preset door types
                preset_doors = self.component_library.get_preset_components_by_type(ComponentType.DOOR)
                preset_door_names = [door.name for door in preset_doors]
                
                door_type = st.selectbox(
                    "Door Type:",
                    options=["Custom"] + preset_door_names,
                    index=preset_door_names.index(door.door_type) + 1 if door and door.door_type in preset_door_names else 0
                )
            
            # U-value and glazing percentage
            with col2:
                if door_type == "Custom":
                    u_value = st.number_input(
                        "U-Value (W/m²K):",
                        min_value=0.0,
                        max_value=10.0,
                        value=door.u_value if door else 2.0,
                        step=0.01
                    )
                    
                    glazing_percentage = st.slider(
                        "Glazing Percentage (%):",
                        min_value=0,
                        max_value=100,
                        value=int(door.glazing_percentage) if door else 0,
                        step=5
                    )
                    
                    # Only show SHGC and VT if door has glazing
                    if glazing_percentage > 0:
                        shgc = st.number_input(
                            "Solar Heat Gain Coefficient (SHGC):",
                            min_value=0.0,
                            max_value=1.0,
                            value=door.shgc if door and door.shgc else 0.7,
                            step=0.01
                        )
                        
                        vt = st.number_input(
                            "Visible Transmittance (VT):",
                            min_value=0.0,
                            max_value=1.0,
                            value=door.vt if door and door.vt else 0.7,
                            step=0.01
                        )
                    else:
                        shgc = 0.0
                        vt = 0.0
                else:
                    # Get properties from preset door
                    selected_door = next((d for d in preset_doors if d.name == door_type), None)
                    if selected_door:
                        u_value = selected_door.u_value
                        glazing_percentage = selected_door.glazing_percentage
                        shgc = selected_door.shgc
                        vt = selected_door.vt
                        st.write(f"U-Value: {u_value:.3f} W/m²K")
                        st.write(f"Glazing: {glazing_percentage:.0f}%")
                        if glazing_percentage > 0:
                            st.write(f"SHGC: {shgc:.2f}")
                            st.write(f"VT: {vt:.2f}")
            
            # Door dimensions
            col1, col2 = st.columns(2)
            
            with col1:
                door_height = st.number_input(
                    "Door Height (m):",
                    min_value=0.1,
                    max_value=5.0,
                    value=2.1 if not door else (door.area / 0.9),  # Assuming width of 0.9m if editing
                    step=0.1
                )
            
            with col2:
                door_width = st.number_input(
                    "Door Width (m):",
                    min_value=0.1,
                    max_value=5.0,
                    value=0.9 if not door else (door.area / door_height),
                    step=0.1
                )
            
            # Calculate door area
            door_area = door_height * door_width
            st.write(f"Door Area: {door_area:.2f} m²")
            
            # Door orientation
            orientation_options = [
                Orientation.NORTH,
                Orientation.NORTHEAST,
                Orientation.EAST,
                Orientation.SOUTHEAST,
                Orientation.SOUTH,
                Orientation.SOUTHWEST,
                Orientation.WEST,
                Orientation.NORTHWEST
            ]
            
            orientation = st.selectbox(
                "Orientation:",
                options=orientation_options,
                index=orientation_options.index(door.orientation) if door and door.orientation in orientation_options else 0,
                format_func=lambda x: x.value
            )
            
            # Wall assignment
            if session_state["components"]["walls"]:
                wall_options = [wall.name for wall in session_state["components"]["walls"]]
                assigned_wall = st.selectbox(
                    "Assign to Wall:",
                    options=["None"] + wall_options,
                    index=0
                )
            else:
                assigned_wall = "None"
                st.warning("No walls available for assignment. Please add walls first.")
            
            # Submit button
            submit_label = "Update Door" if editing_door else "Add Door"
            submit = st.form_submit_button(submit_label)
            
            if submit and not session_state["form_submitted"]["door_form"]:
                # Set the form submission flag to prevent infinite loops
                session_state["form_submitted"]["door_form"] = True

                # Create or update door
                if door_type == "Custom":
                    # Create custom door
                    new_door = Door(
                        id=f"custom_door_{str(uuid.uuid4())[:8]}",
                        name=door_name,
                        component_type=ComponentType.DOOR,
                        u_value=u_value,
                        area=door_area,
                        orientation=orientation,
                        door_type="Custom",
                        glazing_percentage=glazing_percentage,
                        shgc=shgc if glazing_percentage > 0 else 0.0,
                        vt=vt if glazing_percentage > 0 else 0.0
                    )
                else:
                    # Clone preset door and update properties
                    selected_door = next((d for d in preset_doors if d.name == door_type), None)
                    if selected_door:
                        new_door = Door(
                            id=f"custom_door_{str(uuid.uuid4())[:8]}",
                            name=door_name,
                            component_type=ComponentType.DOOR,
                            u_value=selected_door.u_value,
                            area=door_area,
                            orientation=orientation,
                            door_type=selected_door.door_type,
                            glazing_percentage=selected_door.glazing_percentage,
                            shgc=selected_door.shgc,
                            vt=selected_door.vt
                        )
                
                # Add or update door in session state
                if editing_door:
                    session_state["components"]["doors"][door_to_edit] = new_door
                    del session_state["door_to_edit"]
                else:
                    # Queue the add action in pending_actions instead of doing it immediately
                    session_state["pending_actions"]["add_door"] = new_door
                
                # Assign door to wall if selected
                if assigned_wall != "None":
                    # Find the wall by name
                    for wall in session_state["components"]["walls"]:
                        if wall.name == assigned_wall:
                            # Add door ID to wall's doors list
                            if new_door.id not in wall.doors:
                                wall.doors.append(new_door.id)
                            
                            # Update wall's net area
                            if wall.gross_area is None:
                                wall.gross_area = wall.area
                            
                            wall.net_area = wall.gross_area - sum(
                                w.area for w in session_state["components"]["windows"] 
                                if w.id in wall.windows
                            ) - sum(
                                d.area for d in session_state["components"]["doors"] 
                                if d.id in wall.doors
                            )
                            
                            wall.area = wall.net_area
                            break