//
//  ChatSettingController.m
//  AIIM
//
//  Created by qitewei on 2025/5/22.
//

#import "ChatSettingController.h"
#import "ChatSettingCell.h"

#import "UDManager.h"
#import "UserNetApi.h"

@interface ChatSettingController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) UIView * borderView;
@property (nonatomic, strong) UIButton * saveBtn;

@property (nonatomic, strong) NSMutableDictionary * settingDict;
@property (nonatomic, strong) NSMutableArray * titlesArray;
@end

@implementation ChatSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getChatSettingData];
    [self configUI];
}

- (void)viewWillAppear:(BOOL)animated{
    [self setNavigationBarHidden:NO animated:NO];
}

- (void)configUI{
    [self setNavigationTitle:NSLocalizedString(@"userCenter-liaotsz", @"")];
    // 设置导航栏背景色
    [self setNavigationBarBackgroundColor:UIColor.clearColor];
    
    // 设置标题颜色和字体
    [self setNavigationTitleColor:[UIColor whiteColor] font:SYSBFONT(18)];
    
    // 设置返回按钮
    [self setBackButtonTitle:@""];
    [self setBackButtonColor:[UIColor whiteColor]];
    
    UIImageView * bgImageView = [[UIImageView alloc] initWithImage:kImageMake(@"loginBG")];
    [self.view addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(SCREEN_TOP+20);
        make.height.mas_equalTo(350);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];
    
    [self.view addSubview:self.saveBtn];
    [self.saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(self.tableView.mas_bottom).offset(20);
        make.height.mas_equalTo(46);
    }];
}

#pragma mark api
- (void)getChatSettingData{
    NSDictionary * userInfo = [UDManager.shareInstance getDDManager:dkuserinfo];
    [UserNetApi getChatSetting:userInfo[@"id"] succ:^(int code, NSDictionary * _Nullable result) {
        NSLog(@"chat setting:%@",result);
        if ([result jk_hasKey:@"data"]) {
            self.settingDict = [NSMutableDictionary dictionaryWithDictionary:result[@"data"]];
            [self.tableView reloadData];
        }else{
            self.settingDict = [NSMutableDictionary  dictionaryWithDictionary:@{
                @"canAddFriend":@"1",
                @"addFriendValidate":@"1",
                @"canSendMessage":@"1",
                @"canSoundRemind":@"1",
                @"canVoiceRemind":@"1",
                @"showMobile":@"1",
                @"showEmail":@"1",
                @"createBy":@""
            }];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)updateChatSetting:(NSDictionary *)param{
    [UserNetApi updateChatSetting:param succ:^(int code, NSDictionary * _Nullable result) {
        if ([result jk_hasKey:@"msg"]) {
            [MBProgressHUD showWithText:result[@"msg"]];
        }
    } fail:^(NSError * _Nonnull error) {
//        [MBProgressHUD showWithText:@"操作失败"];
    }];
}

#pragma mark event
- (void)saveButtonClicked{
    [self updateChatSetting:self.settingDict];
}

#pragma mark tableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titlesArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChatSettingCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(ChatSettingCell.class) forIndexPath:indexPath];
    cell.titleLbl.text = self.titlesArray[indexPath.row];
    
    switch (indexPath.row) {
        case 0:
            cell.stateSwitch.on = [self.settingDict[@"canAddFriend"] isEqualToString:@"1"];
            break;
        case 1:
            cell.stateSwitch.on = [self.settingDict[@"addFriendValidate"] isEqualToString:@"1"];
            break;
        case 2:
            cell.stateSwitch.on = [self.settingDict[@"canSendMessage"] isEqualToString:@"1"];
            break;
        case 3:
            cell.stateSwitch.on = [self.settingDict[@"canSoundRemind"] isEqualToString:@"1"];
            break;
        case 4:
            cell.stateSwitch.on = [self.settingDict[@"canVoiceRemind"] isEqualToString:@"1"];
            break;
        case 5:
            cell.stateSwitch.on = [self.settingDict[@"showMobile"] isEqualToString:@"1"];
            break;
        case 6:
            cell.stateSwitch.on = [self.settingDict[@"showEmail"] isEqualToString:@"1"];
            break;
        default:
            break;
    }
    
    weakSelf(self);
    cell.stateSwitchChangeValue = ^(BOOL isOn) {
        NSString * keyString = @"";
        switch (indexPath.row) {
            case 0:
                keyString = @"canAddFriend";
                break;
            case 1:
                keyString = @"addFriendValidate";
                break;
            case 2:
                keyString = @"canSendMessage";
                break;
            case 3:
                keyString = @"canSoundRemind";
                break;
            case 4:
                keyString = @"canVoiceRemind";
                break;
            case 5:
                keyString = @"showMobile";
                break;
            case 6:
                keyString = @"showEmail";
                break;
                
            default:
                break;
        }
        
        [weakself.settingDict setValue:isOn?@"1":@"0" forKey:keyString];
    };
    
    return cell;
}

#pragma mark lazy
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource =self;
        _tableView.backgroundColor = UIColor.clearColor;
        _tableView.separatorColor = globalColor(GCTypeGreen);
        _tableView.layer.cornerRadius = 5.f;
        _tableView.layer.borderColor = globalColor(GCTypeGreen).CGColor;
        _tableView.layer.borderWidth = 0.5f;
        _tableView.scrollEnabled = NO;
        [_tableView registerClass:ChatSettingCell.class forCellReuseIdentifier:NSStringFromClass(ChatSettingCell.class)];
    }
    return _tableView;
}

- (UIButton *)saveBtn{
    if (!_saveBtn) {
        _saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_saveBtn setTitle:NSLocalizedString(@"EGroupCtr-baocun", @"") forState:UIControlStateNormal];
        [_saveBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        _saveBtn.titleLabel.font = SYSMFONT(16);
        [_saveBtn setBackgroundColor:globalColor(GCTypeGreen)];
        _saveBtn.layer.cornerRadius = 5.f;
        _saveBtn.layer.masksToBounds = YES;
        [_saveBtn addTarget:self action:@selector(saveButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}

- (NSMutableArray *)titlesArray{
    if (!_titlesArray) {
        _titlesArray = [NSMutableArray arrayWithArray:@[NSLocalizedString(@"ChatSetting_addFriend", @""),NSLocalizedString(@"ChatSetting_review", @""),NSLocalizedString(@"ChatSetting_privateChat", @""),NSLocalizedString(@"ChatSetting_audioRemind", @""),NSLocalizedString(@"ChatSetting_vedioRemind", @""),NSLocalizedString(@"ChatSetting_mobileDisplay", @""),NSLocalizedString(@"ChatSetting_emialDisplay", @"")]];
    }
    return _titlesArray;
}

#pragma mark statusBar
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

@end
