JLChen
2021-08-02 38f4fb064df09f344fc3237409c76a9fba2a8a9e
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
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
/*
 * Copyright (c) 2010-2020 Belledonne Communications SARL.
 *
 * This file is part of linphone-iphone
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
 
#import "linphone/linphonecore_utils.h"
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
 
#import "AssistantView.h"
#import "CountryListView.h"
#import "LinphoneManager.h"
#import "PhoneMainView.h"
#import "UIAssistantTextField.h"
#import "UITextField+DoneButton.h"
#import "LinphoneAppDelegate.h"
 
typedef enum _ViewElement {
    ViewElement_Username = 100,
    ViewElement_Password = 101,
    ViewElement_Password2 = 102,
    ViewElement_Email = 103,
    ViewElement_Domain = 104,
    ViewElement_URL = 105,
    ViewElement_DisplayName = 106,
    ViewElement_Phone = 107,
    ViewElement_SMSCode = 108,
    ViewElement_PhoneCC = 109,
    ViewElement_TextFieldCount = ViewElement_PhoneCC - 100 + 1,
    ViewElement_Transport = 110,
    ViewElement_Username_Label = 120,
    ViewElement_NextButton = 130,
 
    ViewElement_PhoneButton = 150,
 
    ViewElement_UsernameFormView = 181,
    ViewElement_EmailFormView = 182,
} ViewElement;
 
@implementation AssistantView
 
#pragma mark - Lifecycle Functions
 
- (id)init {
    self = [super initWithNibName:NSStringFromClass(self.class) bundle:[NSBundle mainBundle]];
    if (self != nil) {
        [[NSBundle mainBundle] loadNibNamed:@"AssistantViewScreens" owner:self options:nil];
        historyViews = [[NSMutableArray alloc] init];
        currentView = nil;
        mustRestoreView = NO;
    }
    return self;
}
 
#pragma mark - UICompositeViewDelegate Functions
 
static UICompositeViewDescription *compositeDescription = nil;
 
+ (UICompositeViewDescription *)compositeViewDescription {
    if (compositeDescription == nil) {
        compositeDescription = [[UICompositeViewDescription alloc] init:self.class
                                                              statusBar:StatusBarView.class
                                                                 tabBar:nil
                                                               sideMenu:SideMenuView.class
                                                             fullscreen:false
                                                         isLeftFragment:NO
                                                           fragmentWith:nil];
 
        compositeDescription.darkBackground = true;
    }
    return compositeDescription;
}
 
- (UICompositeViewDescription *)compositeViewDescription {
    return self.class.compositeViewDescription;
}
 
#pragma mark - ViewController Functions
 
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
 
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(registrationUpdateEvent:)
                                               name:kLinphoneRegistrationUpdate
                                             object:nil];
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(configuringUpdate:)
                                               name:kLinphoneConfiguringStateUpdate
                                             object:nil];
    if (!account_creator) {
        account_creator = linphone_account_creator_new(
            LC,
            [LinphoneManager.instance lpConfigStringForKey:@"xmlrpc_url" inSection:@"assistant" withDefault:@""]
                .UTF8String);
    }
 
    if (!mustRestoreView) {
        new_account = NULL;
        number_of_accounts_before = bctbx_list_size(linphone_core_get_account_list(LC));
        [self resetTextFields];
        [self changeView:_welcomeView back:FALSE animation:FALSE];
    }
    mustRestoreView = NO;
    _outgoingView = DialerView.compositeViewDescription;
    _qrCodeButton.hidden = !ENABLE_QRCODE;
    [self resetLiblinphone:FALSE];
    [self enableWelcomeViewButtons];
    NSString *message = NSLocalizedString(@"I accept Belledonne Communications’ terms of use and privacy policy", nil);
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:message attributes:@{NSForegroundColorAttributeName : [UIColor systemGrayColor]}];
    [attributedString addAttribute:NSLinkAttributeName
                         value:@"https://www.linphone.org/general-terms"
                         range:[[attributedString string] rangeOfString:NSLocalizedString(@"terms of use", nil)]];
    [attributedString addAttribute:NSLinkAttributeName
                         value:@"https://www.linphone.org/privacy-policy"
                         range:[[attributedString string] rangeOfString:NSLocalizedString(@"privacy policy", nil)]];
 
    NSDictionary *linkAttributes = @{NSForegroundColorAttributeName : [UIColor redColor],
                                     NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)};
 
    _acceptText.linkTextAttributes = linkAttributes;
    _acceptText.attributedText = attributedString;
    _acceptText.editable = NO;
    _acceptText.delegate = self;
}
 
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [NSNotificationCenter.defaultCenter removeObserver:self];
}
 
- (void)fitContent {
    // always resize content view so that it fits whole available width
    CGRect frame = currentView.frame;
    frame.size.width = _contentView.bounds.size.width;
    currentView.frame = frame;
 
    [_contentView setContentSize:frame.size];
    [_contentView contentSizeToFit];
    
    _qrCodeView.frame = [[UIScreen mainScreen] bounds];
}
 
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [self fitContent];
}
 
#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
    return [[UIApplication sharedApplication] openURL:URL];
}
 
#pragma mark - Utils
 
- (void)resetLiblinphone:(BOOL)core {
    if (account_creator) {
        linphone_account_creator_unref(account_creator);
        account_creator = NULL;
    }
    if (core) {
        [LinphoneManager.instance resetLinphoneCore];
    }
    account_creator = linphone_account_creator_new(
        LC, [LinphoneManager.instance lpConfigStringForKey:@"xmlrpc_url" inSection:@"assistant" withDefault:@""]
                .UTF8String);
    linphone_account_creator_set_user_data(account_creator, (__bridge void *)(self));
    linphone_account_creator_cbs_set_is_account_exist(linphone_account_creator_get_callbacks(account_creator),
                                                      assistant_is_account_used);
    linphone_account_creator_cbs_set_create_account(linphone_account_creator_get_callbacks(account_creator),
                                                    assistant_create_account);
    linphone_account_creator_cbs_set_activate_account(linphone_account_creator_get_callbacks(account_creator),
                                                    assistant_activate_account);
    linphone_account_creator_cbs_set_is_account_activated(linphone_account_creator_get_callbacks(account_creator),
                                                       assistant_is_account_activated);
    linphone_account_creator_cbs_set_recover_account(linphone_account_creator_get_callbacks(account_creator),
                                                     assistant_recover_phone_account);
    linphone_account_creator_cbs_set_is_account_linked(linphone_account_creator_get_callbacks(account_creator),
                                                       assistant_is_account_linked);
    linphone_account_creator_cbs_set_login_linphone_account(linphone_account_creator_get_callbacks(account_creator), assistant_login_linphone_account);
    
}
- (void)loadAssistantConfig:(NSString *)rcFilename {
    linphone_core_load_config_from_xml(LC,
                                       [LinphoneManager bundleFile:rcFilename].UTF8String);
    if (account_creator) {
        // Below two settings are applied to account creator when it is built.
        // Reloading Core config after won't change the account creator configuration,
        // hence the manual reload
        linphone_account_creator_set_domain(account_creator, [[LinphoneManager.instance lpConfigStringForKey:@"domain" inSection:@"assistant" withDefault:@""] UTF8String]);
        linphone_account_creator_set_algorithm(account_creator, [[LinphoneManager.instance lpConfigStringForKey:@"algorithm" inSection:@"assistant" withDefault:@""] UTF8String]);
    }
    [self changeView:nextView back:FALSE animation:TRUE];
}
 
- (void)reset {
    [LinphoneManager.instance removeAllAccounts];
    [self resetTextFields];
    [self changeView:_welcomeView back:FALSE animation:FALSE];
    _waitView.hidden = TRUE;
}
 
- (void)clearHistory {
    [historyViews removeAllObjects];
}
 
+ (NSString *)StringForXMLRPCError:(const char *)err {
#define IS(x) (strcmp(err, #x) == 0)
    if
        IS(ERROR_ACCOUNT_ALREADY_ACTIVATED)
    return NSLocalizedString(@"This account is already activated.", nil);
    if
        IS(ERROR_ACCOUNT_ALREADY_IN_USE)
    return NSLocalizedString(@"This account is already in use.", nil);
    if
        IS(ERROR_ACCOUNT_DOESNT_EXIST)
    return NSLocalizedString(@"This account does not exist.", nil);
    if
        IS(ERROR_ACCOUNT_NOT_ACTIVATED)
    return NSLocalizedString(@"This account is not activated yet.", nil);
    if
        IS(ERROR_ALIAS_ALREADY_IN_USE)
    return NSLocalizedString(@"This phone number is already used. Please type a different number. \nYou can delete "
                             @"your existing account if you want to reuse your phone number.",
                             nil);
    if
        IS(ERROR_ALIAS_DOESNT_EXIST)
    return NSLocalizedString(@"This alias does not exist.", nil);
    if
        IS(ERROR_EMAIL_ALREADY_IN_USE)
    return NSLocalizedString(@"This email address is already used.", nil);
    if
        IS(ERROR_EMAIL_DOESNT_EXIST)
    return NSLocalizedString(@"This email does not exist.", nil);
    if
        IS(ERROR_KEY_DOESNT_MATCH)
    return NSLocalizedString(@"The confirmation code is invalid. \nPlease try again.", nil);
    if
        IS(ERROR_PASSWORD_DOESNT_MATCH)
    return NSLocalizedString(@"Passwords do not match.", nil);
    if
        IS(ERROR_PHONE_ISNT_E164)
    return NSLocalizedString(@"Your phone number is invalid.", nil);
    if
        IS(ERROR_CANNOT_SEND_SMS)
    return NSLocalizedString(@"Server error, please try again later.", nil);
    if
        IS(ERROR_NO_PHONE_NUMBER)
    return NSLocalizedString(@"Please confirm your country code and enter your phone number.", nil);
    if
        IS(Missing required parameters)
    return NSLocalizedString(@"Missing required parameters", nil);
    if
        IS(ERROR_BAD_CREDENTIALS)
    return NSLocalizedString(@"Bad credentials, check your account settings", nil);
    if
        IS(ERROR_NO_PASSWORD)
    return NSLocalizedString(@"Please enter a password to your account", nil);
    if
        IS(ERROR_NO_EMAIL)
    return NSLocalizedString(@"Please enter your email", nil);
    if
        IS(ERROR_NO_USERNAME)
    return NSLocalizedString(@"Please enter a username", nil);
    if
        IS(ERROR_INVALID_CONFIRMATION)
    return NSLocalizedString(@"Your confirmation password doesn't match your password", nil);
    if
        IS(ERROR_INVALID_EMAIL)
    return NSLocalizedString(@"Your email is invalid", nil);
 
    if (!linphone_core_is_network_reachable(LC))
        return NSLocalizedString(@"There is no network connection available, enable "
                                 @"WIFI or WWAN prior to configure an account.",
                                 nil);
 
    return NSLocalizedString(@"Unknown error, please try again later.", nil);
}
 
- (void)enableWelcomeViewButtons {
    BOOL acceptTerms = [LinphoneManager.instance lpConfigBoolForKey:@"accept_terms" withDefault:FALSE];
    UIImage *image = acceptTerms ? [UIImage imageNamed:@"checkbox_checked.png"] : [UIImage imageNamed:@"checkbox_unchecked.png"];
    [_acceptButton setImage:image forState:UIControlStateNormal];
    _gotoRemoteProvisioningButton.enabled = _gotoLinphoneLoginButton.enabled = _gotoCreateAccountButton.enabled = _gotoLoginButton.enabled = acceptTerms;
}
 
+ (NSString *)errorForLinphoneAccountCreatorPhoneNumberStatus:(LinphoneAccountCreatorPhoneNumberStatus)status {
    switch (status) {
        case LinphoneAccountCreatorPhoneNumberStatusTooShort: /**< Phone number too short */
            return NSLocalizedString(@"Your phone number is too short.", nil);
        case LinphoneAccountCreatorPhoneNumberStatusTooLong: /**< Phone number too long */
            return NSLocalizedString(@"Your phone number is too long.", nil);
            return nil; /* this is not an error, just user has to finish typing */
        case LinphoneAccountCreatorPhoneNumberStatusInvalidCountryCode: /**< Country code invalid */
            return NSLocalizedString(@"Your country code is invalid.", nil);
        case LinphoneAccountCreatorPhoneNumberStatusInvalid: /**< Phone number invalid */
            return NSLocalizedString(@"Your phone number is invalid.", nil);
        default:
            return NSLocalizedString(@"Unknown error, please try again later.", nil);
    }
}
 
