File size: 57,081 Bytes
5acd9c3
e5ec20b
 
 
a3d40b4
5acd9c3
 
6c71972
 
a3d40b4
 
5acd9c3
 
 
 
 
3cc7c13
7dadc22
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7dadc22
 
 
ef8ee9e
 
 
 
 
 
 
f26b729
 
 
 
7dadc22
f26b729
 
 
 
ef8ee9e
7dadc22
ef8ee9e
 
 
 
 
 
 
 
 
 
 
 
 
 
523275c
 
ef8ee9e
 
 
 
 
 
 
 
 
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d3d781
 
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7dadc22
69af9b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5acd9c3
6c71972
2e813e6
 
 
 
 
 
6c71972
 
 
 
 
 
 
 
 
 
 
 
 
5acd9c3
2e813e6
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7dadc22
6c71972
 
 
 
 
 
 
 
 
 
 
5acd9c3
 
 
6c71972
5acd9c3
 
 
6c71972
 
 
 
 
 
 
 
 
 
 
 
 
a425931
6c71972
 
 
 
 
 
 
 
 
 
 
a3d40b4
 
6c71972
 
 
 
 
 
 
 
 
 
69af9b5
6c71972
69af9b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c71972
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69af9b5
6c71972
69af9b5
6c71972
 
69af9b5
6c71972
 
69af9b5
 
 
 
6c71972
 
 
 
 
 
 
 
 
a425931
69af9b5
6c71972
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69af9b5
 
 
6c71972
 
 
 
 
 
 
 
69af9b5
 
 
 
 
 
 
 
 
 
 
 
 
6c71972
 
 
 
 
 
 
 
 
 
69af9b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31fe0d9
 
 
 
69af9b5
 
 
 
5acd9c3
 
 
 
 
6c71972
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5acd9c3
 
2e813e6
a425931
5acd9c3
 
2e813e6
 
a425931
2e813e6
 
 
 
 
a425931
2e813e6
 
 
 
 
 
 
 
 
 
 
5acd9c3
 
 
a425931
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f26b729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef8ee9e
e5ec20b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5acd9c3
e5ec20b
 
 
 
 
 
 
 
 
5acd9c3
 
 
 
 
 
 
 
2e813e6
5acd9c3
 
 
 
 
 
 
 
 
2e813e6
5acd9c3
 
 
 
 
 
2e813e6
5acd9c3
 
 
2e813e6
5acd9c3
 
 
 
2e813e6
5acd9c3
 
 
6c71972
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5acd9c3
6c71972
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c71972
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5acd9c3
 
 
 
 
2e813e6
5acd9c3
 
 
2e813e6
5acd9c3
 
 
 
 
a425931
5acd9c3
 
 
 
 
 
 
 
6c71972
 
 
 
 
a425931
5acd9c3
 
6c71972
 
 
 
 
 
 
 
 
 
 
2e813e6
 
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a425931
5acd9c3
 
 
 
 
 
 
 
a425931
5acd9c3
 
 
 
a425931
5acd9c3
 
a425931
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
a425931
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
a425931
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a425931
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a787b8
 
 
 
 
 
 
5acd9c3
 
 
 
 
 
 
 
 
 
 
6a787b8
 
 
 
 
7dadc22
6a787b8
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a425931
5acd9c3
a425931
5acd9c3
a425931
5acd9c3
a425931
5acd9c3
 
 
 
 
 
 
 
 
 
2e813e6
5acd9c3
2e813e6
5acd9c3
2e813e6
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
2e813e6
 
 
 
 
 
 
 
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7edecf
 
 
 
 
5acd9c3
 
 
 
 
 
 
 
d7edecf
 
 
 
 
 
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7dadc22
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
d7edecf
 
5acd9c3
 
 
 
2e813e6
 
 
e15d9f5
 
2e813e6
 
e15d9f5
 
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7edecf
 
 
 
 
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7edecf
 
 
 
 
 
 
5acd9c3
 
 
 
 
 
 
 
 
d7edecf
 
 
 
 
5acd9c3
 
 
 
 
d7edecf
 
 
 
 
 
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
d7edecf
 
 
 
 
 
 
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7dadc22
 
 
5acd9c3
 
 
 
 
7dadc22
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a787b8
 
 
 
 
 
 
5acd9c3
 
 
6a787b8
 
 
 
 
 
7dadc22
6a787b8
 
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7dadc22
3cc7c13
5acd9c3
 
 
 
 
 
 
 
a425931
5acd9c3
 
 
5ca5f4b
5acd9c3
 
 
 
 
 
3cc7c13
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a787b8
 
 
 
 
 
 
5acd9c3
 
 
 
 
 
6a787b8
 
 
 
7dadc22
6a787b8
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
d7edecf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d1d1b5
7dadc22
 
 
 
8d1d1b5
d7edecf
a425931
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ab4638
a425931
6a787b8
 
 
 
 
 
 
d7edecf
 
 
 
 
 
 
 
 
 
a425931
6a787b8
 
 
 
7dadc22
6a787b8
d7edecf
 
 
 
 
 
8d1d1b5
d7edecf
 
 
8d1d1b5
 
 
8ab4638
8d1d1b5
d7edecf
8d1d1b5
 
d7edecf
 
 
 
 
 
 
 
 
 
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c71972
 
 
5acd9c3
 
 
6c71972
5acd9c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c71972
 
5acd9c3
 
 
 
 
 
 
 
 
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
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
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
import 'dart:async';
import 'dart:io' as io;
// For web platform, conditionally import http instead of HttpClient
import 'package:http/http.dart' as http;
import 'package:tikslop/services/settings_service.dart';
import 'package:synchronized/synchronized.dart';
import 'dart:convert';
// Conditionally import html for web platform with proper handling
import 'html_stub.dart' if (dart.library.html) 'dart:html' as html;
import 'package:tikslop/config/config.dart';
import 'package:tikslop/models/chat_message.dart';
import 'package:flutter/foundation.dart';
import 'package:uuid/uuid.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
import '../models/search_state.dart';
import '../models/video_result.dart';
import '../models/video_orientation.dart';
import '../utils/colored_logger.dart';

class WebSocketRequest {
  final String requestId;
  final String action;
  final Map<String, dynamic> params;

  WebSocketRequest({
    String? requestId,
    required this.action,
    required this.params,
  }) : requestId = requestId ?? const Uuid().v4();

  Map<String, dynamic> toJson() => {
        'requestId': requestId,
        'action': action,
        ...params,
      };
}

enum ConnectionStatus {
  disconnected,
  connecting,
  connected,
  reconnecting,
  error,
  maintenance
}

class WebSocketApiService {
  // Singleton implementation
  static final WebSocketApiService _instance = WebSocketApiService._internal();
  factory WebSocketApiService() => _instance;
  WebSocketApiService._internal();

  // Colored logger
  final _log = ColoredLogger.get('WebSocketApiService');

