//
//  SLTargetViewController.m
//  SLAiELTS
//
//  Created by Gusont on 2023/3/14.
//

#import "SLTargetViewController.h"
#import "SLBaseTableViewCell.h"
#import "SLDatePickView.h"
#import "PGDatePickManager.h"
#import "SLPickView.h"
#import "SLTargetModel.h"

@interface SLTargetViewController ()<UITableViewDelegate, UITableViewDataSource>


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

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

@property (nonatomic, strong) SLTargetModel *targetModel;

@end

@implementation SLTargetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"考试目标";
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 100;
    [self.tableView registerNibCellWithReuseIdentifierClass:SLBaseTableViewCell.class];
    self.tableView.backgroundColor = [UIColor colorFormString:@"#F2F3F7"];
    YMBarButtonItem *rightBarItem = [[YMBarButtonItem alloc] initWithTitle:@"保存  " target:self action:@selector(preserveBtnClick)];
    rightBarItem.titleInsets = UIEdgeInsetsMake(0, 0, 0, 20);
    self.navigationBar.rightBarButtonItem = rightBarItem;
}

- (void)preserveBtnClick {
    if (!self.targetModel.goalFraction) {
        [ZFToast ShowWithMessage:@"请选择目标分数"];
        return;
    }
    if (!self.targetModel.goalTime) {
        [ZFToast ShowWithMessage:@"请选择计划考试时间"];
        return;
    }
    if (!self.targetModel.examGoal) {
        [ZFToast ShowWithMessage:@"请选择考试目的"];
        return;
    }
    if (!self.targetModel.isIelts) {
        [ZFToast ShowWithMessage:@"请选择是否考过雅思"];
        return;
    }
    NSMutableDictionary *mDict = [self.targetModel mj_keyValues];
    [[SLHttpCenter SharedInstance] postWithUrl:@"/api/User/OperateUserGoal" parameter:mDict success:^(id responseObject) {
        [ZFToast ShowWithMessage:@"保存成功"];
        [SLGlobalInfo SharedInstance].targetModel = self.targetModel;
    } failure:^(SPRequestError *error) {
        
    }];
}

- (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 {
    __block SLPageModelDetail *detail = [[self.dataModels objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    WS(weakSelf);
    if ([detail.title isEqualToString:@"计划考试时间"]) {
        SLDatePickerDO *itemDo = [[SLDatePickerDO alloc] init];
        itemDo.format = @"yyyy年MM月dd日";
        itemDo.minDateString = [NSString getCurrentDayWithFormatter:itemDo.format timeDiff:24 * 3600];
        SLDatePickView *datePick = [[SLDatePickView alloc] initWithStyle:SLDatePickerYMD pickerData:itemDo];
        datePick.resultBlock = ^(NSString * _Nullable str) {
            detail.subTitle = str;
            weakSelf.targetModel.goalTime = str;
            [weakSelf.tableView reloadData];
        };
        [datePick setTitleLabelText:detail.title];
        [datePick show];
    }else {
        NSMutableArray<NSArray *> *dataArr = [NSMutableArray array];
        NSArray *titles;
        if ([detail.title isEqualToString:@"考试目的"]) {
            [dataArr addObject:@[@"出国留学",@"移民或工作",@"出国旅游",@"其他"]];
        }else if ([detail.title isEqualToString:@"是否考过雅思"]) {
            [dataArr addObject:@[@"是",@"否"]];
        }else if ([detail.title isEqualToString:@"目标分数"]) {
            titles = @[@"听力",@"阅读",@"口语",@"写作"];
            for (NSInteger i = 0; i < titles.count; i ++) {
                NSMutableArray *arr = [NSMutableArray array];
                for (NSInteger i = 2; i <= 20; i++) {
                    if (i % 2 == 0) {
                        [arr addObject:[NSString stringWithFormat:@"%.0f",i / 2.0]];
                    }else {
                        [arr addObject:[NSString stringWithFormat:@"%.1f",i / 2.0]];
                    }
                }
                [dataArr addObject:arr];
            }
        }
        SLPickView *pickView = [[SLPickView alloc] initWithPickerData:dataArr];
        pickView.resultBlock = ^(NSString * _Nullable str, NSArray *titlesValue) {
            detail.subTitle = str;
            if ([detail.title isEqualToString:@"考试目的"]) {
                weakSelf.targetModel.examGoal = str;
            }else if ([detail.title isEqualToString:@"是否考过雅思"]) {
                weakSelf.targetModel.isIelts = [str isEqualToString:@"是"];
            }else if ([detail.title isEqualToString:@"目标分数"]) {
                detail.subTitle = [NSString stringWithFormat:@"%@分",str];
                self.targetModel.goalFraction = str;
                self.targetModel.hearing = [titlesValue objectAtIndex:0];
                self.targetModel.read = [titlesValue objectAtIndex:1];
                self.targetModel.speech = [titlesValue objectAtIndex:2];
                self.targetModel.writing = [titlesValue objectAtIndex:3];
            }
            [weakSelf.tableView reloadData];
        };
        if (titles.count) {
            [pickView setComponentTitles:titles];
        }
        [pickView setTitleLabelText:detail.title];
        [pickView show];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50.f;
}

- (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 = SLBgColor;
    return view;
}

- (NSMutableArray<NSArray<SLPageModelDetail *> *> *)dataModels {
    if (!_dataModels) {
        SLTargetModel *targetModel = [SLGlobalInfo SharedInstance].targetModel;
        NSString *isIelts = targetModel ? (targetModel.isIelts ? @"是" : @"否") : @"";
        NSString *goalFraction = targetModel.goalFraction ? [NSString stringWithFormat:@"%@分",targetModel.goalFraction] : @"";
        NSArray *arr = @[
        @[[SLPageModelDetail initWithTitle:@"目标分数" subTitle:goalFraction bgImage:@""],
            
          [SLPageModelDetail initWithTitle:@"计划考试时间" subTitle:targetModel.goalTime bgImage:@""],
            
          [SLPageModelDetail initWithTitle:@"考试目的" subTitle:targetModel.examGoal bgImage:@""],
          [SLPageModelDetail initWithTitle:@"是否考过雅思" subTitle:isIelts bgImage:@""]]];
        _dataModels = [NSMutableArray arrayWithArray:arr];
    }
    return _dataModels;
}

- (SLTargetModel *)targetModel {
    if (!_targetModel) {
        _targetModel = [[SLTargetModel alloc] init];
        if ([SLGlobalInfo SharedInstance].targetModel) {
            _targetModel = [SLGlobalInfo SharedInstance].targetModel;
        }
    }
    return _targetModel;
}
@end
