//
//  SLUserCenterViewController.m
//  SLAiELTS
//
//  Created by Gusont on 2023/3/9.
//

#import "SLUserCenterViewController.h"
#import "SLBaseTableViewCell.h"
#import "SLModifyNicknameVc.h"
#import "SLChangeMobileVc.h"
#import "SLImagePickerAndUpload.h"
#import "SLCitySetVc.h"
#import "SLMsgBgSetVc.h"

@interface SLUserCenterViewController ()<UITableViewDelegate, UITableViewDataSource>

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

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

@property (nonatomic, strong) SLImagePickerAndUpload *imageUpload;
@end

@implementation SLUserCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = self.isSetting ? NSLocalizedString(@"通用设置", nil) : NSLocalizedString(@"个人中心", 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.backgroundColor = [UIColor colorFormString:@"#F2F3F7"];
}

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

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    SLBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SLBaseTableViewCell"];
    SLPageModelDetail *detail = [[self.dataModels objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    [cell confignCell:detail];
    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(@"头像", nil)]) {
        [self.imageUpload willShowActionSheet];
    }
    if ([detail.title isEqualToString:NSLocalizedString(@"昵称", nil)]) {
        SLModifyNicknameVc *vc = [SLModifyNicknameVc loadViewControllewWithNib];
        [self navPushViewController:vc animated:YES];
    }
    if ([detail.title isEqualToString:NSLocalizedString(@"手机号", nil)]) {
        SLChangeMobileVc *vc = [SLChangeMobileVc loadViewControllewWithNib];
        [self navPushViewController:vc animated:YES];
    }
    if ([detail.title isEqualToString:NSLocalizedString(@"地区", nil)]) {
        SLCitySetVc *vc = [SLCitySetVc loadViewControllewWithNib];
        [self navPushViewController:vc animated:YES];
    }
    if ([detail.title isEqualToString:NSLocalizedString(@"退出登录", nil)]) {
        [SLCustomizeAlert showAletrWithTitle:NSLocalizedString(@"退出确认", nil) message:NSLocalizedString(@"是否确认退出登录？", nil) sureBtnTitle:NSLocalizedString(@"确定", nil) cancelBtnTitle:NSLocalizedString(@"取消", nil) sureBtnAction:^{
            [[SLGlobalInfo SharedInstance] userLogout];
            AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [appDelegate setLoginVcWithRootWindow];
        } cancelBtnAction:^{
            
        }];
    }
    if ([detail.title isEqualToString:NSLocalizedString(@"用户服务协议", nil)]) {
        NSString *urlStr = @"http://115.238.47.234:8446/index.html";
        SLWebViewController *vc = [SLWebViewController loadViewControllewWithNib];
        vc.webUrlStr = urlStr;
        vc.title = detail.title;
        [self navPushViewController:vc animated:YES];
    }
    if ([detail.title isEqualToString:NSLocalizedString(@"免责声明", nil)]) {
        NSString *urlStr = @"http://115.238.47.234:8446/mzsm.html";
        SLWebViewController *vc = [SLWebViewController loadViewControllewWithNib];
        vc.webUrlStr = urlStr;
        vc.title = detail.title;
        [self navPushViewController:vc animated:YES];
    }
    if ([detail.title isEqualToString:NSLocalizedString(@"清空聊天记录", nil)]) {
        [SLCustomizeAlert showAletrWithTitle:NSLocalizedString(@"温馨提示", nil) message:NSLocalizedString(@"是否清空聊天记录？", nil) sureBtnTitle:NSLocalizedString(@"确定", nil) cancelBtnTitle:NSLocalizedString(@"取消", nil) sureBtnAction:^{
            [[SLHttpCenter SharedInstance] getWithUrl:@"/api/Chat/ResetChatRecord" parameter:@{} success:^(id responseObject) {
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                NSDictionary *dic = [userDefaults dictionaryRepresentation];
                for (id  key in dic) {
                    if ([key isKindOfClass:NSString.class] && [key containsString:[NSString stringWithFormat:@"%@--to--",[SLGlobalInfo SharedInstance].loginInfo.user.userId]]) {
                        [userDefaults removeObjectForKey:key];
                    }
                }
                [userDefaults synchronize];
                //语音文件
                NSString *voicePath = [YMPath_DOCUMENT stringByAppendingFormat:@"/%@", @"voices"];
                [[NSFileManager defaultManager] removeItemAtPath:voicePath error:nil];
                [ZFToast ShowWithMessage:@"清空成功"];
            } failure:^(SPRequestError *error) {
                
            }];
        } cancelBtnAction:^{
            
        }];
    }
    if ([detail.title isEqualToString:NSLocalizedString(@"聊天背景", nil)]) {
        SLMsgBgSetVc *vc = [SLMsgBgSetVc loadViewControllewWithNib];
        [self navPushViewController:vc animated:YES];
    }
    if ([detail.title isEqualToString:NSLocalizedString(NSLocalizedString(@"语言设置", nil), nil)]) {
        SLMsgBgSetVc *vc = [SLMsgBgSetVc loadViewControllewWithNib];
        [self navPushViewController:vc animated:YES];
        NSArray *arr = @[
            @[[SLPageModelDetail initWithTitle:@"简体中文" subTitle:@"" bgImage:@""],
              [SLPageModelDetail initWithTitle:@"English" subTitle:@"" bgImage:@""]],
        ];
        vc.dataModels = [NSMutableArray arrayWithArray:arr];
        vc.title = NSLocalizedString(@"语言设置", nil);
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    SLPageModelDetail *detail = [[self.dataModels objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    if ([detail.title isEqualToString:NSLocalizedString(@"头像", nil)]) {
        return 68.0f;
    }
    return 50.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20.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, 20)];
    view.backgroundColor = [UIColor colorFormString:@"#F2F3F7"];
    return view;
}