  // Dynamically build WebSocket URL based on current host in web platform
  // or use environment variable/production URL/localhost for development on other platforms
  static String get _wsUrl {
    if (kIsWeb) {
      // Get the current host and protocol from the browser window
      final location = Uri.base;
      final protocol = location.scheme == 'https' ? 'wss' : 'ws';
      
      // For localhost, explicitly include port 8080
      if (location.host == 'localhost' || location.host.startsWith('localhost:')) {
        final url = '$protocol://localhost:8080/ws';
        ColoredLogger.get('WebSocketApiService').network('Using localhost:8080 WebSocket URL: $url');
        return url;
      }
      
      // For other hosts, include the original port number in the URL
      final url = '$protocol://${location.host}/ws';
      ColoredLogger.get('WebSocketApiService').network('Using dynamic WebSocket URL: $url');
      return url;
    } else {
      // First try to get WebSocket URL from environment variable (highest priority)
      const envWsUrl = String.fromEnvironment('API_WS_URL', defaultValue: '');
      
      if (envWsUrl.isNotEmpty) {
        debugPrint('WebSocketApiService: Using WebSocket URL from environment: $envWsUrl');
        return envWsUrl;
      }
      
      // Second, check if we're in production mode (determined by build flag)
      const isProduction = bool.fromEnvironment('PRODUCTION_MODE', defaultValue: false);
      
      if (isProduction) {
        // Production default is tikslop.com
        const productionUrl = 'wss://tikslop.com/ws';
        debugPrint('WebSocketApiService: Using production WebSocket URL: $productionUrl');
        return productionUrl;
      } else {
        // Fallback to localhost for development
        debugPrint('WebSocketApiService: Using default localhost WebSocket URL');
        return 'ws://localhost:8080/ws';
      }
    }
  }
  WebSocketChannel? _channel;
  final _responseController = StreamController<Map<String, dynamic>>.broadcast();
  final _pendingRequests = <String, Completer<Map<String, dynamic>>>{};
  final _statusController = StreamController<ConnectionStatus>.broadcast();
  Timer? _heartbeatTimer;
  Timer? _reconnectTimer;
  bool _disposed = false;
  int _reconnectAttempts = 0;
  static const int _maxReconnectAttempts = 5;
  static const Duration _initialReconnectDelay = Duration(seconds: 2);
  static bool _initialized = false;

  final _connectionLock = Lock();
  final _disposeLock = Lock();
  
  final _chatController = StreamController<ChatMessage>.broadcast();
  Stream<ChatMessage> get chatStream => _chatController.stream;
  
  Stream<ConnectionStatus> get statusStream => _statusController.stream;
  ConnectionStatus _status = ConnectionStatus.disconnected;
  ConnectionStatus get status => _status;
  bool get isConnected => _status == ConnectionStatus.connected;
  bool get isInMaintenance => _status == ConnectionStatus.maintenance;

  SearchState? _currentSearchState;
  final _searchController = StreamController<VideoResult>.broadcast();
  final _activeSearches = <String, bool>{};
  static const int maxFailedAttempts = 2;
  static const int maxResults = 2;

  Stream<VideoResult> get searchStream => _searchController.stream;

  static const Duration _minRequestInterval = Duration(milliseconds: 100);
  DateTime _lastRequestTime = DateTime.now();
  final _activeRequests = <String, bool>{};

  final _subscribers = <String, int>{};

  // Track the user role
  String _userRole = 'anon';
  String get userRole => _userRole;
  
  // Stream to notify listeners when user role changes
  final _userRoleController = StreamController<String>.broadcast();
  Stream<String> get userRoleStream => _userRoleController.stream;

  Future<void> initialize() async {
    if (_initialized) return;
    
    try {
      _log.info('Initializing and connecting...');
      
      // Add page unload handler for web platform
      if (kIsWeb) {
        try {
          // Add beforeunload listener to clean up connection
          html.window.onBeforeUnload.listen((_) {
            debugPrint('WebSocketApiService: Page unloading, cleaning up connection...');
            _unregisterDeviceConnection();
          });
          
          // Also add pagehide listener as a fallback
          html.window.addEventListener('pagehide', (event) {
            debugPrint('WebSocketApiService: Page hiding, cleaning up connection...');
            _unregisterDeviceConnection();
          });
          
          // And visibilitychange for when the page is being closed
          html.document.addEventListener('visibilitychange', (event) {
            if (html.document.hidden == true) {
              debugPrint('WebSocketApiService: Page becoming hidden, updating heartbeat...');
              // Update heartbeat when page becomes hidden to keep connection alive
              _updateConnectionHeartbeat();
            }
          });
        } catch (e) {
          debugPrint('Error setting up page unload handlers: $e');
        }
      }
      
      await connect();
      
      // Only continue if we're properly connected
      if (_status != ConnectionStatus.connected) {
        debugPrint('WebSocketApiService: Connection not established, status: $_status');
        return;
      }
      
      try {
        // Request user role after connection
        await _requestUserRole();
      } catch (e) {
        // Handle the case where we fail to get user role due to device connection limit
        if (e.toString().contains('Device connection limit exceeded')) {
          // We've already set the appropriate status, just return
          return;
        }
        // Otherwise rethrow
        rethrow;
      }
      
      _initialized = true;
      debugPrint('WebSocketApiService: Successfully initialized, status: $_status');
    } catch (e) {
      debugPrint('Failed to initialize WebSocketApiService: $e');
      rethrow;
    }
  }
  
  Future<void> _requestUserRole() async {
    try {
      final response = await _sendRequest(
        WebSocketRequest(
          action: 'get_user_role',
          params: {},
        ),
        timeout: const Duration(seconds: 5),
      );
      
      if (response['success'] == true && response['user_role'] != null) {
        _userRole = response['user_role'] as String;
        _userRoleController.add(_userRole);
        _log.success('User role set to $_userRole');
        
        // Now that we know the role, check device connection limit for non-anonymous users
        if (kIsWeb && _userRole != 'anon') {
          final connectionAllowed = _checkAndRegisterDeviceConnection();
          if (!connectionAllowed) {
            _isDeviceLimitExceeded = true;
            _deviceLimitController.add(true);
            _setStatus(ConnectionStatus.error);
            throw Exception('Device connection limit exceeded');
          }
        }
      }
    } catch (e) {
      debugPrint('WebSocketApiService: Failed to get user role: $e');
      rethrow;
    }
  }

  // New status for anonymous users exceeding connection limit
  bool _isAnonLimitExceeded = false;
  bool get isAnonLimitExceeded => _isAnonLimitExceeded;
  
  // Status for VIP users exceeding device connection limit (web only)
  bool _isDeviceLimitExceeded = false;
  bool get isDeviceLimitExceeded => _isDeviceLimitExceeded;
  
  // Message to display when anonymous limit is exceeded
  String _anonLimitMessage = '';
  String get anonLimitMessage => _anonLimitMessage;
  
  // Message to display when device limit is exceeded
  final String _deviceLimitMessage = 'Too many connections from this device. Please close other tabs running TikSlop.';
  String get deviceLimitMessage => _deviceLimitMessage;
  
