//
//  SLCitySetVc.m
//  SLAiELTS
//
//  Created by Gusont on 2023/6/1.
//

#import "SLCitySetVc.h"
#import <BRPickerView.h>
#import "SLBaseCellView.h"
#import "SLLocationManager.h"

@interface SLCitySetVc ()

@end

@implementation SLCitySetVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = NSLocalizedString(@"设置地区", nil);
    [self confignSexSetView];
}

- (void)confignSexSetView {
    NSArray *arr = @[[SLPageModelDetail initWithTitle:NSLocalizedString(@"定位到的位置", nil) bgImage:@""],
      [SLPageModelDetail initWithTitle:NSLocalizedString(@"选择地区", nil) bgImage:@""],];
    [arr enumerateObjectsUsingBlock:^(SLPageModelDetail * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        SLBaseCellView *baseCell = [[SLBaseCellView alloc] initWithFrame:CGRectZero];
        baseCell.titleLabel.text = obj.title;
        baseCell.subTitleLabel.text = @"";
        [baseCell updateConstraintsWith:SLBaseCellView1];
        baseCell.tag = 8888 + idx;
        WS(weakSelf);
        __block SLBaseCellView *blockBaseCell = baseCell;
        [baseCell addTapWithBlock:^{
            blockBaseCell.contentView.backgroundColor = SLColSel;
            [UIView animateWithDuration:0.5 animations:^{
                blockBaseCell.contentView.backgroundColor = [UIColor whiteColor];
            }];
            switch (idx) {
                case 0:
                {
                    [[SLLocationManager shareInstance] getLocationComplete:^(CLLocation * _Nullable currentLocation) {
                        if (currentLocation) {
                            SLLoginInfo *newLoginModel = [SLGlobalInfo SharedInstance].loginInfo;
                            newLoginModel.user.province = [SLLocationManager shareInstance].province;
                            newLoginModel.user.city = [SLLocationManager shareInstance].city;
                            [SLGlobalInfo SharedInstance].loginInfo = newLoginModel;
                            SLBaseCellView *baseCell = [weakSelf.view viewWithTag:8888];
                            baseCell.subTitleLabel.text = [NSString stringWithFormat:@"%@%@",[SLLocationManager shareInstance].province,[SLLocationManager shareInstance].city];
                            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                [weakSelf.navigationController popViewControllerAnimated:YES];
                            });
                        }else {
                            [ZFToast ShowWithMessage:@"定位失败，请检查权限设置"];
                        }
                    }];
                }
                    break;
                case 1:
                {
                    [weakSelf addressPickerViewShow];
                }
                    break;
                default:
                    break;
            }

        }];
        baseCell.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:baseCell];
        CGFloat top = 56 * idx * KScaleW + kNavBarHeight + 10;
        [baseCell mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.height.mas_equalTo(56 * KScaleW);
            make.top.equalTo(self.view).offset(top);
        }];
        
    }];
}


- (void)addressPickerViewShow {
    /// 地址选择器
    BRAddressPickerView *addressPickerView = [[BRAddressPickerView alloc]init];
    addressPickerView.pickerMode = BRAddressPickerModeCity;
    addressPickerView.title = @"请选择地区";
    //addressPickerView.selectValues = @[@"浙江省", @"杭州市", @"西湖区"];
//    addressPickerView.selectIndexs = @[@10, @0, @4];
//    addressPickerView.isAutoSelect = YES;
    WS(weakSelf);
    addressPickerView.resultBlock = ^(BRProvinceModel *province, BRCityModel *city, BRAreaModel *area) {
        [[SLHttpCenter SharedInstance] postWithUrl:@"/api/User/SetUserAddress" parameter:@{@"province" : province.name, @"city" : city.name ?: @""} success:^(id responseObject) {
            SLLoginInfo *newLoginModel = [SLGlobalInfo SharedInstance].loginInfo;
            newLoginModel.user.province = province.name;
            newLoginModel.user.city = city.name;
            [SLGlobalInfo SharedInstance].loginInfo = newLoginModel;
            SLBaseCellView *baseCell = [weakSelf.view viewWithTag:8888 + 1];
            baseCell.titleLabel.text = @"已选地区";
            baseCell.subTitleLabel.text = [NSString stringWithFormat:@"%@%@",province.name,city.name];
            [ZFToast ShowWithMessage:@"保存成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            });
        } failure:^(SPRequestError *error) {
            
        }];
    };
    [addressPickerView show];
}

@end
