//
//  SLMsgBgVc.m
//  SLAiELTS
//
//  Created by Gusont on 2023/6/2.
//

#import "SLMsgBgSetVc.h"
#import "SLBaseTableViewCell.h"
#import "SLImagePickerAndUpload.h"
#import "DAConfig.h"

@interface SLMsgBgSetVc ()

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

@property (nonatomic, strong) SLImagePickerAndUpload *imageUpload;

@end

@implementation SLMsgBgSetVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title =  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"];
}


- (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];
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            [self.imageUpload addPicwithCamera];
        }else if (indexPath.row == 1) {
            [self.imageUpload addPicWithPhotoLibrary];
        }
    }
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        return;
    }
    for (UITableViewCell *acell in tableView.visibleCells) {
        acell.accessoryType = acell == cell ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    }
    if (indexPath.row == 0) {
        [DAConfig setUserLanguage:@"zh-Hans"];
    } else if (indexPath.row == 1) {
        [DAConfig setUserLanguage:@"en"];
    } else {
        [DAConfig setUserLanguage:nil];

    }
    //更新当前storyboard
//    UITabBarController *tbc = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
//    tbc.selectedIndex = 2;
//    DALanguageSettingsViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([DALanguageSettingsViewController class])];
//    UINavigationController *nvc = tbc.selectedViewController;
//    NSMutableArray *vcs = nvc.viewControllers.mutableCopy;
//    [vcs addObject:vc];
    //解决奇怪的动画bug。异步执行
//    dispatch_async(dispatch_get_main_queue(), ^{
//        [UIApplication sharedApplication].keyWindow.rootViewController = tbc;
//        nvc.viewControllers = vcs;
//        NSLog(@"已切换到语言 %@", [NSBundle currentLanguage]);
//    });
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    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) {
        NSArray *arr = @[
            @[[SLPageModelDetail initWithTitle:NSLocalizedString(@"选择背景图", nil) subTitle:@""],],
            @[[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.isOriginImage = YES;
        WS(weakSelf);
        _imageUpload.uploadBlock = ^(NSString *imageUrl) {
            [[SLHttpCenter SharedInstance] getWithUrl:@"/api/User/SetFriendCircleImage" parameter:@{@"imageUrl" : imageUrl ?: @""} success:^(id responseObject) {
                SLLoginInfo *loginInfo = [SLGlobalInfo SharedInstance].loginInfo;
                loginInfo.user.friendCircleImage = imageUrl;
                [SLGlobalInfo SharedInstance].loginInfo = loginInfo;
                [weakSelf.navigationController popViewControllerAnimated:YES];
            } failure:^(SPRequestError *error) {
                
            }];
        };
    }
    return _imageUpload;
}


@end