  // Stream to notify listeners when anonymous limit status changes
  final _anonLimitController = StreamController<bool>.broadcast();
  Stream<bool> get anonLimitStream => _anonLimitController.stream;
  
  // Stream to notify listeners when device limit status changes
  final _deviceLimitController = StreamController<bool>.broadcast();
  Stream<bool> get deviceLimitStream => _deviceLimitController.stream;
  
  // Constants for device connection limits
  static const String _connectionCountKey = 'tikslop_connection_count';
  static const String _connectionIdKey = 'tikslop_connection_id';
  static const int _maxDeviceConnections = 3; // Maximum number of tabs/connections per device
  static const Duration _connectionHeartbeatInterval = Duration(seconds: 10);
  Timer? _connectionHeartbeatTimer;
  String? _connectionId;
  
  // Function to check and register device connection (web only)
  bool _checkAndRegisterDeviceConnection() {
    if (!kIsWeb) return true; // Only apply on web platform
    
    try {
      // Try to reuse existing connection ID from localStorage if valid
      if (_connectionId == null) {
        final storedId = html.window.localStorage[_connectionIdKey];
        if (storedId != null && storedId.isNotEmpty) {
          // Check if this connection ID is still active
          final countJson = html.window.localStorage[_connectionCountKey];
          if (countJson != null && countJson.isNotEmpty) {
            try {
              final connections = json.decode(countJson) as Map<String, dynamic>;
              final now = DateTime.now().millisecondsSinceEpoch;
              final connectionTimestamp = connections[storedId];
              
              // Reuse the ID if it's still recent (within 15 seconds)
              if (connectionTimestamp is int && now - connectionTimestamp < 15000) {
                _connectionId = storedId;
                debugPrint('Reusing existing connection ID: $_connectionId');
              }
            } catch (e) {
              debugPrint('Error checking stored connection: $e');
            }
          }
        }
        
        // Generate new ID if we couldn't reuse one
        if (_connectionId == null) {
          _connectionId = const Uuid().v4();
          debugPrint('Generated new connection ID: $_connectionId');
        }
        
        // Store connection ID in localStorage
        html.window.localStorage[_connectionIdKey] = _connectionId!;
      }
      
      // Get current connection count from localStorage
      final countJson = html.window.localStorage[_connectionCountKey];
      Map<String, dynamic> connections = {};
      
      if (countJson != null && countJson.isNotEmpty) {
        try {
          connections = json.decode(countJson) as Map<String, dynamic>;
        } catch (e) {
          debugPrint('Error parsing connection count: $e');
          connections = {};
        }
      }
      
      // Clean up stale connections (older than 20 seconds - more aggressive)
      final now = DateTime.now().millisecondsSinceEpoch;
      final beforeCleanup = connections.length;
      connections.removeWhere((key, value) {
        if (value is! int) return true;
        return now - value > 20000; // 20 seconds timeout (more aggressive)
      });
      
      if (beforeCleanup != connections.length) {
        debugPrint('Cleaned up ${beforeCleanup - connections.length} stale connections');
      }
      
      // Add/update this connection
      connections[_connectionId!] = now;
      
      // Store back to localStorage
      html.window.localStorage[_connectionCountKey] = json.encode(connections);
      
      // Check if we're exceeding the limit, but only for non-anonymous users
      // For anonymous users, we rely on the server-side IP check
      if (_userRole != 'anon' && connections.length > _maxDeviceConnections) {
        debugPrint('Device connection limit exceeded: ${connections.length} connections for $_userRole user');
        debugPrint('Active connections: ${connections.keys.toList()}');
        return false;
      }
      
      return true;
    } catch (e) {
      debugPrint('Error checking device connections: $e');
      return true; // Default to allowing connection on error
    }
  }
  
  // Function to update the connection heartbeat
  void _updateConnectionHeartbeat() {
    if (!kIsWeb || _connectionId == null) return;
    
    try {
      // Get current connection count
      final countJson = html.window.localStorage[_connectionCountKey];
      Map<String, dynamic> connections = {};
      
      if (countJson != null && countJson.isNotEmpty) {
        try {
          connections = json.decode(countJson) as Map<String, dynamic>;
        } catch (e) {
          debugPrint('Error parsing connection count: $e');
          connections = {};
        }
      }
      
      // Update timestamp for this connection
      final now = DateTime.now().millisecondsSinceEpoch;
      connections[_connectionId!] = now;
      
      // Store back to localStorage
      html.window.localStorage[_connectionCountKey] = json.encode(connections);
    } catch (e) {
      debugPrint('Error updating connection heartbeat: $e');
    }
  }
  
  // Function to unregister this connection
  void _unregisterDeviceConnection() {
    if (!kIsWeb || _connectionId == null) return;
    
    try {
      // Get current connection count
      final countJson = html.window.localStorage[_connectionCountKey];
      Map<String, dynamic> connections = {};
      
      if (countJson != null && countJson.isNotEmpty) {
        try {
          connections = json.decode(countJson) as Map<String, dynamic>;
        } catch (e) {
          debugPrint('Error parsing connection count: $e');
          connections = {};
        }
      }
      
      // Remove this connection
      connections.remove(_connectionId);
      
      // Store back to localStorage
      html.window.localStorage[_connectionCountKey] = json.encode(connections);
      
      // Also remove the stored connection ID
      html.window.localStorage.remove(_connectionIdKey);
      
      // Stop the heartbeat timer
      _connectionHeartbeatTimer?.cancel();
      _connectionHeartbeatTimer = null;
    } catch (e) {
      debugPrint('Error unregistering device connection: $e');
    }
  }
  
  // Public method to clear all device connections (useful for debugging)
  static void clearAllDeviceConnections() {
    if (!kIsWeb) return;
    
    try {
      html.window.localStorage.remove(_connectionCountKey);
      html.window.localStorage.remove(_connectionIdKey);
      debugPrint('WebSocketApiService: Cleared all device connections from localStorage');
    } catch (e) {
      debugPrint('Error clearing device connections: $e');
    }
  }
  
  // Start the connection heartbeat timer
  void _startConnectionHeartbeat() {
    if (!kIsWeb) return;
    
    _connectionHeartbeatTimer?.cancel();
    _connectionHeartbeatTimer = Timer.periodic(_connectionHeartbeatInterval, (timer) {
      _updateConnectionHeartbeat();
    });
  }

  /// Reconnect to the WebSocket server, closing any existing connection first
  Future<void> reconnect() async {
    if (_disposed) {
      throw Exception('WebSocketApiService has been disposed');
    }

    debugPrint('WebSocketApiService: Forcing reconnection...');
    
    // Unregister the current connection
    _unregisterDeviceConnection();
    
    // Close existing connection if any
    if (_channel != null) {
      try {
        await _channel!.sink.close();
      } catch (e) {
        debugPrint('WebSocketApiService: Error closing existing channel: $e');
      }
      _channel = null;
    }
    
    // Reset connection state and ID
    _setStatus(ConnectionStatus.disconnected);
    _reconnectAttempts = 0;
    _connectionId = null;  // Clear connection ID to force a new one
    
    // Reset user role to anon until we get the new role from server
    _userRole = 'anon';
    _userRoleController.add(_userRole);
    
    // Connect again
    await connect();
  }