+ (NSString *)errorForLinphoneAccountCreatorUsernameStatus:(LinphoneAccountCreatorUsernameStatus)status {
    switch (status) {
        case LinphoneAccountCreatorUsernameStatusTooShort: /**< Username too short */
            return NSLocalizedString(@"Your username is too short.", nil);
        case LinphoneAccountCreatorUsernameStatusTooLong: /**< Username too long */
            return NSLocalizedString(@"Your username is too long.", nil);
        case LinphoneAccountCreatorUsernameStatusInvalidCharacters: /**< Contain invalid characters */
            return NSLocalizedString(@"Your username contains invalid characters.", nil);
        case LinphoneAccountCreatorUsernameStatusInvalid: /**< Invalid username */
            return NSLocalizedString(@"Your username is invalid.", nil);
        default:
            return NSLocalizedString(@"Unknown error, please try again later.", nil);
    }
}
 
+ (NSString *)errorForLinphoneAccountCreatorEmailStatus:(LinphoneAccountCreatorEmailStatus)status {
    switch (status) {
        case LinphoneAccountCreatorEmailStatusMalformed: /**< Email malformed */
            return NSLocalizedString(@"Your email is malformed.", nil);
        case LinphoneAccountCreatorEmailStatusInvalidCharacters: /**< Contain invalid characters */
            return NSLocalizedString(@"Your email contains invalid characters.", nil);
        default:
            return NSLocalizedString(@"Unknown error, please try again later.", nil);
    }
}
 
+ (NSString *)errorForLinphoneAccountCreatorPasswordStatus:(LinphoneAccountCreatorPasswordStatus)status {
    switch (status) {
        case LinphoneAccountCreatorPasswordStatusTooShort: /**< Password too short */
        // return NSLocalizedString(@"Your password is too short.", nil);
        case LinphoneAccountCreatorPasswordStatusTooLong: /**< Password too long */
            // return NSLocalizedString(@"Your password is too long.", nil);
            return nil;
        case LinphoneAccountCreatorPasswordStatusInvalidCharacters: /**< Contain invalid characters */
            return NSLocalizedString(@"Your password contains invalid characters.", nil);
        case LinphoneAccountCreatorPasswordStatusMissingCharacters: /**< Missing specific characters */
        default:
            return NSLocalizedString(@"Unknown error, please try again later.", nil);
    }
}
 
+ (NSString *)errorForLinphoneAccountCreatorActivationCodeStatus:(LinphoneAccountCreatorActivationCodeStatus)status {
    switch (status) {
        case LinphoneAccountCreatorActivationCodeStatusTooShort: /**< Activation code too short */
            return NSLocalizedString(@"Your country code is too short.", nil);
        case LinphoneAccountCreatorActivationCodeStatusTooLong: /**< Activation code too long */
            return NSLocalizedString(@"Your country code is too long.", nil);
            return nil; /* this is not an error, just user has to finish typing */
        case LinphoneAccountCreatorActivationCodeStatusInvalidCharacters: /**< Contain invalid characters */
            return NSLocalizedString(@"Your activation code contains invalid characters.", nil);
        default:
            return NSLocalizedString(@"Unknown error, please try again later.", nil);
    }
}
 
+ (NSString *)errorForLinphoneAccountCreatorStatus:(LinphoneAccountCreatorStatus)status {
    switch (status) {
        case LinphoneAccountCreatorStatusRequestFailed: /**< Request failed */
            return NSLocalizedString(@"Server error, please try again later.", nil);
        case LinphoneAccountCreatorStatusMissingArguments: /**< Request failed due to missing argument(s) */
            return NSLocalizedString(@"Missing required parameters", nil);
        case LinphoneAccountCreatorStatusMissingCallbacks: /**< Request failed due to missing callback(s) */
            return NSLocalizedString(@"Missing required callbacks", nil);
 
        /** Account status **/
        /* Existence */
        case LinphoneAccountCreatorStatusAccountNotExist: /**< Account not exist */
            return NSLocalizedString(@"This account does not exist.", nil);
        case LinphoneAccountCreatorStatusAliasExist: /**< Alias exist */
            return NSLocalizedString(
                @"This phone number is already used. Please type a different number. \nYou can delete "
                @"your existing account if you want to reuse your phone number.",
                nil);
        case LinphoneAccountCreatorStatusAliasNotExist: /**< Alias not exist */
            return NSLocalizedString(@"This alias does not exist.", nil);
        /* Activation */
        case LinphoneAccountCreatorStatusAccountAlreadyActivated: /**< Account already activated */
            return NSLocalizedString(@"This account is already activated.", nil);
        case LinphoneAccountCreatorStatusAccountNotActivated: /**< Account not activated */
            return NSLocalizedString(@"This account is not activated yet.", nil);
 
        /** Server **/
        case LinphoneAccountCreatorStatusServerError: /**< Error server */
            return NSLocalizedString(@"Server error, please try again later.", nil);
        default:
            if (!linphone_core_is_network_reachable(LC)) {
                return NSLocalizedString(@"There is no network connection available, enable "
                                         @"WIFI or WWAN prior to configure an account.",
                                         nil);
            }
            return NSLocalizedString(@"Unknown error, please try again later.", nil);
    }
}
 
+ (NSString *)errorForLinphoneAccountCreatorDomainStatus:(LinphoneAccountCreatorDomainStatus)status {
    switch (status) {
        case LinphoneAccountCreatorDomainInvalid: /**< Domain invalid */
            return NSLocalizedString(@"Invalid.", nil);
        default:
            return NSLocalizedString(@"Unknown error, please try again later.", nil);
    }
}
 
