//
//  SLAiSetVc.m
//  SLAiELTS
//
//  Created by Gusont on 2023/5/31.
//

#import "SLAiSetVc.h"
#import "SLBaseTableViewCell.h"
#import "SLMySpeedCell.h"
#import "SLModifyNicknameVc.h"
#import "SLNuisdkPlay.h"
#import "SLSexSetVc.h"

@interface SLAiSetVc ()<UITableViewDelegate, UITableViewDataSource,YMCustomSegmentDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray<NSArray<SLPageModelDetail *> *> *dataModels;

@property (nonatomic, strong)SLNuisdkPlay *play;

@end

@implementation SLAiSetVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = NSLocalizedString(@"AI设置", nil);
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tableView.estimatedRowHeight = 100;
    [self.tableView registerNibCellWithReuseIdentifierClass:SLBaseTableViewCell.class];
    [self.tableView registerNibCellWithReuseIdentifierClass:SLMySpeedCell.class];
    self.tableView.backgroundColor = [UIColor colorFormString:@"#F2F3F7"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_play stopNuisdkPlay];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear: animated];
    _dataModels = nil;
    [self.tableView reloadData];
}

- (void)dealloc {
    [_play stopNuisdkPlay];
    _play = nil;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UITableViewCell *cell;
    SLPageModelDetail *detail = [[self.dataModels objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    if (indexPath.section == 0) {
        SLBaseTableViewCell *baseCell = [tableView dequeueReusableCellWithIdentifier:@"SLBaseTableViewCell"];
        [baseCell confignCell:detail];
        baseCell.baseCellView.lineView.hidden = YES;
        cell = baseCell;
    }else {
        SLMySpeedCell *speedCell = [tableView dequeueReusableCellWithIdentifier:@"SLMySpeedCell"];
        speedCell.segmentView.delegate = nil;
        [speedCell confignCell:detail];
        speedCell.segmentView.delegate = self;
        cell = speedCell;
    }
    return cell;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataModels objectAtIndex:section].count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataModels.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SLPageModelDetail *detail = [[self.dataModels objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    if ([detail.title isEqualToString:NSLocalizedString(@"AI昵称", nil)]) {
        SLModifyNicknameVc *vc = [SLModifyNicknameVc loadViewControllewWithNib];
        vc.isAiNickName = YES;
        [self navPushViewController:vc animated:YES];
    }
    if ([detail.title isEqualToString:NSLocalizedString(@"性别", nil)]) {
        SLSexSetVc *vc = [SLSexSetVc loadViewControllewWithNib];
        [self navPushViewController:vc animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        return 82.f;
    }
    return 50.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 14.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 14)];
    view.backgroundColor = [UIColor colorFormString:@"#F2F3F7"];
    return view;
}

- (void)customSegmentSelectIndex:(NSInteger)selectIndex title:(NSString *)title {
    NSString *speechRate = [[RoleModel roleModelDict] objectForKey:title];
    if ([SLGlobalInfo SharedInstance].loginInfo && speechRate) {
        RoleModel *model = [RoleModel mj_objectWithKeyValues:[[SLGlobalInfo SharedInstance].roleModel mj_keyValues]];
        if ([title containsString:@"音"]) {
            model.language = speechRate;
        }else {
            model.speechRate = speechRate;
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.play startTTSWith:SLDefaultPlay roleModel:model finshBlock:^{

            }];
        });
        NSString *url = [title containsString:@"音"] ? @"/api/User/UpdateAILanguage" : @"/api/User/UpdateSpeechRate";
        NSDictionary *params = [title containsString:@"音"] ? @{@"language" : speechRate} : @{@"speechRate" : speechRate};
        [[SLHttpCenter SharedInstance] getWithUrl:url parameter:params success:^(id responseObject) {
            [SLGlobalInfo SharedInstance].roleModel = model;
        } failure:^(SPRequestError *error) {

        }];
    }
}

- (NSMutableArray<NSArray<SLPageModelDetail *> *> *)dataModels {
    if (!_dataModels) {
        RoleModel *user = [SLGlobalInfo SharedInstance].roleModel;
        NSArray *arr = @[
        @[[SLPageModelDetail initWithTitle:NSLocalizedString(@"AI昵称", nil) subTitle:user.aiName bgImage:@""],
          [SLPageModelDetail initWithTitle:NSLocalizedString(@"性别", nil) subTitle:user.aiSex bgImage:@""],],
          @[[SLPageModelDetail initWithTitle:NSLocalizedString(@"口音设置", nil) segments:@[NSLocalizedString(@"American Accent", nil), NSLocalizedString(@"British Accent", nil)]],
            [SLPageModelDetail initWithTitle:NSLocalizedString(@"难度设置", nil) segments:@[NSLocalizedString(@"简单", nil), NSLocalizedString(@"中等", nil), NSLocalizedString(@"困难", nil)]],
            [SLPageModelDetail initWithTitle:NSLocalizedString(@"语速设置", nil) segments:@[NSLocalizedString(@"slow", nil), NSLocalizedString(@"medium", nil), NSLocalizedString(@"fast", nil)]]],];
        _dataModels = [NSMutableArray arrayWithArray:arr];
    }
    return _dataModels;
}

- (SLNuisdkPlay *)play {
    if (!_play) {
        _play = [[SLNuisdkPlay alloc] init];
    }
    return _play;
}

@end