  Future<void> connect() async {
    if (_disposed) {
      throw Exception('WebSocketApiService has been disposed');
    }

    // Reset limit exceeded statuses on connection attempt
    _isAnonLimitExceeded = false;
    _isDeviceLimitExceeded = false;
    
    // Check device connection limit (for web only, but only after determining user role)
    // We'll check again after getting the actual role, this is just to prevent excessive connections
    if (kIsWeb) {
      final connectionAllowed = _checkAndRegisterDeviceConnection();
      if (!connectionAllowed) {
        _isDeviceLimitExceeded = true;
        _deviceLimitController.add(true);
        _setStatus(ConnectionStatus.error);
        throw Exception('Device connection limit exceeded');
      }
    }

    // Prevent multiple simultaneous connection attempts
    return _connectionLock.synchronized(() async {
      if (_status == ConnectionStatus.connected) {
        // debugPrint('WebSocketApiService: Already connected, skipping connection attempt');
        return;
      }
      
      if (_status == ConnectionStatus.connecting) {
        // debugPrint('WebSocketApiService: Connection already in progress, waiting...');
        
        // Wait for a short time to see if connection completes
        for (int i = 0; i < 10; i++) {
          await Future.delayed(const Duration(milliseconds: 200));
          if (_status == ConnectionStatus.connected) {
            // debugPrint('WebSocketApiService: Connection completed while waiting');
            return;
          }
          if (_status == ConnectionStatus.error || _status == ConnectionStatus.maintenance) {
            debugPrint('WebSocketApiService: Connection failed while waiting with status: $_status');
            throw Exception('Connection attempt failed with status: $_status');
          }
        }
        
        // If still connecting after waiting, we'll try again
        debugPrint('WebSocketApiService: Previous connection attempt timed out, trying again');
      }

      try {
        _setStatus(ConnectionStatus.connecting);
        // debugPrint('WebSocketApiService: Setting status to CONNECTING');
        
        // Close existing channel if any
        await _channel?.sink.close();
        _channel = null;
        
        // Get the HF API key if available
        final settings = SettingsService();
        final hfApiKey = settings.huggingfaceApiKey;
        
        // Construct the connection URL with the API key as a query parameter if available
        final baseUrl = Uri.parse(_wsUrl);
        final connectionUrl = hfApiKey.isNotEmpty 
            ? baseUrl.replace(queryParameters: {'hf_token': hfApiKey}) 
            : baseUrl;
        
        debugPrint('WebSocketApiService: Connecting to WebSocket with API key: ${hfApiKey.isNotEmpty ? 'provided' : 'not provided'}');
        
        // First check if server is in maintenance mode by making an HTTP request to the status endpoint
        try {
          // Determine HTTP URL based on WebSocket URL and current location
          String httpUrl;
          if (kIsWeb) {
            // In web, use the current location with api/status appended
            final location = Uri.base;
            final protocol = location.scheme;
            
            // For localhost, explicitly include port 8080
            if (location.host == 'localhost' || location.host.startsWith('localhost:')) {
              httpUrl = '$protocol://localhost:8080/api/status';
            } else {
              httpUrl = '$protocol://${location.host}/api/status';
            }
          } else {
            // For non-web, derive from WebSocket URL
            final wsUri = Uri.parse(_wsUrl);
            final protocol = wsUri.scheme == 'wss' ? 'https' : 'http';
            httpUrl = '$protocol://${wsUri.authority}/api/status';
          }
          
          debugPrint('WebSocketApiService: Checking maintenance status at: $httpUrl');
          
          // Use conditional import to handle platform differences
          if (kIsWeb) {
            // For web platform, use http package instead of HttpClient which is only available in dart:io
            final response = await http.get(Uri.parse(httpUrl));
            
            if (response.statusCode == 200) {
              final statusData = jsonDecode(response.body);
              
              if (statusData['maintenance_mode'] == true) {
                debugPrint('WebSocketApiService: Server is in maintenance mode');
                _setStatus(ConnectionStatus.maintenance);
                return;
              }
            }
          } else {
            // For non-web platforms, use HttpClient from dart:io
            final httpClient = io.HttpClient();
            final request = await httpClient.getUrl(Uri.parse(httpUrl));
            final response = await request.close();
            
            if (response.statusCode == 200) {
              final responseBody = await response.transform(utf8.decoder).join();
              final statusData = jsonDecode(responseBody);
              
              if (statusData['maintenance_mode'] == true) {
                debugPrint('WebSocketApiService: Server is in maintenance mode');
                _setStatus(ConnectionStatus.maintenance);
                return;
              }
            }
          }
        } catch (e) {
          debugPrint('WebSocketApiService: Failed to check maintenance status: $e');
          // Continue with connection attempt even if status check fails
        }
        
        try {
          debugPrint('WebSocketApiService: Creating WebSocket channel...');
          _channel = WebSocketChannel.connect(connectionUrl);
        } catch (e) {
          debugPrint('WebSocketApiService: Failed to create WebSocket channel: $e');
          
          // If connection fails and we were using an API key, try without it
          if (hfApiKey.isNotEmpty) {
            debugPrint('WebSocketApiService: Retrying connection without API key');
            _channel = WebSocketChannel.connect(baseUrl);
          } else {
            _setStatus(ConnectionStatus.error);
            rethrow;
          }
        }
        
        // Wait for connection with proper error handling
        try {
          debugPrint('WebSocketApiService: Waiting for connection ready signal...');
          await _channel!.ready.timeout(
            const Duration(seconds: 10),
            onTimeout: () {
              debugPrint('WebSocketApiService: Connection timeout');
              _setStatus(ConnectionStatus.error);
              throw TimeoutException('Connection timeout');
            },
          );
          debugPrint('WebSocketApiService: Connection ready signal received!');
        } catch (e) {
          debugPrint('WebSocketApiService: Connection failed: $e');
          
          String errorMessage = e.toString();
          
          // Check for anonymous user connection limit exceeded
          if (errorMessage.contains('429') && (errorMessage.contains('anon_limit_exceeded') || 
              errorMessage.contains('Anonymous user limit exceeded'))) {
            debugPrint('WebSocketApiService: Anonymous user connection limit exceeded');
            
            // Try to extract the error message from the response
            String errorMsg = 'Anonymous users can enjoy 1 stream per IP address. If you are on a shared IP please enter your HF token, thank you!';
            
            try {
              // Extract JSON content from the error message if available
              final match = RegExp(r'\{.*\}').firstMatch(errorMessage);
              if (match != null) {
                final jsonStr = match.group(0);
                if (jsonStr != null) {
                  final errorData = json.decode(jsonStr);
                  if (errorData['message'] != null) {
                    errorMsg = errorData['message'];
                  }
                }
              }
            } catch (_) {
              // If parsing fails, use the default message
            }
            
            _setStatus(ConnectionStatus.error);
            _isAnonLimitExceeded = true;
            _anonLimitMessage = errorMsg;
            _anonLimitController.add(true);
            
            // We don't rethrow here - we want to handle this specific error differently
            return;
          }
          
          // If server sent a 503 response with maintenance mode indication
          if (errorMessage.contains('503') && errorMessage.contains('maintenance')) {
            debugPrint('WebSocketApiService: Server is in maintenance mode');
            _setStatus(ConnectionStatus.maintenance);
            return;
          }
          
          // If connection fails and we were using an API key, try without it
          if (hfApiKey.isNotEmpty) {
            debugPrint('WebSocketApiService: Retrying connection without API key after ready timeout');
            
            // Close the failed channel
            await _channel?.sink.close();
            
            // Try connecting without the API key
            _channel = WebSocketChannel.connect(baseUrl);
            
            try {
              await _channel!.ready.timeout(
                const Duration(seconds: 10),
                onTimeout: () {
                  _setStatus(ConnectionStatus.error);
                  throw TimeoutException('Connection timeout on fallback attempt');
                },
              );
            } catch (retryError) {
              // Check again for maintenance mode or anonymous limit
              final retryErrorMsg = retryError.toString();
              
              if (retryErrorMsg.contains('429') && (retryErrorMsg.contains('anon_limit_exceeded') || 
                  retryErrorMsg.contains('Anonymous user limit exceeded'))) {
                debugPrint('WebSocketApiService: Anonymous user connection limit exceeded on retry');
                
                // Try to extract the error message from the response
                String errorMsg = 'Anonymous users can enjoy 1 stream per IP address. If you are on a shared IP please enter your HF token, thank you!';
                
                try {
                  // Extract JSON content from the error message if available
                  final match = RegExp(r'\{.*\}').firstMatch(retryErrorMsg);
                  if (match != null) {
                    final jsonStr = match.group(0);
                    if (jsonStr != null) {
                      final errorData = json.decode(jsonStr);
                      if (errorData['message'] != null) {
                        errorMsg = errorData['message'];
                      }
                    }
                  }
                } catch (_) {
                  // If parsing fails, use the default message
                }
                
                _setStatus(ConnectionStatus.error);
                _isAnonLimitExceeded = true;
                _anonLimitMessage = errorMsg;
                _anonLimitController.add(true);
                return;
              }
              
              if (retryErrorMsg.contains('503') && retryErrorMsg.contains('maintenance')) {
                debugPrint('WebSocketApiService: Server is in maintenance mode on retry');
                _setStatus(ConnectionStatus.maintenance);
                return;
              }
              
              debugPrint('WebSocketApiService: Fallback connection also failed: $retryError');
              _setStatus(ConnectionStatus.error);
              rethrow;
            }
          } else {
            _setStatus(ConnectionStatus.error);
            rethrow;
          }
        }
        
        // Setup stream listener with error handling
        // debugPrint('WebSocketApiService: Setting up stream listeners...');
        _channel!.stream.listen(
          _handleMessage,
          onError: _handleError,
          onDone: _handleDisconnect,
          cancelOnError: true,
        );

        _startHeartbeat();
        // Start the device connection heartbeat for web (we'll only apply limits to VIP users)
        if (kIsWeb) {
          _startConnectionHeartbeat();
        }
        
        // debugPrint('WebSocketApiService: Setting status to CONNECTED');
        _setStatus(ConnectionStatus.connected);
        _reconnectAttempts = 0;
        
        // Clear limit flags if we successfully connected
        if (_isAnonLimitExceeded) {
          _isAnonLimitExceeded = false;
          _anonLimitController.add(false);
        }
        
        if (_isDeviceLimitExceeded) {
          _isDeviceLimitExceeded = false;
          _deviceLimitController.add(false);
        }
        
        debugPrint('WebSocketApiService: Connection completed successfully');
      } catch (e) {
        // Check if the error indicates maintenance mode
        if (e.toString().contains('maintenance')) {
          debugPrint('WebSocketApiService: Server is in maintenance mode');
          _setStatus(ConnectionStatus.maintenance);
        } else {
          debugPrint('WebSocketApiService: Connection error: $e');
          _setStatus(ConnectionStatus.error);
          rethrow;
        }
      }
    });
  }