- (void)configureAccount {
    LinphoneManager *lm = LinphoneManager.instance;
 
    if (!linphone_core_is_network_reachable(LC)) {
        UIAlertController *errView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Network Error", nil)
                                                                         message:NSLocalizedString(@"There is no network connection available, enable "
                                                                                                   @"WIFI or WWAN prior to configure an account",
                                                                                                   nil)
                                                                  preferredStyle:UIAlertControllerStyleAlert];
            
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
                                                                style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {}];
            
        [errView addAction:defaultAction];
        [self presentViewController:errView animated:YES completion:nil];
        _waitView.hidden = YES;
        return;
    }
 
    // remove previous proxy config, if any
    if (new_account != NULL) {
        const LinphoneAuthInfo *auth = linphone_account_find_auth_info(new_account);
        linphone_core_remove_account(LC, new_account);
        if (auth) {
            linphone_core_remove_auth_info(LC, auth);
        }
    }
    // Used to be linphone_account_creator_create_proxy_config, which is now deprecated and has no "account" equivalent since linphone_account_creator_create_account is a very different function.
    
    /** start of linphone_account_creator_create_proxy_config re-implementation for accounts **/
    LinphoneAuthInfo *info;
    LinphoneAccountParams *accountParams = linphone_core_create_account_params(LC);
    char *identity_str = _get_identity(account_creator);
    LinphoneAddress *identity = linphone_address_new(identity_str);
 
    ms_free(identity_str);
    char const *creatorDisplayName = linphone_account_creator_get_display_name(account_creator);
    if (creatorDisplayName) {
        linphone_address_set_display_name(identity, creatorDisplayName);
    }
    linphone_account_params_set_identity_address(accountParams, identity);
    if (linphone_account_creator_get_phone_country_code(account_creator)) {
        linphone_account_params_set_international_prefix(accountParams, linphone_account_creator_get_phone_country_code(account_creator));
    } else if (linphone_account_creator_get_phone_number(account_creator)) {
        int dial_prefix_number = linphone_dial_plan_lookup_ccc_from_e164(linphone_account_creator_get_phone_number(account_creator));
        char buff[4];
        snprintf(buff, sizeof(buff), "%d", dial_prefix_number);
        linphone_account_params_set_international_prefix(accountParams, buff);
    }
    char const* creatorDomain = linphone_account_creator_get_domain(account_creator);
    if (linphone_account_params_get_server_addr(accountParams) == NULL && creatorDomain != NULL) {
        char *url = ms_strdup_printf("sip:%s", creatorDomain);
        LinphoneAddress *proxy_addr = linphone_address_new(url);
        if (proxy_addr) {
            linphone_address_set_transport(proxy_addr, linphone_account_creator_get_transport(account_creator));
            linphone_account_params_set_server_addr(accountParams, linphone_address_as_string_uri_only(proxy_addr));
            linphone_address_unref(proxy_addr);
        } else {
            linphone_account_params_set_server_addr(accountParams, creatorDomain);
        }
        ms_free(url);
    }
 
    linphone_account_params_set_register_enabled(accountParams, TRUE);
 
    const char* creatorPassword = linphone_account_creator_get_password(account_creator);
    const char* creatorHa1 = linphone_account_creator_get_ha1(account_creator);
    info = linphone_auth_info_new_for_algorithm(linphone_address_get_username(identity), // username
                                NULL, //user id
                                creatorPassword, // passwd
                                creatorPassword ? NULL : creatorHa1,  // ha1
                                !creatorPassword && creatorHa1 ? linphone_address_get_domain(identity) : NULL,  // realm - assumed to be domain
                                linphone_address_get_domain(identity), // domain
                                creatorPassword ? NULL : linphone_account_creator_get_algorithm(account_creator) //if clear text password is given, allow its usage with all algorithms.
    );
    linphone_core_add_auth_info(LC, info);
    linphone_address_unref(identity);
    
    LinphonePushNotificationConfig *pushConfig = linphone_account_params_get_push_notification_config(accountParams);
#ifdef DEBUG
#define PROVIDER_NAME "apns.dev"
#else
#define PROVIDER_NAME "apns"
#endif
    linphone_push_notification_config_set_provider(pushConfig, PROVIDER_NAME);
    
    new_account = linphone_core_create_account(LC, accountParams);
    linphone_account_params_unref(accountParams);
 
    if (linphone_core_add_account(LC, new_account) != -1) {
        if (linphone_account_creator_get_set_as_default(account_creator)) {
            linphone_core_set_default_account(LC, new_account);
        }
    }
    else {
        linphone_core_remove_auth_info(LC, info);
        linphone_auth_info_unref(info);
        new_account = NULL;
    }
    /** end of linphone_account_creator_create_proxy_config re-implementation for accounts **/
 
    if (new_account) {
        // reload address book to prepend proxy config domain to contacts' phone number
        // todo: STOP doing that!
        [[LinphoneManager.instance fastAddressBook] fetchContactsInBackGroundThread];
    } else
        [self displayAssistantConfigurationError];
    
    [LinphoneManager.instance migrationPerAccount];
}
 
- (void)displayAssistantConfigurationError {
    UIAlertController *errView = [UIAlertController
        alertControllerWithTitle:NSLocalizedString(@"Assistant error", nil)
                         message:NSLocalizedString(
                                     @"Could not configure your account, please check parameters or try again later",
                                     nil)
                  preferredStyle:UIAlertControllerStyleAlert];
 
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction *action){
                                                          }];
 
    [errView addAction:defaultAction];
    [self presentViewController:errView animated:YES completion:nil];
    _waitView.hidden = YES;
    return;
}
 
#pragma mark - UI update
 