- (NSMutableArray<NSArray<SLPageModelDetail *> *> *)dataModels {
    if (!_dataModels) {
        SLUserModel *user = [SLGlobalInfo SharedInstance].loginInfo.user;
        NSArray *arr = @[
        @[[SLPageModelDetail initWithTitle:NSLocalizedString(@"头像", nil) subTitle:@""],
          [SLPageModelDetail initWithTitle:NSLocalizedString(@"昵称", nil) subTitle:user.userName],
          [SLPageModelDetail initWithTitle:NSLocalizedString(@"手机号", nil) subTitle:user.phone],
          [SLPageModelDetail initWithTitle:@"DLid" subTitle:user.dlId canEdit:YES],
          [SLPageModelDetail initWithTitle:NSLocalizedString(@"性别", nil) subTitle:user.sex canEdit:YES],
          [SLPageModelDetail initWithTitle:NSLocalizedString(@"地区", nil) subTitle:[NSString stringWithFormat:@"%@%@",user.province,user.city]],
          [SLPageModelDetail initWithTitle:NSLocalizedString(@"星座", nil) subTitle:user.starSign canEdit:YES],],
          @[[SLPageModelDetail initWithTitle:NSLocalizedString(@"用户服务协议", nil) subTitle:@"" bgImage:@""],
            [SLPageModelDetail initWithTitle:NSLocalizedString(@"免责声明", nil) subTitle:@"" bgImage:@""]],
          @[[SLPageModelDetail initWithTitle:NSLocalizedString(@"退出登录", nil) subTitle:@"" bgImage:@""],]];
        if (self.isSetting) {
            arr = @[
                  @[[SLPageModelDetail initWithTitle:NSLocalizedString(NSLocalizedString(@"语言设置", nil), nil) subTitle:@"" bgImage:@""],
                    [SLPageModelDetail initWithTitle:NSLocalizedString(@"聊天背景", nil) subTitle:@"" bgImage:@""]],
                  @[[SLPageModelDetail initWithTitle:NSLocalizedString(@"清空聊天记录", nil) subTitle:@"" bgImage:@""],]];
        }
        _dataModels = [NSMutableArray arrayWithArray:arr];
    }
    return _dataModels;
}

- (SLImagePickerAndUpload *)imageUpload {
    if (!_imageUpload) {
        _imageUpload = [[SLImagePickerAndUpload alloc] init];
        _imageUpload.isUploadHeadImage = YES;
        WS(weakSelf);
        _imageUpload.uploadBlock = ^(NSString *imageUrl) {
            weakSelf.dataModels = nil;
            [weakSelf.tableView reloadData];
        };
    }
    return _imageUpload;
}

@end