   void addSubscriber(String id) {
    _subscribers[id] = (_subscribers[id] ?? 0) + 1;
    // debugPrint('WebSocket subscriber added: $id (total: ${_subscribers[id]})');
  }

  void removeSubscriber(String id) {
    if (_subscribers.containsKey(id)) {
      _subscribers[id] = _subscribers[id]! - 1;
      if (_subscribers[id]! <= 0) {
        _subscribers.remove(id);
      }
      // debugPrint('WebSocket subscriber removed: $id (remaining: ${_subscribers[id] ?? 0})');
    }
  }

  Future<void> joinChatRoom(String videoId) async {
   // debugPrint('WebSocketApiService: Attempting to join chat room: $videoId');
    
    if (!isConnected) {
      // debugPrint('WebSocketApiService: Not connected, connecting first...');
      await connect();
    }
    
    try {
      final response = await _sendRequest(
        WebSocketRequest(
          action: 'join_chat',
          params: {'videoId': videoId},
        ),
        timeout: const Duration(seconds: 10),
      );

      // debugPrint('WebSocketApiService: Join chat room response received: $response');

      if (!response['success']) {
        final error = response['error'] ?? 'Failed to join chat room';
        debugPrint('WebSocketApiService: Join chat room failed: $error');
        throw Exception(error);
      }

      // Process chat history if provided
      if (response['messages'] != null) {
        _handleChatHistory(response);
      }

      // debugPrint('WebSocketApiService: Successfully joined chat room: $videoId');
    } catch (e) {
      debugPrint('WebSocketApiService: Error joining chat room: $e');
      rethrow;
    }
  }


  Future<void> leaveChatRoom(String videoId) async {
    if (!isConnected) return;

    try {
      await _sendRequest(
        WebSocketRequest(
          action: 'leave_chat',
          params: {'videoId': videoId},
        ),
        timeout: const Duration(seconds: 5),
      );
      // debugPrint('Successfully left chat room: $videoId');
    } catch (e) {
      debugPrint('Failed to leave chat room: $e');
    }
  }

  ////// ---- OLD VERSION OF THE CODE ------
  ///
 