- (void)changeView:(UIView *)view back:(BOOL)back animation:(BOOL)animation {
 
    static BOOL placement_done = NO; // indicates if the button placement has been done in the assistant choice view
 
 
    if (view == _welcomeView) {
        BOOL show_logo = [LinphoneManager.instance lpConfigBoolForKey:@"show_assistant_logo_in_choice_view_preference"];
        BOOL show_extern = ![LinphoneManager.instance lpConfigBoolForKey:@"hide_assistant_custom_account"];
        BOOL show_new = ![LinphoneManager.instance lpConfigBoolForKey:@"hide_assistant_create_account"];
        BOOL show_fetch_remote = ![LinphoneManager.instance lpConfigBoolForKey:@"show_remote_provisioning_in_assistant"];
        
        if (!placement_done) {
            // visibility
            _welcomeLogoImage.hidden = !show_logo;
            _gotoLoginButton.hidden = !show_extern;
            _gotoCreateAccountButton.hidden = !show_new;
            _gotoRemoteProvisioningButton.hidden = !show_fetch_remote;
 
            // placement
            if (show_logo && show_new && !show_extern) {
                // lower both remaining buttons
                [_gotoCreateAccountButton setCenter:[_gotoLinphoneLoginButton center]];
                [_gotoLoginButton setCenter:[_gotoLoginButton center]];
 
            } else if (!show_logo && !show_new && show_extern) {
                // move up the extern button
                [_gotoLoginButton setCenter:[_gotoCreateAccountButton center]];
            }
            placement_done = YES;
        }
        if (!show_extern && !show_logo) {
            // no option to create or specify a custom account: go to connect view directly
            view = _linphoneLoginView;
        }
    }
    
    if (currentView == _qrCodeView) {
        linphone_core_enable_video_preview(LC, FALSE);
        linphone_core_enable_qrcode_video_preview(LC, FALSE);
        LinphoneAppDelegate *delegate = (LinphoneAppDelegate *)UIApplication.sharedApplication.delegate;
        delegate.onlyPortrait = FALSE;
    }
 
    // Animation
    if (animation && ANIMATED) {
        CATransition *trans = [CATransition animation];
        [trans setType:kCATransitionPush];
        [trans setDuration:0.35];
        [trans setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        if (back) {
            [trans setSubtype:kCATransitionFromLeft];
        } else {
            [trans setSubtype:kCATransitionFromRight];
        }
        [_contentView.layer addAnimation:trans forKey:@"Transition"];
    }
 
    // Stack current view
    if (currentView != nil) {
        if (!back)
            [historyViews addObject:currentView];
        [currentView removeFromSuperview];
    }
 
    // Set current view
    currentView = view;
    [_contentView insertSubview:currentView atIndex:0];
    [_contentView setContentOffset:CGPointMake(0, -_contentView.contentInset.top) animated:NO];
 
    // Resize next button to fix text length
    UIRoundBorderedButton *button = [self findButton:ViewElement_NextButton];
    CGSize size = [button.titleLabel.text sizeWithFont:button.titleLabel.font];
    size.width += 60;
    CGRect frame = button.frame;
    frame.origin.x += (button.frame.size.width - size.width) / 2;
    frame.size.width = size.width;
    [button setFrame:frame];
 
    [self fitContent];
 
    // also force next button alignement on create account page
    if ([self findView:ViewElement_PhoneButton inView:currentView ofType:UIRoundBorderedButton.class]) {
        CTTelephonyNetworkInfo *networkInfo = [CTTelephonyNetworkInfo new];
        CTCarrier *carrier = networkInfo.subscriberCellularProvider;
        NSDictionary *country = [CountryListView countryWithIso:carrier.isoCountryCode];
 
        if (!IPAD) {
            UISwitch *emailSwitch = (UISwitch *)[self findView:ViewElement_EmailFormView inView:self.contentView ofType:UISwitch.class];
            UILabel *emailLabel = (UILabel *)[self findView:ViewElement_EmailFormView inView:self.contentView ofType:UILabel.class];
            [emailSwitch removeFromSuperview];
            [emailLabel removeFromSuperview];
            //Move up the createAccountButton
            CGRect r1 = [currentView frame];
            r1.size.height = 460;
            [currentView setFrame:r1];
        }
 
        if (!country) {
            //fetch phone locale
            for (NSString* lang in [NSLocale preferredLanguages]) {
                NSUInteger idx = [lang rangeOfString:@"-"].location;
                idx = (idx == NSNotFound) ? idx = 0 : idx + 1;
                if ((country = [CountryListView countryWithIso:[lang substringFromIndex:idx]]) != nil)
                    break;
            }
        }
 
        if (country) {
            [self didSelectCountry:country];
        }
        [self onFormSwitchToggle:nil];
    }
 
    // every UITextField subviews with phone keyboard must be tweaked to have a done button
    [self addDoneButtonRecursivelyInView:self.view];
    [self prepareErrorLabels];
}
 
- (void)addDoneButtonRecursivelyInView:(UIView *)subview {
    for (UIView *child in [subview subviews]) {
        if ([child isKindOfClass:UITextField.class]) {
            UITextField *tf = (UITextField *)child;
            if (tf.keyboardType == UIKeyboardTypePhonePad || tf.keyboardType == UIKeyboardTypeNumberPad) {
                [tf addDoneButton];
            }
        }
        [self addDoneButtonRecursivelyInView:child];
    }
}
 
- (void)fillDefaultValues {
    [self resetTextFields];
 
    LinphoneAccountParams *default_account_params = linphone_core_create_account_params(LC);
    LinphoneAccount *default_account = linphone_core_create_account(LC, default_account_params);
    const char *identity = linphone_account_params_get_identity(linphone_account_get_params(default_account));
    if (identity) {
        LinphoneAddress *default_addr = linphone_core_interpret_url(LC, identity);
        if (default_addr) {
            const char *domain = linphone_address_get_domain(default_addr);
            const char *username = linphone_address_get_username(default_addr);
            if (domain && strlen(domain) > 0) {
                [self findTextField:ViewElement_Domain].text = [NSString stringWithUTF8String:domain];
            }
            if (username && strlen(username) > 0 && username[0] != '?') {
                [self findTextField:ViewElement_Username].text = [NSString stringWithUTF8String:username];
            }
        }
    }
 
    [self changeView:_remoteProvisioningLoginView back:FALSE animation:TRUE];
 
    linphone_account_params_unref(default_account_params);
}
 
- (void)resetTextFields {
    for (UIView *view in @[
             _welcomeView,
             _createAccountView,
             _linphoneLoginView,
             _loginView,
             _createAccountActivateEmailView,
             _createAccountActivateSMSView,
             _remoteProvisioningLoginView
         ]) {
        [AssistantView cleanTextField:view];
#if DEBUG
        UIAssistantTextField *atf =
            (UIAssistantTextField *)[self findView:ViewElement_Domain inView:view ofType:UIAssistantTextField.class];
        atf.text = @"test.linphone.org";
#endif
    }
    phone_number_length = 0;
}
 
+ (void)cleanTextField:(UIView *)view {
    if ([view isKindOfClass:UIAssistantTextField.class]) {
        [(UIAssistantTextField *)view setText:@""];
        ((UIAssistantTextField *)view).canShowError = NO;
    } else if (view.tag == ViewElement_PhoneButton) {
        [(UIButton *)view setTitle:NSLocalizedString(@"Select your country", nil) forState:UIControlStateNormal];
    } else {
        for (UIView *subview in view.subviews) {
            [AssistantView cleanTextField:subview];
        }
    }
}
 
- (UIView *)findView:(ViewElement)tag inView:view ofType:(Class)type {
    for (UIView *child in [view subviews]) {
        if (child.tag == tag && child.class == type) {
            return child;
        } else {
            UIView *o = [self findView:tag inView:child ofType:type];
            if (o)
                return o;
        }
    }
    return nil;
}
 
- (UIAssistantTextField *)findTextField:(ViewElement)tag {
    return (UIAssistantTextField *)[self findView:tag inView:self.contentView ofType:[UIAssistantTextField class]];
}
 
- (UIRoundBorderedButton *)findButton:(ViewElement)tag {
    return (UIRoundBorderedButton *)[self findView:tag inView:self.contentView ofType:[UIRoundBorderedButton class]];
}
 
- (UILabel *)findLabel:(ViewElement)tag {
    return (UILabel *)[self findView:tag inView:self.contentView ofType:[UILabel class]];
}
 
- (void)prepareErrorLabels {
    UIAssistantTextField *createUsername = [self findTextField:ViewElement_Username];
    [createUsername
        showError:[AssistantView
                      errorForLinphoneAccountCreatorUsernameStatus:LinphoneAccountCreatorUsernameStatusInvalid]
             when:^BOOL(NSString *inputEntry) {
               LinphoneAccountCreatorUsernameStatus s =
                   linphone_account_creator_set_username(account_creator, inputEntry.UTF8String);
               if (s != LinphoneAccountCreatorUsernameStatusOk)
                   linphone_account_creator_set_username(account_creator, NULL);
               createUsername.errorLabel.text = [AssistantView errorForLinphoneAccountCreatorUsernameStatus:s];
               return s != LinphoneAccountCreatorUsernameStatusOk;
             }];
    UIAssistantTextField *createPhone = [self findTextField:ViewElement_Phone];
    [createPhone
        showError:[AssistantView
                      errorForLinphoneAccountCreatorPhoneNumberStatus:LinphoneAccountCreatorPhoneNumberStatusInvalid]
             when:^BOOL(NSString *inputEntry) {
 
               UIAssistantTextField *countryCodeField = [self findTextField:ViewElement_PhoneCC];
               NSString *newStr =
                   [countryCodeField.text substringWithRange:NSMakeRange(1, [countryCodeField.text length] - 1)];
               NSString *prefix = (inputEntry.length > 0) ? newStr : nil;
               LinphoneAccountCreatorPhoneNumberStatus s = linphone_account_creator_set_phone_number(
                   account_creator, inputEntry.length > 0 ? inputEntry.UTF8String : NULL, prefix.UTF8String);
               if (s != LinphoneAccountCreatorPhoneNumberStatusOk) {
                   linphone_account_creator_set_phone_number(account_creator, NULL, NULL);
               }
 
               createPhone.errorLabel.text = [AssistantView errorForLinphoneAccountCreatorPhoneNumberStatus:s];
 
               return s != LinphoneAccountCreatorPhoneNumberStatusOk;
             }];
 
    UIAssistantTextField *password = [self findTextField:ViewElement_Password];
    [password showError:[AssistantView
                            errorForLinphoneAccountCreatorPasswordStatus:LinphoneAccountCreatorPasswordStatusTooShort]
                   when:^BOOL(NSString *inputEntry) {
                     LinphoneAccountCreatorPasswordStatus s =
                         linphone_account_creator_set_password(account_creator, inputEntry.UTF8String);
                     password.errorLabel.text = [AssistantView errorForLinphoneAccountCreatorPasswordStatus:s];
                     return s != LinphoneAccountCreatorPasswordStatusOk;
                   }];
 
    UIAssistantTextField *password2 = [self findTextField:ViewElement_Password2];
    [password2 showError:NSLocalizedString(@"The confirmation code is invalid. \nPlease check your SMS and try again.", nil)
                    when:^BOOL(NSString *inputEntry) {
                      return ![inputEntry isEqualToString:[self findTextField:ViewElement_Password].text];
                    }];
 
    UIAssistantTextField *email = [self findTextField:ViewElement_Email];
    [email
        showError:[AssistantView errorForLinphoneAccountCreatorEmailStatus:LinphoneAccountCreatorEmailStatusMalformed]
             when:^BOOL(NSString *inputEntry) {
               LinphoneAccountCreatorEmailStatus s =
                   linphone_account_creator_set_email(account_creator, inputEntry.UTF8String);
               email.errorLabel.text = [AssistantView errorForLinphoneAccountCreatorEmailStatus:s];
               return s != LinphoneAccountCreatorEmailStatusOk;
             }];
 
    UIAssistantTextField *domain = [self findTextField:ViewElement_Domain];
    [domain showError:[AssistantView errorForLinphoneAccountCreatorDomainStatus:LinphoneAccountCreatorDomainInvalid]
                 when:^BOOL(NSString *inputEntry) {
                   if (![inputEntry isEqualToString:@""]) {
                       LinphoneAccountCreatorDomainStatus s =
                           linphone_account_creator_set_domain(account_creator, inputEntry.UTF8String);
                       domain.errorLabel.text = [AssistantView errorForLinphoneAccountCreatorDomainStatus:s];
                       return s != LinphoneAccountCreatorDomainOk;
                   }
                   return true;
                 }];
 
    UIAssistantTextField *url = [self findTextField:ViewElement_URL];
    [url showError:NSLocalizedString(@"Invalid remote provisioning URL", nil)
              when:^BOOL(NSString *inputEntry) {
                if (inputEntry.length > 0) {
                    bool isValid = linphone_core_set_provisioning_uri(LC, [self addSchemeToProvisiionninUriIMissing:inputEntry].UTF8String) != 0;
                    linphone_core_set_provisioning_uri(LC,NULL);
                    return isValid;
                }
                return TRUE;
              }];
 
    UIAssistantTextField *displayName = [self findTextField:ViewElement_DisplayName];
    [displayName showError:[AssistantView
                               errorForLinphoneAccountCreatorUsernameStatus:LinphoneAccountCreatorUsernameStatusInvalid]
                      when:^BOOL(NSString *inputEntry) {
                        LinphoneAccountCreatorUsernameStatus s = LinphoneAccountCreatorUsernameStatusOk;
                        if (inputEntry.length > 0) {
                            s = linphone_account_creator_set_display_name(account_creator, inputEntry.UTF8String);
                            displayName.errorLabel.text =
                                [AssistantView errorForLinphoneAccountCreatorUsernameStatus:s];
                        }
                        return s != LinphoneAccountCreatorUsernameStatusOk;
                      }];
 
    UIAssistantTextField *smsCode = [self findTextField:ViewElement_SMSCode];
    [smsCode showError:nil when:^BOOL(NSString *inputEntry) {
        return inputEntry.length != 4;
    }];
    [self shouldEnableNextButton];
 
}
 
-(NSString *) addSchemeToProvisiionninUriIMissing:(NSString *)uri {
    // missing prefix will result in http:// being used
    return [uri rangeOfString:@"://"].location == NSNotFound ? [NSString stringWithFormat:@"http://%@", uri] : uri;
}
 
- (void)shouldEnableNextButton {
    BOOL invalidInputs = NO;
    for (int i = 0; !invalidInputs && i < ViewElement_TextFieldCount; i++) {
        ViewElement ve = (ViewElement)100+i;
        if ([self findTextField:ve].isInvalid) {
            invalidInputs = YES;
            break;
        }
    }
    
    UISwitch *emailSwitch = (UISwitch *)[self findView:ViewElement_EmailFormView inView:self.contentView ofType:UISwitch.class];
    if (!emailSwitch.isOn) {
        [self findButton:ViewElement_NextButton].enabled = !invalidInputs;
    }
}
 
- (BOOL) checkFields {
    UISwitch *emailSwitch = (UISwitch *)[self findView:ViewElement_EmailFormView inView:self.contentView ofType:UISwitch.class];
    if (emailSwitch.isOn) {
        if ([self findTextField:ViewElement_Username].text.length == 0) {
            [self showErrorPopup:"ERROR_NO_USERNAME"];
            return FALSE;
        }
        if ([self findTextField:ViewElement_Email].text.length == 0) {
            [self showErrorPopup:"ERROR_NO_EMAIL"];
            return FALSE;
        } else {
            LinphoneAccountCreatorEmailStatus s = linphone_account_creator_set_email(
                account_creator, [self findTextField:ViewElement_Email].text.UTF8String);
            if (s == LinphoneAccountCreatorEmailStatusMalformed) {
                [self showErrorPopup:"ERROR_INVALID_EMAIL"];
                return FALSE;
            }
        }
        if ([self findTextField:ViewElement_Password].text.length == 0) {
            [self showErrorPopup:"ERROR_NO_PASSWORD"];
            return FALSE;
        }
        if (![[self findTextField:ViewElement_Password].text isEqualToString:[self findTextField:ViewElement_Password2].text]) {
            [self showErrorPopup:"ERROR_INVALID_CONFIRMATION"];
            return FALSE;
        }
        
        return TRUE;
    } else {
        return TRUE;
    }
}
 
#pragma mark - Event Functions
 
- (void)registrationUpdateEvent:(NSNotification *)notif {
    NSString *message = [notif.userInfo objectForKey:@"message"];
    [self registrationUpdate:[[notif.userInfo objectForKey:@"state"] intValue]
                    forAccount:[[notif.userInfo objectForKeyedSubscript:@"account"] pointerValue]
                     message:message];
}
 
- (void)registrationUpdate:(LinphoneRegistrationState)state
                  forAccount:(LinphoneAccount *)account
                   message:(NSString *)message {
    // in assistant we only care about ourself
    if (account != new_account) {
        return;
    }
 
    switch (state) {
        case LinphoneRegistrationOk: {
            _waitView.hidden = true;
 
            [LinphoneManager.instance
                lpConfigSetInt:[NSDate new].timeIntervalSince1970 +
                               [LinphoneManager.instance lpConfigIntForKey:@"link_account_popup_time" withDefault:84200]
                        forKey:@"must_link_account_time"];
            [PhoneMainView.instance popToView:_outgoingView];
            break;
        }
        case LinphoneRegistrationNone:
        case LinphoneRegistrationCleared: {
            _waitView.hidden = true;
            break;
        }
        case LinphoneRegistrationFailed: {
            _waitView.hidden = true;
            UIAlertController *errView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Connection failure", nil)
                                                                             message:message
                                                                      preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
                                                                    style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {}];
            
            UIAlertAction* continueAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Continue", nil)
                                                                     style:UIAlertActionStyleDefault
                                                                   handler:^(UIAlertAction * action) {
                                                                       [PhoneMainView.instance popToView:DialerView.compositeViewDescription];
                                                                   }];
            
            [errView addAction:defaultAction];
            [errView addAction:continueAction];
            [self presentViewController:errView animated:YES completion:nil];
            break;
        }
        case LinphoneRegistrationProgress: {
            _waitView.hidden = false;
            break;
        }
        default:
            break;
    }
}
 