  Future<void> startContinuousSearch(String query) async {
    if (!_initialized) {
      await initialize();
    }

    debugPrint('Starting continuous search for query: $query');
    _activeSearches[query] = true;
    _currentSearchState = SearchState(query: query);
    int failedAttempts = 0;

    // Get LLM settings once for the whole search session
    final settings = SettingsService();
    final llmProvider = settings.llmProvider;
    final llmModel = settings.llmModel;
    final llmApiKey = settings.llmApiKey;
    final hfApiKey = settings.huggingfaceApiKey;

    while (_activeSearches[query] == true && 
          !_disposed && 
          failedAttempts < maxFailedAttempts && 
          (_currentSearchState?.resultCount ?? 0) < maxResults) {
      try {
        final response = await _sendRequest(
          WebSocketRequest(
            action: 'search',
            params: {
              'query': query,
              'attemptCount': failedAttempts,
              'llm_config': {
                'provider': llmProvider,
                'model': llmModel,
                'api_key': llmApiKey,
                'hf_token': hfApiKey,
                'game_master_prompt': settings.gameMasterPrompt,
              },
            },
          ),
          timeout: const Duration(seconds: 30),
        );

        if (_disposed || _activeSearches[query] != true) break;

        if (response['success'] == true && response['result'] != null) {
          final result = VideoResult.fromJson(response['result'] as Map<String, dynamic>);
          _searchController.add(result);
          _currentSearchState = _currentSearchState?.incrementCount();
          failedAttempts = 0;
        } else {
          failedAttempts++;
          debugPrint('Search attempt $failedAttempts failed for query: $query. Error: ${response['error']}');
        }
      } catch (e) {
        failedAttempts++;
        debugPrint('Search error (attempt $failedAttempts): $e');
        
        if (failedAttempts < maxFailedAttempts) {
          await Future.delayed(const Duration(seconds: 2));
        }
      }
    }

    _activeSearches[query] = false;
    
    if (_disposed) {
      // debugPrint('Search terminated: Service disposed');
    } else if (failedAttempts >= maxFailedAttempts) {
      // debugPrint('Search terminated: Max failures ($maxFailedAttempts) reached');
    } else if ((_currentSearchState?.resultCount ?? 0) >= maxResults) {
      // debugPrint('Search terminated: Max results ($maxResults) reached');
    } else {
      // debugPrint('Search terminated: Search cancelled');
    }
  }

  void stopContinuousSearch(String query) {
    _activeSearches[query] = false;
  }

  String get statusMessage {
    switch (_status) {
      case ConnectionStatus.disconnected:
        return 'Disconnected';
      case ConnectionStatus.connecting:
        return 'Connected...'; // Make connecting status appear like connected to show green
      case ConnectionStatus.connected:
        return _userRole == 'anon' ? 'Connected as anon' : 'Connected as $_userRole';
      case ConnectionStatus.reconnecting:
        return 'Connection lost. Attempting to reconnect (${_reconnectAttempts + 1}/$_maxReconnectAttempts)...';
      case ConnectionStatus.error:
        return 'Failed to connect';
      case ConnectionStatus.maintenance:
        return 'Server is in maintenance mode';
    }
  }

  void _setStatus(ConnectionStatus newStatus) {
    if (_status != newStatus) {
      _status = newStatus;
      _statusController.add(newStatus);
      
      // Force an additional status emission for UI updates
      // This ensures Flutter's reactive system picks up the change
      Future.microtask(() {
        if (!_statusController.isClosed && _status == newStatus) {
          _statusController.add(newStatus);
        }
      });
    }
  }

  void _startHeartbeat() {
    _heartbeatTimer?.cancel();
    _heartbeatTimer = Timer.periodic(const Duration(seconds: 30), (timer) {
      if (isConnected) {
        _channel?.sink.add(json.encode({
          'action': 'heartbeat',
          'requestId': const Uuid().v4(),
        }));
      }
    });
  }

  Future<bool> sendChatMessage(ChatMessage message) async {
    if (_disposed) {
      debugPrint('WebSocketApiService: Cannot send message, service is disposed');
      throw Exception('WebSocketApiService is disposed');
    }
    
    if (!_initialized) {
      debugPrint('WebSocketApiService: Initializing before sending message...');
      await initialize();
    }

    try {
      debugPrint('WebSocketApiService: Sending chat message...');
      
      // Check if the chatController is still open to avoid "Cannot add events after calling close" error
      if (_chatController.isClosed) {
        debugPrint('WebSocketApiService: Chat controller is closed, cannot process messages');
        throw Exception('Chat controller is closed');
      }
      
      final response = await _sendRequest(
        WebSocketRequest(
          action: 'chat_message',
          params: {
            'videoId': message.videoId,
            ...message.toJson(),
          },
        ),
        timeout: const Duration(seconds: 10),
      );

      if (!response['success']) {
        debugPrint('WebSocketApiService: Server returned error: ${response['error']}');
        throw Exception(response['error'] ?? 'Failed to send message');
      }

      debugPrint('WebSocketApiService: Message sent successfully');
      return true;
    } catch (e) {
      debugPrint('WebSocketApiService: Error in sendChatMessage: $e');
      rethrow;
    }
  }

  void _handleMessage(dynamic message) {
    try {
      final data = json.decode(message as String) as Map<String, dynamic>;
      final action = data['action'] as String?;
      final requestId = data['requestId'] as String?;

      _log.websocket('Received message for action: $action, requestId: [$requestId]');
      
      // Update user role if present in response (from heartbeat or get_user_role)
      if (data['user_role'] != null) {
        final newRole = data['user_role'] as String;
        if (_userRole != newRole) {
          _userRole = newRole;
          _userRoleController.add(_userRole);
          debugPrint('WebSocketApiService: User role updated to $_userRole');
        }
      }

      if (requestId != null && _pendingRequests.containsKey(requestId)) {
        if (action == 'chat_message') {
          debugPrint('WebSocketApiService: Processing chat message response');
          // Don't process the message data from our own request since we already added it locally
          // We only need to complete the request to signal success/failure
          _pendingRequests[requestId]!.complete(data);
        } else if (action == 'join_chat') {
          debugPrint('WebSocketApiService: Processing join chat response');
          _pendingRequests[requestId]!.complete(data);
        } else if (action == 'search' && data['success'] == true && data['result'] != null) {
          final result = VideoResult.fromJson(data['result'] as Map<String, dynamic>);
          
          // Complete the pending request but don't add to search results here
          // The search results will be handled by the startContinuousSearch method
          _pendingRequests[requestId]!.complete(data);
          
          // Don't add to search controller here to avoid duplicates
          // _searchController.add(result);
        } else {
          // debugPrint('WebSocketApiService: Processing generic response');
          _pendingRequests[requestId]!.complete(data);
        }
        
        _cleanup(requestId);
      } else if (action == 'chat_message' && data['broadcast'] == true) {
        // For broadcast messages, the message is directly in the data
        debugPrint('WebSocketApiService: Processing chat broadcast');
        _handleChatMessage(data);
      }
      
    } catch (e, stackTrace) {
      debugPrint('WebSocketApiService: Error handling message: $e');
      debugPrint('Stack trace: $stackTrace');
    }
  }

  void _handleChatMessage(Map<String, dynamic> data) {
    if (_disposed) {
      debugPrint('WebSocketApiService: Skipping message handling, service is disposed');
      return;
    }
    
    try {
      // Log the exact data we're trying to parse
      debugPrint('Parsing chat message data: ${json.encode(data)}');
      
      // Verify required fields are present
      final requiredFields = ['userId', 'username', 'content', 'videoId'];
      final missingFields = requiredFields.where((field) => !data.containsKey(field) || data[field] == null);
      
      if (missingFields.isNotEmpty) {
        throw FormatException(
          'Missing required fields: ${missingFields.join(', ')}'
        );
      }
      
      final message = ChatMessage.fromJson(data);
      debugPrint('Successfully parsed message: ${message.toString()}');
      
      // Only add to stream if it's still open
      if (!_chatController.isClosed) {
        _chatController.add(message);
      } else {
        debugPrint('WebSocketApiService: Chat controller is closed, cannot add message');
      }
    } catch (e, stackTrace) {
      debugPrint('Error handling chat message: $e');
      debugPrint('Stack trace: $stackTrace');
      debugPrint('Raw message data: ${json.encode(data)}');
    }
  }