- (void)configuringUpdate:(NSNotification *)notif {
    LinphoneConfiguringState status = (LinphoneConfiguringState)[[notif.userInfo valueForKey:@"state"] integerValue];
 
    switch (status) {
        case LinphoneConfiguringSuccessful:
            // we successfully loaded a remote provisioned config, go to dialer
            [LinphoneManager.instance lpConfigSetInt:[NSDate new].timeIntervalSince1970
                                              forKey:@"must_link_account_time"];
            if (number_of_accounts_before < bctbx_list_size(linphone_core_get_account_list(LC))) {
                LOGI(@"A proxy config was set up with the remote provisioning, skip assistant");
                [self onDialerClick:nil];
            }
 
            if (nextView == nil) {
                [self fillDefaultValues];
            } else {
                [self changeView:nextView back:false animation:TRUE];
                nextView = nil;
            }
            break;
        case LinphoneConfiguringFailed: {
            _waitView.hidden = true;
            NSString *error_message = [notif.userInfo valueForKey:@"message"];
            UIAlertController *errView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Provisioning Load error", nil)
                                                                             message:error_message
                                                                      preferredStyle:UIAlertControllerStyleAlert];
                
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK"
                                                                    style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {}];
            
            [errView addAction:defaultAction];
            [self presentViewController:errView animated:YES completion:nil];
            break;
        }
 
        case LinphoneConfiguringSkipped:
            _waitView.hidden = true;
        default:
            break;
    }
}
 
- (void)showErrorPopup:(const char *)error {
    const char *err = error ? error : "";
    if (strcmp(err, "ERROR_BAD_CREDENTIALS") == 0) {
        UIAlertController *errView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Connection failure", nil)
                                                                         message:[AssistantView StringForXMLRPCError:err]
                                                                  preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
                                                                style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {}];
        
        UIAlertAction* continueAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Continue", nil)
                                                                 style:UIAlertActionStyleDefault
                                                               handler:^(UIAlertAction * action) {
                                                                   [PhoneMainView.instance popToView:DialerView.compositeViewDescription];
                                                               }];
 
        defaultAction.accessibilityLabel = @"PopUpResp";
        [errView addAction:defaultAction];
        [errView addAction:continueAction];
        [self presentViewController:errView animated:YES completion:nil];
    } else if (strcmp(err, "ERROR_KEY_DOESNT_MATCH") == 0) {
        UIAlertController *errView =
            [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Account configuration issue", nil)
                                                message:[AssistantView StringForXMLRPCError:err]
                                         preferredStyle:UIAlertControllerStyleAlert];
 
        UIAlertAction *defaultAction = [UIAlertAction
            actionWithTitle:@"OK"
                      style:UIAlertActionStyleDefault
                    handler:^(UIAlertAction *action) {
                      NSString *tmp_phone =
                          [NSString stringWithUTF8String:linphone_account_creator_get_phone_number(account_creator)];
                      int ccc = -1;
                      const LinphoneDialPlan *dialplan = NULL;
                      char *nationnal_significant_number = NULL;
                      ccc = linphone_dial_plan_lookup_ccc_from_e164(tmp_phone.UTF8String);
                      if (ccc > -1) { /*e164 like phone number*/
                          dialplan = linphone_dial_plan_by_ccc_as_int(ccc);
                          nationnal_significant_number = strstr(tmp_phone.UTF8String, linphone_dial_plan_get_country_calling_code(dialplan));
                          if (nationnal_significant_number) {
                              nationnal_significant_number += strlen(linphone_dial_plan_get_country_calling_code(dialplan));
                          }
                      }
                      [self changeView:_linphoneLoginView back:FALSE animation:TRUE];
                      UISwitch *usernameSwitch = (UISwitch *)[self findView:ViewElement_UsernameFormView
                                                                     inView:self.contentView
                                                                     ofType:UISwitch.class];
                      [usernameSwitch setOn:FALSE];
                      UIView *usernameView =
                          [self findView:ViewElement_UsernameFormView inView:self.contentView ofType:UIView.class];
                      usernameView.hidden = !usernameSwitch.isOn;
                      if (nationnal_significant_number) {
                          ((UITextField *)[self findView:ViewElement_Phone
                                                  inView:_linphoneLoginView
                                                  ofType:[UIAssistantTextField class]])
                              .text = [NSString stringWithUTF8String:nationnal_significant_number];
                      }
                      ((UITextField *)[self findView:ViewElement_SMSCode
                                              inView:_createAccountActivateSMSView
                                              ofType:[UITextField class]])
                          .text = @"";
                      linphone_account_creator_set_activation_code(account_creator, "");
                      if (linphone_dial_plan_get_iso_country_code(dialplan)) {
                          NSDictionary *country = [CountryListView
                              countryWithIso:[NSString stringWithUTF8String:linphone_dial_plan_get_iso_country_code(dialplan)]];
                          [self didSelectCountry:country];
                      }
                      // Reset phone number in account_creator to be sure to let the user retry
                      if (nationnal_significant_number) {
                          linphone_account_creator_set_phone_number(account_creator, nationnal_significant_number,
                                                                    linphone_dial_plan_get_country_calling_code(dialplan));
                      }
                    }];
 
        defaultAction.accessibilityLabel = @"PopUpResp";
        [errView addAction:defaultAction];
        [self presentViewController:errView animated:YES completion:nil];
    } else {
        UIAlertController *errView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Account configuration issue", nil)
                                                                         message:[AssistantView StringForXMLRPCError:err]
                                                                  preferredStyle:UIAlertControllerStyleAlert];
    
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {}];
    
        [errView addAction:defaultAction];
        defaultAction.accessibilityLabel = @"PopUpResp";
        [self presentViewController:errView animated:YES completion:nil];
    }
    
    // enable linphoneLoginButton if error
    [_linphoneLoginButton setBackgroundColor:[UIColor clearColor]];
    _linphoneLoginButton.enabled = YES;
}
 
- (void)isAccountUsed:(LinphoneAccountCreatorStatus)status withResp:(const char *)resp {
    if (currentView == _linphoneLoginView) {
        if (status == LinphoneAccountCreatorStatusAccountExistWithAlias) {
            _outgoingView = DialerView.compositeViewDescription;
            [self configureAccount];
        } else if (status == LinphoneAccountCreatorStatusAccountExist) {
            _outgoingView = AssistantLinkView.compositeViewDescription;
            [self configureAccount];
        } else {
            if (resp) {
                if (linphone_account_creator_get_username(account_creator) &&
                    (strcmp(resp, "ERROR_ACCOUNT_DOESNT_EXIST") == 0)) {
                    [self showErrorPopup:"ERROR_BAD_CREDENTIALS"];
                } else {
                    [self showErrorPopup:resp];
                }
            } else {
                [self showErrorPopup:""];
            }
        }
    } else {
        if (status == LinphoneAccountCreatorStatusAccountExist ||
            status == LinphoneAccountCreatorStatusAccountExistWithAlias) {
            if (linphone_account_creator_get_phone_number(account_creator) != NULL) {
                // Offer the possibility to resend a sms confirmation in some cases
                linphone_account_creator_is_account_activated(account_creator);
            } else {
                [self showErrorPopup:resp];
            }
        } else if (status == LinphoneAccountCreatorStatusAccountNotExist) {
            NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
            linphone_account_creator_set_language(account_creator, [[language substringToIndex:2] UTF8String]);
            linphone_account_creator_create_account(account_creator);
        } else {
            [self showErrorPopup:resp];
    
        }
    }
}
 
- (void) isAccountActivated:(const char *)resp {
    if (currentView != _createAccountView) {
        if( linphone_account_creator_get_phone_number(account_creator) == NULL) {
            [self configureAccount];
            [PhoneMainView.instance changeCurrentView:AssistantLinkView.compositeViewDescription];
        } else {
            [PhoneMainView.instance changeCurrentView:DialerView.compositeViewDescription];
        }
    } else {
        if (!linphone_account_creator_get_username(account_creator)) {
            [self showErrorPopup:"ERROR_ALIAS_ALREADY_IN_USE"];
        } else {
            [self showErrorPopup:"ERROR_ACCOUNT_ALREADY_IN_USE"];
        }
    }
}
 
#pragma mark - Account creator callbacks
 
void assistant_is_account_used(LinphoneAccountCreator *creator, LinphoneAccountCreatorStatus status, const char *resp) {
    AssistantView *thiz = (__bridge AssistantView *)(linphone_account_creator_get_user_data(creator));
    thiz.waitView.hidden = YES;
    [thiz isAccountUsed:status withResp:resp];
}
 
void assistant_create_account(LinphoneAccountCreator *creator, LinphoneAccountCreatorStatus status, const char *resp) {
    AssistantView *thiz = (__bridge AssistantView *)(linphone_account_creator_get_user_data(creator));
    thiz.waitView.hidden = YES;
    if (status == LinphoneAccountCreatorStatusAccountCreated) {
        if (linphone_account_creator_get_phone_number(creator)) {
            NSString* phoneNumber = [NSString stringWithUTF8String:linphone_account_creator_get_phone_number(creator)];
            thiz.activationSMSText.text = [NSString stringWithFormat:NSLocalizedString(@"We have sent a SMS with a validation code to %@. To complete your phone number verification, please enter the 4 digit code below:", nil), phoneNumber];
            [thiz changeView:thiz.createAccountActivateSMSView back:FALSE animation:TRUE];
        } else {
            NSString* email = [NSString stringWithUTF8String:linphone_account_creator_get_email(creator)];
            thiz.activationEmailText.text = [NSString stringWithFormat:NSLocalizedString(@" Your account is created. We have sent a confirmation email to %@. Please check your mails to validate your account. Once it is done, come back here and click on the button.", nil), email];
            [thiz changeView:thiz.createAccountActivateEmailView back:FALSE animation:TRUE];
        }
    } else {
        [thiz showErrorPopup:resp];
    }
}
 
void assistant_recover_phone_account(LinphoneAccountCreator *creator, LinphoneAccountCreatorStatus status,
                                     const char *resp) {
    AssistantView *thiz = (__bridge AssistantView *)(linphone_account_creator_get_user_data(creator));
    thiz.waitView.hidden = YES;
    if (status == LinphoneAccountCreatorStatusRequestOk) {
        NSString* phoneNumber = [NSString stringWithUTF8String:linphone_account_creator_get_phone_number(creator)];
        thiz.activationSMSText.text = [NSString stringWithFormat:NSLocalizedString(@"We have sent a SMS with a validation code to %@. To complete your phone number verification, please enter the 4 digit code below:", nil), phoneNumber];
        [thiz changeView:thiz.createAccountActivateSMSView back:FALSE animation:TRUE];
    } else {
        if(!resp) {
            [thiz showErrorPopup:"ERROR_CANNOT_SEND_SMS"];
        } else {
            [thiz showErrorPopup:resp];
        }
    }
}
 
void assistant_activate_account(LinphoneAccountCreator *creator, LinphoneAccountCreatorStatus status,
                                const char *resp) {
    AssistantView *thiz = (__bridge AssistantView *)(linphone_account_creator_get_user_data(creator));
    thiz.waitView.hidden = YES;
    if (status == LinphoneAccountCreatorStatusAccountActivated) {
        [thiz configureAccount];
        [[NSNotificationCenter defaultCenter] postNotificationName:kLinphoneAddressBookUpdate object:NULL];
    } else if (status == LinphoneAccountCreatorStatusAccountAlreadyActivated) {
        // in case we are actually trying to link account, let's try it now
        linphone_account_creator_activate_alias(creator);
    } else {
        [thiz showErrorPopup:resp];
    }
}
 
void assistant_login_linphone_account(LinphoneAccountCreator *creator, LinphoneAccountCreatorStatus status,
                                const char *resp) {
    AssistantView *thiz = (__bridge AssistantView *)(linphone_account_creator_get_user_data(creator));
    thiz.waitView.hidden = YES;
    if (status == LinphoneAccountCreatorStatusRequestOk) {
        [thiz configureAccount];
        [[NSNotificationCenter defaultCenter] postNotificationName:kLinphoneAddressBookUpdate object:NULL];
    } else {
        [thiz showErrorPopup:resp];
    }
}
 
void assistant_is_account_activated(LinphoneAccountCreator *creator, LinphoneAccountCreatorStatus status,
                                    const char *resp) {
    AssistantView *thiz = (__bridge AssistantView *)(linphone_account_creator_get_user_data(creator));
    thiz.waitView.hidden = YES;
    if (status == LinphoneAccountCreatorStatusAccountActivated) {
        [thiz isAccountActivated:resp];
    } else if (status == LinphoneAccountCreatorStatusAccountNotActivated) {
        if (!IPAD || linphone_account_creator_get_phone_number(creator) != NULL) {
            //Re send SMS if the username is the phone number
            if (linphone_account_creator_get_username(creator) != linphone_account_creator_get_phone_number(creator) && linphone_account_creator_get_username(creator) != NULL) {
                [thiz showErrorPopup:"ERROR_ACCOUNT_ALREADY_IN_USE"];
                [thiz findButton:ViewElement_NextButton].enabled = NO;
            } else {
                NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
                linphone_account_creator_set_language(creator, [[language substringToIndex:2] UTF8String]);
                linphone_account_creator_recover_account(creator);
            }
        } else {
            // TODO : Re send email ?
            [thiz showErrorPopup:"ERROR_ACCOUNT_ALREADY_IN_USE"];
            [thiz findButton:ViewElement_NextButton].enabled = NO;
        }
    } else {
        [thiz showErrorPopup:resp];
    }
}
 
void assistant_is_account_linked(LinphoneAccountCreator *creator, LinphoneAccountCreatorStatus status,
                                    const char *resp) {
    AssistantView *thiz = (__bridge AssistantView *)(linphone_account_creator_get_user_data(creator));
    thiz.waitView.hidden = YES;
    if (status == LinphoneAccountCreatorStatusAccountLinked) {
        [LinphoneManager.instance lpConfigSetInt:0 forKey:@"must_link_account_time"];
    } else if (status == LinphoneAccountCreatorStatusAccountNotLinked) {
        [LinphoneManager.instance lpConfigSetInt:[NSDate new].timeIntervalSince1970 forKey:@"must_link_account_time"];
    } else {
        [thiz showErrorPopup:resp];
    }
}
 
#pragma mark - UITextFieldDelegate Functions
 
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    UIAssistantTextField *atf = (UIAssistantTextField *)textField;
    [atf textFieldDidBeginEditing:atf];
}
 
- (void)textFieldDidEndEditing:(UITextField *)textField {
    UIAssistantTextField *atf = (UIAssistantTextField *)textField;
    [atf textFieldDidEndEditing:atf];
    [self shouldEnableNextButton];
}
 
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    UIAssistantTextField *atf = (UIAssistantTextField *)textField;
    [textField resignFirstResponder];
    if (textField.returnKeyType == UIReturnKeyNext) {
        [atf.nextFieldResponder becomeFirstResponder];
    } else if (textField.returnKeyType == UIReturnKeyDone) {
        [[self findButton:ViewElement_NextButton] sendActionsForControlEvents:UIControlEventTouchUpInside];
    }
    return YES;
}
 
- (BOOL)textField:(UITextField *)textField
    shouldChangeCharactersInRange:(NSRange)range
                replacementString:(NSString *)string {
    if (textField.tag == ViewElement_SMSCode) {
        // max 4 length
        return range.location + range.length <= 4;
    } else {
        UIAssistantTextField *atf = (UIAssistantTextField *)textField;
        BOOL replace = YES;
        // if we are hitting backspace on secure entry, this will clear all text
        if ([string isEqual:@""] && textField.isSecureTextEntry) {
            range = NSMakeRange(0, atf.text.length);
        }
        [atf textField:atf shouldChangeCharactersInRange:range replacementString:string];
        if (atf.tag == ViewElement_Username && currentView == _createAccountView) {
            atf.text = [atf.text stringByReplacingCharactersInRange:range withString:string.lowercaseString];
            replace = NO;
        }
 
        if (textField.tag == ViewElement_Phone || textField.tag == ViewElement_Username) {
            [self refreshYourUsername];
        }
        [self shouldEnableNextButton];
        
        return replace;
    }
}
 