  void _handleChatHistory(Map<String, dynamic> data) {
    if (_disposed) {
      debugPrint('WebSocketApiService: Skipping chat history handling, service is disposed');
      return;
    }
    
    try {
      if (data['messages'] == null) {
        debugPrint('No messages found in chat history');
        return;
      }
      
      // Check if chat controller is still open
      if (_chatController.isClosed) {
        debugPrint('WebSocketApiService: Chat controller is closed, cannot process chat history');
        return;
      }

      final messages = (data['messages'] as List).map((m) {
        try {
          return ChatMessage.fromJson(m as Map<String, dynamic>);
        } catch (e) {
          debugPrint('Error parsing historical message: $e');
          debugPrint('Raw message data: ${json.encode(m)}');
          return null;
        }
      }).whereType<ChatMessage>().toList();
      
      debugPrint('Processing ${messages.length} historical messages');
      
      // Check again before adding messages in case it was closed during processing
      if (!_chatController.isClosed) {
        for (final message in messages) {
          _chatController.add(message);
        }
      } else {
        debugPrint('WebSocketApiService: Chat controller was closed during processing');
      }
    } catch (e, stackTrace) {
      debugPrint('Error handling chat history: $e');
      debugPrint('Stack trace: $stackTrace');
    }
  }

  void _handleError(dynamic error) {
    debugPrint('WebSocket error occurred: $error');
    _setStatus(ConnectionStatus.error);
    _scheduleReconnect();
  }

  void _handleDisconnect() {
    debugPrint('WebSocket disconnected');
    _setStatus(ConnectionStatus.disconnected);
    _scheduleReconnect();
  }

  void _scheduleReconnect() {
    if (_disposed || isConnected || _status == ConnectionStatus.reconnecting) {
      return;
    }

    _reconnectTimer?.cancel();

    if (_reconnectAttempts >= _maxReconnectAttempts) {
      _setStatus(ConnectionStatus.error);
      _cancelPendingRequests('Max reconnection attempts reached');
      return;
    }

    _setStatus(ConnectionStatus.reconnecting);

    final delay = _initialReconnectDelay * (1 << _reconnectAttempts);
    _reconnectTimer = Timer(delay, () async {
      _reconnectAttempts++;
      try {
        await connect();
      } catch (e) {
        debugPrint('Reconnection attempt failed: $e');
      }
    });
  }

  void _cancelPendingRequests([String? error]) {
    final err = error ?? 'WebSocket connection closed';
    _pendingRequests.forEach((_, completer) {
      if (!completer.isCompleted) {
        completer.completeError(err);
      }
    });
    _pendingRequests.clear();
  }

  Future<Map<String, dynamic>> _sendRequest(WebSocketRequest request, {Duration? timeout}) async {
    // Throttle requests
    final now = DateTime.now();
    final timeSinceLastRequest = now.difference(_lastRequestTime);
    if (timeSinceLastRequest < _minRequestInterval) {
      await Future.delayed(_minRequestInterval - timeSinceLastRequest);
    }
    _lastRequestTime = DateTime.now();

    // Prevent duplicate requests
    if (_activeRequests[request.requestId] == true) {
      debugPrint('WebSocketApiService: Duplicate request detected ${request.requestId}');
      throw Exception('Duplicate request');
    }
    _activeRequests[request.requestId] = true;

    if (!isConnected) {
      debugPrint('WebSocketApiService: Connecting before sending request...');
      await connect();
    }

    final completer = Completer<Map<String, dynamic>>();
    _pendingRequests[request.requestId] = completer;

    try {
      final requestData = request.toJson();
      _log.websocket('Sending request [${request.requestId}] (${request.action})', {
        'data': json.encode(requestData)
      });
      _channel!.sink.add(json.encode(requestData));
      
      final response = await completer.future.timeout(
        timeout ?? const Duration(seconds: 10),
        onTimeout: () {
          _log.error('Request [${request.requestId}] timed out');
          _cleanup(request.requestId);
          throw TimeoutException('Request timeout');
        },
      );
      
      return response;
    } catch (e) {
      debugPrint('WebSocketApiService: Error in _sendRequest: $e');
      _cleanup(request.requestId);
      rethrow;
    }
  }

  void _cleanup(String requestId) {
    _pendingRequests.remove(requestId);
    _activeRequests.remove(requestId);
  }

  Future<VideoResult> search(String query) async {
    if (query.trim().isEmpty) {
      throw Exception('Search query cannot be empty');
    }

    try {
      // Get LLM settings
      final settings = SettingsService();
      final llmProvider = settings.llmProvider;
      final llmModel = settings.llmModel;
      final llmApiKey = settings.llmApiKey;
      final hfApiKey = settings.huggingfaceApiKey;
      
      final response = await _sendRequest(
        WebSocketRequest(
          action: 'search',
          params: {
            'query': query,
            'llm_config': {
              'provider': llmProvider,
              'model': llmModel,
              'api_key': llmApiKey,
              'game_master_prompt': settings.gameMasterPrompt,
            },
          },
        ),
        timeout: const Duration(seconds: 30),
      );

      if (!response['success']) {
        throw Exception(response['error'] ?? 'Search failed');
      }

      final result = response['result'];
      if (result == null) {
        throw Exception('No result returned from search');
      }

      return VideoResult.fromJson(result as Map<String, dynamic>);

    } catch (e) {
      throw Exception('Error performing search: $e');
    }
  }

  Future<String> generateVideo(VideoResult video, {
    bool enhancePrompt = false,
    String? negativePrompt,
    int height = 320,
    int width = 512,
    int seed = 0,
    Duration timeout = const Duration(seconds: 8), // we keep things super tight to fail quickly, as normally a video only takes 2 seconds to generate (including the transatlantic round trip)
    VideoOrientation orientation = VideoOrientation.LANDSCAPE,
  }) async {
    final settings = SettingsService();

    final response = await _sendRequest(
      WebSocketRequest(
        action: 'generate_video',
        params: {
          'title': video.title,
          'description': video.evolvedDescription.isEmpty ? video.description : video.evolvedDescription,
          'video_prompt_prefix': settings.videoPromptPrefix,
          'options': {
            'enhance_prompt': enhancePrompt,
            'negative_prompt': negativePrompt ?? settings.negativeVideoPrompt,
            'frame_rate': Configuration.instance.originalClipFrameRate,
            'num_inference_steps': Configuration.instance.numInferenceSteps,
            'guidance_scale': Configuration.instance.guidanceScale,
            'height': Configuration.instance.originalClipHeight,
            'width': Configuration.instance.originalClipWidth,
            'num_frames': Configuration.instance.originalClipNumberOfFrames,
            'orientation': orientation.value, // Add orientation parameter
            'seed': seed,
          },
        },
      ),
      timeout: timeout,
    );

    if (!response['success']) {
      throw Exception(response['error'] ?? 'Video generation failed');
    }

    return response['video'] as String;
  }