// Change button color and wait the display of this
#define ONCLICKBUTTON(button, timewaitmsec, body) \
[button setBackgroundColor:[UIColor lightGrayColor]]; \
    _waitView.hidden = NO; \
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (timewaitmsec * NSEC_PER_MSEC)); \
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ \
        body \
        [button setBackgroundColor:[UIColor clearColor]]; \
        _waitView.hidden = YES; \
    }); \
 
// Change button color and wait until object finished to avoid duplicated actions
#define ONNEWCLICKBUTTON(button, timewaitmsec, body) \
[button setBackgroundColor:[UIColor lightGrayColor]]; \
    _waitView.hidden = NO; \
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (timewaitmsec * NSEC_PER_MSEC)); \
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ \
        body \
        [button setBackgroundColor:[UIColor clearColor]]; \
    }); \
 
#pragma mark - Action Functions
 
- (IBAction)onGotoCreateAccountClick:(id)sender {
    ONCLICKBUTTON(sender, 100, {
        nextView = _createAccountView;
        _accountLabel.text = NSLocalizedString(@"Please enter your phone number", nil);
        [self loadAssistantConfig:@"assistant_linphone_create.rc"];
    });
}
 
- (IBAction)onGotoLinphoneLoginClick:(id)sender {
    ONCLICKBUTTON(sender, 100, {
        nextView = _linphoneLoginView;
        [self loadAssistantConfig:@"assistant_linphone_existing.rc"];
    });
}
 
- (IBAction)onGotoLoginClick:(id)sender {
    ONCLICKBUTTON(sender, 100, {
        nextView = _loginView;
        [self loadAssistantConfig:@"assistant_external_sip.rc"];
    });
}
 
- (IBAction)onGotoRemoteProvisioningClick:(id)sender {
    ONCLICKBUTTON(sender, 100, {
        nextView = _remoteProvisioningView;
        [self findTextField:ViewElement_URL].text =
        [LinphoneManager.instance lpConfigStringForKey:@"config-uri" inSection:@"misc"];
        [self loadAssistantConfig:@"assistant_remote.rc"];
    });
}
 
- (IBAction)onCreateAccountClick:(id)sender {
    if ([self checkFields]) {
        ONCLICKBUTTON(sender, 100, {
            _activationTitle.text = @"CREATE ACCOUNT";
            _waitView.hidden = NO;
            linphone_account_creator_is_account_exist(account_creator);
        });
    }
}
 
- (IBAction)onCreateAccountActivationClick:(id)sender {
    ONCLICKBUTTON(sender, 100, {
        _waitView.hidden = NO;
        linphone_account_creator_set_activation_code(
            account_creator,
            ((UITextField *)[self findView:ViewElement_SMSCode inView:_contentView ofType:UITextField.class])
                .text.UTF8String);
        if (linphone_account_creator_get_password(account_creator) == NULL &&
            linphone_account_creator_get_ha1(account_creator) == NULL) {
            if ([_activationTitle.text isEqualToString:@"USE LINPHONE ACCOUNT"]) {
                linphone_account_creator_login_linphone_account(account_creator);
            } else {
                linphone_account_creator_activate_account(account_creator);
            }
        } else {
            NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
            linphone_account_creator_set_language(account_creator, [[language substringToIndex:2] UTF8String]);
            linphone_account_creator_link_account(account_creator);
            linphone_account_creator_activate_alias(account_creator);
        }
    });
}
 
- (IBAction)onCreateAccountCheckActivatedClick:(id)sender {
    ONCLICKBUTTON(sender, 100, {
        _waitView.hidden = NO;
        linphone_account_creator_is_account_activated(account_creator);
    });
}
 
- (IBAction)onLinkAccountClick:(id)sender {
    ONCLICKBUTTON(sender, 100, {
        _waitView.hidden = NO;
        NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
        linphone_account_creator_set_language(account_creator, [[language substringToIndex:2] UTF8String]);
        linphone_account_creator_link_account(account_creator);
    });
}
 
- (IBAction)onLinphoneLoginClick:(id)sender {
    // disable button after first click
    _linphoneLoginButton.enabled = NO;
    [_linphoneLoginButton setBackgroundColor:[UIColor lightGrayColor]];
    _waitView.hidden = NO;
 
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (100 * NSEC_PER_MSEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        ((UITextField *)[self findView:ViewElement_SMSCode inView:_contentView ofType:UITextField.class]).text = @"";
        _activationTitle.text = @"USE LINPHONE ACCOUNT";
        if ((linphone_account_creator_get_phone_number(account_creator) != NULL) &&
            linphone_account_creator_get_password(account_creator) == NULL &&
            linphone_account_creator_get_ha1(account_creator) == NULL) {
            NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
            linphone_account_creator_set_language(account_creator, [[language substringToIndex:2] UTF8String]);
            linphone_account_creator_recover_account(account_creator);
        } else {
            // check if account is already linked with a phone number.
            // if not, propose it to the user
            linphone_account_creator_is_account_exist(account_creator);
        }
    });
}
 
//2021-07-28 登录接口
- (IBAction)onLoginClick:(id)sender {
    ONCLICKBUTTON(sender, 100, {
        _waitView.hidden = NO;
        NSString *domain = [self findTextField:ViewElement_Domain].text;
        NSString *username = [self findTextField:ViewElement_Username].text;
        NSString *displayName = [self findTextField:ViewElement_DisplayName].text;
        NSString *pwd = [self findTextField:ViewElement_Password].text;
        
        LinphoneAccountParams *accountParams =  linphone_core_create_account_params(LC);
        LinphoneAddress *addr = linphone_address_new(NULL);
        LinphoneAddress *tmpAddr = linphone_address_new([NSString stringWithFormat:@"sip:%@",domain].UTF8String);
        if (tmpAddr == nil) {
            [self displayAssistantConfigurationError];
            return;
        }
        
        linphone_address_set_username(addr, username.UTF8String);
        linphone_address_set_port(addr, linphone_address_get_port(tmpAddr));
        linphone_address_set_domain(addr, linphone_address_get_domain(tmpAddr));
        if (displayName && ![displayName isEqualToString:@""]) {
            linphone_address_set_display_name(addr, displayName.UTF8String);
        }
        
        linphone_account_params_set_identity_address(accountParams, addr);
        // set transport
        UISegmentedControl *transports = (UISegmentedControl *)[self findView:ViewElement_Transport
                                                                       inView:self.contentView
                                                                       ofType:UISegmentedControl.class];
        if (transports) {
            NSString *type = [transports titleForSegmentAtIndex:[transports selectedSegmentIndex]];
            LinphoneAddress *transportAddr = linphone_address_new([NSString stringWithFormat:@"sip:%s;transport=%s", domain.UTF8String, type.lowercaseString.UTF8String].UTF8String);
            linphone_account_params_set_routes_addresses(accountParams, bctbx_list_new(transportAddr));
            linphone_account_params_set_server_addr(accountParams, [NSString stringWithFormat:@"%s;transport=%s", domain.UTF8String, type.lowercaseString.UTF8String].UTF8String);
            
            linphone_address_unref(transportAddr);
        }
        linphone_account_params_set_publish_enabled(accountParams, FALSE);
        linphone_account_params_set_register_enabled(accountParams, TRUE);
        
        LinphoneAuthInfo *info =
            linphone_auth_info_new(linphone_address_get_username(addr), // username
                                   NULL,                                // user id
                                   pwd.UTF8String,                        // passwd
                                   NULL,                                // ha1
                                   linphone_address_get_domain(addr),   // realm - assumed to be domain
                                   linphone_address_get_domain(addr)    // domain
                                   );
        linphone_core_add_auth_info(LC, info);
        linphone_address_unref(addr);
        linphone_address_unref(tmpAddr);
        
        LinphoneAccount *account = linphone_core_create_account(LC, accountParams);
        linphone_account_params_unref(accountParams);
        if (account) {
            if (linphone_core_add_account(LC, account) != -1) {
                linphone_core_set_default_account(LC, account);
                // reload address book to prepend proxy config domain to contacts' phone number
                // todo: STOP doing that!
                [[LinphoneManager.instance fastAddressBook] fetchContactsInBackGroundThread];
                [PhoneMainView.instance changeCurrentView:DialerView.compositeViewDescription];
            } else {
              [self displayAssistantConfigurationError];
            }
        } else {
          [self displayAssistantConfigurationError];
        }
    });
}
 
- (IBAction)onRemoteProvisioningLoginClick:(id)sender {
    ONCLICKBUTTON(sender, 100, {
        _waitView.hidden = NO;
        [LinphoneManager.instance lpConfigSetInt:1 forKey:@"transient_provisioning" inSection:@"misc"];
        [self configureAccount];
    });
}
 
- (IBAction)onRemoteProvisioningDownloadClick:(id)sender {
    ONNEWCLICKBUTTON(sender, 100, {
        if (number_of_accounts_before > 0) {
            // TODO remove ME when it is fixed in SDK.
            linphone_core_set_provisioning_uri(LC, NULL);
            UIAlertController *errView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Provisioning Load error", nil)
                                                                             message:NSLocalizedString(@"Please remove other accounts before remote provisioning.", nil)
                                                                      preferredStyle:UIAlertControllerStyleAlert];
                
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK"
                                                                    style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {}];
            
            [errView addAction:defaultAction];
            [self presentViewController:errView animated:YES completion:nil];
        } else {
            linphone_core_set_provisioning_uri(LC,  [self addSchemeToProvisiionninUriIMissing:[self findTextField:ViewElement_URL].text].UTF8String);
            [self resetLiblinphone:TRUE];
        }
    });
}
 
- (IBAction)onLaunchQRCodeView:(id)sender {
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(qrCodeFound:)
                                               name:kLinphoneQRCodeFound
                                             object:nil];
    LinphoneAppDelegate *delegate = (LinphoneAppDelegate *)UIApplication.sharedApplication.delegate;
    delegate.onlyPortrait = TRUE;
    NSNumber *value = [NSNumber numberWithInt:UIDeviceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    //[UIViewController attemptRotationToDeviceOrientation];
    AVCaptureDevice *backCamera = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInWideAngleCamera mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack];
    if (![[NSString stringWithUTF8String:linphone_core_get_video_device(LC) ?: ""] containsString:[backCamera uniqueID]]) {
        
        bctbx_list_t *deviceList = linphone_core_get_video_devices_list(LC);
        NSMutableArray *devices = [NSMutableArray array];
        
        while (deviceList) {
            char *data = deviceList->data;
            if (data) [devices addObject:[NSString stringWithUTF8String:data]];
            deviceList = deviceList->next;
        }
        bctbx_list_free(deviceList);
        
        for (NSString *device in devices) {
            if ([device containsString:backCamera.uniqueID]) {
                linphone_core_set_video_device(LC, device.UTF8String);
            }
        }
    }
    
    
    linphone_core_set_native_preview_window_id(LC, (__bridge void *)(_qrCodeView));
    linphone_core_enable_video_preview(LC, TRUE);
    linphone_core_enable_qrcode_video_preview(LC, TRUE);
    
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(qrCodeFound:)
                                               name:kLinphoneQRCodeFound
                                             object:nil];
    
    [self changeView:_qrCodeView back:FALSE animation:TRUE];
}
 
- (void)refreshYourUsername {
    UIAssistantTextField *username = [self findTextField:ViewElement_Username];
    UIAssistantTextField *phone = [self findTextField:ViewElement_Phone];
    const char* uri = NULL;
    if (!username.superview.hidden && ![username.text isEqualToString:@""]) {
        uri = linphone_account_creator_get_username(account_creator);
    } else if (!phone.superview.hidden && ![phone.text isEqualToString:@""]) {
        uri = linphone_account_creator_get_phone_number(account_creator);
    }
 
    if (uri) {
        _accountLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Your SIP address will be sip:%s@sip.linphone.org", nil), uri];
    } else if (!username.superview.hidden) {
        _accountLabel.text = NSLocalizedString(@"Please enter your username", nil);
    } else {
        _accountLabel.text = NSLocalizedString(@"Please enter your phone number", nil);
    }
}
 
- (IBAction)onFormSwitchToggle:(UISwitch*)sender {
    UISwitch *usernameSwitch = (UISwitch *)[self findView:ViewElement_UsernameFormView inView:self.contentView ofType:UISwitch.class];
    UISwitch *emailSwitch = (UISwitch *)[self findView:ViewElement_EmailFormView inView:self.contentView ofType:UISwitch.class];
 
    UIView * usernameView = [self findView:ViewElement_UsernameFormView inView:self.contentView ofType:UIView.class];
    UIView * emailView = [self findView:ViewElement_EmailFormView inView:self.contentView ofType:UIView.class];
    usernameView.hidden = !usernameSwitch.isOn && !emailSwitch.isOn;
    emailView.hidden = !emailSwitch.isOn;
    
    [self findTextField:ViewElement_Phone].hidden = emailSwitch.isOn;
    [self findTextField:ViewElement_PhoneCC].hidden = emailSwitch.isOn;
    [self findButton:ViewElement_PhoneButton].hidden = emailSwitch.isOn;
    self.phoneLabel.hidden = emailSwitch.isOn;
    self.phoneTitle.hidden = emailSwitch.isOn;
    self.phoneTitle.text = NSLocalizedString(@"Please confirm your country code and enter your phone number", nil);
    self.infoLoginButton.hidden = !usernameView.hidden;
    if (!usernameView.hidden) {
        self.subtileLabel_useLinphoneAccount.text = NSLocalizedString(@"Please enter your username and password", nil);
    } else {
        self.subtileLabel_useLinphoneAccount.text = NSLocalizedString(@"Please confirm your country code and enter your phone number", nil);
    }
    
 
    UIAssistantTextField* countryCodeField = [self findTextField:ViewElement_PhoneCC];
    UIRoundBorderedButton *phoneButton = [self findButton:ViewElement_PhoneButton];
    usernameSwitch.enabled = phoneButton.enabled = countryCodeField.enabled = countryCodeField.userInteractionEnabled =
        [self findTextField:ViewElement_Phone].userInteractionEnabled = [self findTextField:ViewElement_Phone].enabled =
            !emailSwitch.isOn;
 
    [self refreshYourUsername];
 
    // put next button right after latest field (avoid blanks)
    int old = _createAccountNextButtonPositionConstraint.constant;
    _createAccountNextButtonPositionConstraint.constant = IPAD || !usernameView.hidden ? 21 : -10;
    if (!usernameView.hidden) {
        _createAccountNextButtonPositionConstraint.constant += usernameView.frame.size.height;
    }
    if (!emailView.hidden) {
        _createAccountNextButtonPositionConstraint.constant += emailView.frame.size.height;
    }
    // make view scrollable only if next button is too away
    CGRect viewframe = currentView.frame;
    viewframe.size.height = 30 + _createAccountNextButtonPositionConstraint.constant - old + [self findButton:ViewElement_NextButton].frame.origin.y + [self findButton:ViewElement_NextButton].frame.size.height;
    [_contentView setContentSize:viewframe.size];
    if (emailSwitch.isOn) {
        [self findButton:ViewElement_NextButton].enabled = TRUE;
    }
    [self shouldEnableNextButton];
}
 
- (IBAction)onCountryCodeClick:(id)sender {
    mustRestoreView = YES;
 
    CountryListView *view = VIEW(CountryListView);
    [view setDelegate:(id)self];
    [PhoneMainView.instance changeCurrentView:view.compositeViewDescription];
}
 
- (void)updateCountry:(BOOL)force {
    UIAssistantTextField* countryCodeField = [self findTextField:ViewElement_PhoneCC];
    NSDictionary *c = [CountryListView countryWithCountryCode:countryCodeField.text];
    if (c || force) {
        UIRoundBorderedButton *phoneButton = [self findButton:ViewElement_PhoneButton];
        [phoneButton setTitle:c ? [c objectForKey:@"name"] : NSLocalizedString(@"Unknown country code", nil)
                     forState:UIControlStateNormal];
    }
}
 
- (IBAction)onCountryCodeFieldChange:(id)sender {
    [self updateCountry:NO];
}
 
- (IBAction)onCountryCodeFieldEnd:(id)sender {
    [self updateCountry:YES];
}
 
- (IBAction)onPhoneNumberDisclosureClick:(id)sender {
    UIAlertController *errView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"What will my phone number be used for?", nil)
                                                                     message:NSLocalizedString(@"Your friends will find your more easily if you link your account to your "
                                                                                               @"phone number. \n\nYou will see in your address book who is using "
                                                                                               @"Linphone and your friends will know that they can reach you on Linphone "
                                                                                               @"as well.",
                                                                                               nil)
                                                              preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {}];
        
    [errView addAction:defaultAction];
    [self presentViewController:errView animated:YES completion:nil];
}
 
- (IBAction)onBackClick:(id)sender {
    if ([historyViews count] > 0) {
        if (currentView == _createAccountActivateSMSView || currentView == _createAccountActivateEmailView || currentView == _qrCodeView) {
            UIView *view = [historyViews lastObject];
            [historyViews removeLastObject];
            [self changeView:view back:TRUE animation:TRUE];
        } else if (currentView == _welcomeView) {
            [PhoneMainView.instance popCurrentView];
        } else {
            [self changeView:_welcomeView back:TRUE animation:TRUE];
        }
    } else {
        [self onDialerClick:nil];
    }
}
 
- (IBAction)onDialerClick:(id)sender {
        [PhoneMainView.instance popToView:DialerView.compositeViewDescription];
    }
 
- (IBAction)onLinkTap:(id)sender {
    NSString *url = @"https://subscribe.linphone.org";
    if (![UIApplication.sharedApplication openURL:[NSURL URLWithString:url]]) {
        LOGE(@"Failed to open %@, invalid URL", url);
    }
}
 
- (IBAction)onAcceptTermsClick:(id)sender {
    BOOL acceptTerms = [LinphoneManager.instance lpConfigBoolForKey:@"accept_terms" withDefault:FALSE];
    [LinphoneManager.instance lpConfigSetBool:!acceptTerms forKey:@"accept_terms"];
    [self enableWelcomeViewButtons];
}
 
#pragma mark - select country delegate
 
- (void)didSelectCountry:(NSDictionary *)country {
    UIRoundBorderedButton *phoneButton = [self findButton:ViewElement_PhoneButton];
    [phoneButton setTitle:[country objectForKey:@"name"] forState:UIControlStateNormal];
    UIAssistantTextField* countryCodeField = [self findTextField:ViewElement_PhoneCC];
    countryCodeField.text = countryCodeField.lastText = [country objectForKey:@"code"];
    phone_number_length = [[country objectForKey:@"phone_length  "] integerValue];
    [self shouldEnableNextButton];
}
 
-(void)qrCodeFound:(NSNotification *)notif {
    if ([notif.userInfo count] == 0){
        return;
    }
    [NSNotificationCenter.defaultCenter removeObserver:self name:kLinphoneQRCodeFound object:nil];
    dispatch_async(dispatch_get_main_queue(), ^{
        self.urlLabel.text = [notif.userInfo objectForKey:@"qrcode"];
 
        if ([historyViews count] > 0) {
            if (currentView == _qrCodeView) {
                UIView *view = [historyViews lastObject];
                [historyViews removeLastObject];
                [self changeView:view back:TRUE animation:TRUE];
            } else {
                [self changeView:_welcomeView back:TRUE animation:TRUE];
            }
        }
    });
}
 
@end