  Future<String> generateCaption(String title, String description) async {
    // Get LLM settings
    final settings = SettingsService();
    final llmProvider = settings.llmProvider;
    final llmModel = settings.llmModel;
    final llmApiKey = settings.llmApiKey;
    final hfApiKey = settings.huggingfaceApiKey;
    
    final response = await _sendRequest(
      WebSocketRequest(
        action: 'generate_caption',
        params: {
          'title': title,
          'description': description,
          'llm_config': {
            'provider': llmProvider,
            'model': llmModel,
            'api_key': llmApiKey,
            'game_master_prompt': settings.gameMasterPrompt,
          },
        },
      ),
      timeout: const Duration(seconds: 45),
    );

    if (!response['success']) {
      throw Exception(response['error'] ?? 'caption generation failed');
    }

    return response['caption'] as String;
  }

  /// Simulate a video by evolving its description to create a dynamic narrative
  Future<Map<String, String>> simulate({
    required String videoId,
    required String originalTitle,
    required String originalDescription,
    required String currentDescription,
    required String condensedHistory,
    int evolutionCount = 0,
    String chatMessages = '',
  }) async {
    // Skip if the API is not connected
    if (!isConnected) {
      debugPrint('WebSocketApiService: Cannot simulate video, not connected');
      return {
        'evolved_description': currentDescription,
        'condensed_history': condensedHistory
      };
    }

    debugPrint('WebSocketApiService: Sending simulation request for video $videoId (evolution #$evolutionCount)');
    debugPrint('CHAT_DEBUG: WebSocket simulate() called with chatMessages length: ${chatMessages.length}');
    if (chatMessages.isNotEmpty) {
      debugPrint('CHAT_DEBUG: Chat messages content: $chatMessages');
    }
    
    try {
      // If chat messages are provided directly, use them; otherwise the default empty string is used
      String formattedChatMessages = chatMessages;
      
      // If no chat messages were provided but we have a chat stream, try to get recent messages
      if (formattedChatMessages.isEmpty) {
        // Check if we have any active chat messages in our stream
        try {
          // Get messages directly from the chatController's history
          final List<ChatMessage> recentChatMessages = [];
          // We'd ideally query for recent messages in a real implementation
          // but to avoid circular dependencies we'll use any messages provided to us
          
          if (recentChatMessages.isNotEmpty) {
            formattedChatMessages = recentChatMessages.map((msg) => 
              "${msg.username}: ${msg.content}"
            ).join("\n");
            debugPrint('WebSocketApiService: Including ${recentChatMessages.length} chat messages in simulation');
          }
        } catch (e) {
          debugPrint('WebSocketApiService: Error getting chat messages: $e');
        }
      }
      
      // debugPrint('WebSocketApiService: Chat messages included: ${formattedChatMessages.isNotEmpty ? 'Yes' : 'No'}');
      
      // Get LLM settings
      final settings = SettingsService();
      final llmProvider = settings.llmProvider;
      final llmModel = settings.llmModel;
      final llmApiKey = settings.llmApiKey;
      final hfApiKey = settings.huggingfaceApiKey;
      
      final response = await _sendRequest(
        WebSocketRequest(
          action: 'simulate',
          params: {
            'video_id': videoId,
            'original_title': originalTitle,
            'original_description': originalDescription,
            'current_description': currentDescription,
            'condensed_history': condensedHistory,
            'evolution_count': evolutionCount,
            'chat_messages': formattedChatMessages,
            'llm_config': {
              'provider': llmProvider,
              'model': llmModel,
              'api_key': llmApiKey,
              'game_master_prompt': settings.gameMasterPrompt,
            },
          },
        ),
        timeout: const Duration(seconds: 60),
      );

      if (!response['success']) {
        debugPrint('WebSocketApiService: Simulation API returned error: ${response['error']}');
        throw Exception(response['error'] ?? 'Simulation failed');
      }

      final evolvedDescription = response['evolved_description'] as String? ?? currentDescription;
      final newHistory = response['condensed_history'] as String? ?? condensedHistory;
      
      // debugPrint('WebSocketApiService: Simulation successful, received ${evolvedDescription.length} chars for evolved description');
      
      return {
        'evolved_description': evolvedDescription,
        'condensed_history': newHistory
      };
    } catch (e) {
      debugPrint('WebSocketApiService: Error simulating video: $e');
      return {
        'evolved_description': currentDescription,
        'condensed_history': condensedHistory
      };
    }
  }


  // Additional utility methods
  Future<void> waitForConnection() async {
    if (isConnected) return;

    final completer = Completer<void>();
    StreamSubscription<ConnectionStatus>? subscription;

    subscription = statusStream.listen((status) {
      if (status == ConnectionStatus.connected) {
        subscription?.cancel();
        completer.complete();
      } else if (status == ConnectionStatus.error) {
        subscription?.cancel();
        completer.completeError('Failed to connect');
      }
    });

    await connect();
    return completer.future;
  }

  void cancelRequestsForVideo(String videoId) {
    final requestsToCancel = _pendingRequests.entries
        .where((entry) => entry.key.startsWith('video_$videoId'))
        .toList();
        
    for (var entry in requestsToCancel) {
      if (!entry.value.isCompleted) {
        entry.value.completeError('Video closed');
      }
      _cleanup(entry.key);
    }
  }

  Future<void> dispose() async {
    if (_subscribers.isNotEmpty) {
      debugPrint('WebSocketApiService: Skipping disposal - active subscribers remain: ${_subscribers.length}');
      return;
    }
    
    // Use the lock to prevent multiple simultaneous disposal attempts
    return _disposeLock.synchronized(() async {
      if (_disposed) return;
      
      debugPrint('WebSocketApiService: Starting disposal...');
      _disposed = true;
      _initialized = false;
      
      // Unregister device connection (web only)
      _unregisterDeviceConnection();
      
      // Cancel timers
      _heartbeatTimer?.cancel();
      _reconnectTimer?.cancel();
      _connectionHeartbeatTimer?.cancel();
      
      // Clear all pending requests
      _cancelPendingRequests('Service is being disposed');
      
      // Close channel properly
      if (_channel != null) {
        try {
          await _channel!.sink.close();
        } catch (e) {
          debugPrint('WebSocketApiService: Error closing channel: $e');
        }
      }
      
      // Close controllers
      await _responseController.close();
      await _statusController.close();
      await _searchController.close();
      await _chatController.close();
      await _userRoleController.close();
      await _anonLimitController.close();
      await _deviceLimitController.close();
      
      _activeSearches.clear();
      _channel = null;
      
      debugPrint('WebSocketApiService: Disposal complete');
    });
  }

}