//
//  XVPersonalViewController.m
//  fastservice
//
//  Created by xuning on 2/6/24.
//  Copyright © 2024 FastService. All rights reserved.
//

#import "XVPersonalViewController.h"
#import "XVPersonalHeaderView.h"
#import <SDCycleScrollView/SDCycleScrollView.h>
#import "XVVerticalButton.h"
#import "XVSettingViewController.h"
#import "XVMyViewController.h"
#import "HomeBannerModel.h"
#import "XVWebViewController.h"


@interface XVPersonalViewController ()<SDCycleScrollViewDelegate>

@property (nonatomic, strong) UIImageView *userImageView;

@property (nonatomic, strong) UILabel *nameLabel;

@property (nonatomic, strong) UIImageView *levelImageView;

@property (nonatomic, strong) UIButton *settingButton;


@property (nonatomic, strong) UILabel *phoneLabel;


@property (nonatomic, strong) UIButton *eyeButton;

// 健康证
@property (nonatomic, strong) UIButton *healthButton;

@property (nonatomic, strong) UIButton *idButton;

@property (nonatomic, strong) UIScrollView *scrollView;


@property (nonatomic, strong) XVPersonalHeaderView *headerView;

@property (nonatomic, strong)  SDCycleScrollView *cycleScrollView;

@property (nonatomic, strong) UIView *jobView;
@property (nonatomic, strong) UILabel *jobLabel;

@property (nonatomic, strong) NSArray *dataArray;


@property (nonatomic, strong) UIView *moreView;
@property (nonatomic, strong) UILabel *moreLabel;
@property (nonatomic, strong) NSArray *moreArray;


@property (nonatomic,assign) CGFloat w;

@property (nonatomic, strong) UIButton *serviceButton;

@property (nonatomic, strong) UserInfo *userInfo;

@property (nonatomic, strong) NSArray<HomeBannerModel *>* bannerList;


@end

@implementation XVPersonalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.w = SCREEN_WIDTH * 0.8 - 12;
    [self setupUI];
    
    [self loadData];
    [self loadMenuData];
}

- (void)loadData {
    IMP_BLOCK_SELF(XVPersonalViewController);
    [MyNetWork loginAfterLoadUserProfile:^(UserInfo *userInfo) {
        [block_self setupViews:userInfo];
    }];
}

//横幅banner
- (void)loadMenuData{
    NSMutableDictionary * pDic = [NSMutableDictionary dictionary];
    [pDic setObject:@([AppConfig getUserInfo].user_id) forKey: @"user_id"];
    [pDic setObject:@([AppConfig getGroupID]) forKey: @"group_id"];
    //参数 city 和 district
    pDic = [MyNetWork parameterShowCityAndDistrict:pDic];
    IMP_BLOCK_SELF(XVPersonalViewController);
    NSURLSessionTask * task = [[AFNetworkingClient sharedClient] runPost:SS_GET_SMENU parameters:pDic success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([MyNetWork isSuccessServerData:responseObject]) {
            NSArray<HomeBannerModel *>* bannerList = [HomeBannerModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"banner_data"]];
            
            self.bannerList = bannerList;
            
            NSMutableArray *imgGorup = [NSMutableArray arrayWithCapacity:bannerList.count];
            for (HomeBannerModel *model in bannerList) {
                [imgGorup addObject:model.banner_url];
            }
            block_self.cycleScrollView.imageURLStringsGroup = imgGorup;
            
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {

    }];
}


- (void)setupViews:(UserInfo *)userInfo {
    
    self.userInfo = userInfo;
    
    if (StringIsNullOrEmpty(userInfo.headPortrait)) {
        self.userImageView.image = userInfo.b.gender == 2 ? IMG(@"woman") : IMG(@"man");
    }else{
        self.userImageView.image = [UIImage imageNamed:userInfo.headPortrait];
    }
    
    self.nameLabel.text = userInfo.b.nickname;
    
    NSString *phone = self.userInfo.b.mobile;
    
    if (phone && phone.length > 7) {
        phone = [phone stringByReplacingCharactersInRange:NSMakeRange(phone.length -8, 4) withString:@"****"];//防止号码有前缀所以使用倒数第8位开始替换
    }
    self.phoneLabel.text = phone;


    // 实名认证状态
    int flag = userInfo.c.c_s;
    if (flag == 2 || flag == 3 || flag == 6) {
        self.healthButton.selected = YES;
    }else{
        self.healthButton.selected = NO;
    }
    // 服务保障状态
    self.idButton.selected = userInfo.c.g_s == 1;
    //0=铁牌骑士,1=铜牌骑士,2=银牌骑士,3=金牌骑士,4=钻石骑士
    switch (userInfo.c.l) {
        case 0:
            self.levelImageView.image = IMG(@"tiepai");
            break;
        case 1:
            self.levelImageView.image = IMG(@"tongpai");
            break;
        case 2:
            self.levelImageView.image = IMG(@"yinpai");
            break;
        case 3:
            self.levelImageView.image = IMG(@"jinpai");
            break;
        case 4:
            self.levelImageView.image = IMG(@"zuanshi");
            break;
            
        default:
            self.levelImageView.image = IMG(@"zhuansong");
            break;
    }
    
}

- (void)eyeButtonClick {
    self.eyeButton.selected = ! self.eyeButton.selected;
    if (self.eyeButton.selected) {
        self.phoneLabel.text = self.userInfo.b.mobile;
   
    }else{
        NSString *phone = self.userInfo.b.mobile;
        
        if (phone && phone.length > 7) {
            phone = [phone stringByReplacingCharactersInRange:NSMakeRange(phone.length -8, 4) withString:@"****"];//防止号码有前缀所以使用倒数第8位开始替换
        }
        self.phoneLabel.text = phone;
    }
}
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    
    HomeBannerModel *model = self.bannerList[index];
    
    XVWebViewController *web = [[XVWebViewController alloc]init];
    [web loadWebURLSring:model.obj_url];;
    web.titleName = model.title;
    [self cw_pushViewController:web];
}
- (void)setupUI {
    
    
    [self.view addSubview:self.userImageView];
    [self.userImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).mas_offset(KSTATUSBAR_HEIGHT + 15);
        make.left.mas_equalTo(self.view).mas_offset(13);
        make.size.mas_equalTo(CGSizeMake(66, 66));
    }];
    
    [self.view addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userImageView.mas_right).mas_offset(13);
        make.top.mas_equalTo(self.userImageView);
        make.width.mas_equalTo(70);
    }];
    
    [self.view addSubview:self.levelImageView];
    [self.levelImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.nameLabel);
        make.size.mas_equalTo(CGSizeMake(84, 24));
        make.left.mas_equalTo(self.nameLabel.mas_right).mas_offset(4);
    }];
    
    [self.view addSubview:self.settingButton];
    [self.settingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(36, 36));
        make.centerY.mas_equalTo(self.nameLabel);
        make.right.mas_equalTo(self.view).mas_offset(-15);
    }];
    
    self.settingButton.hitWidthScale = 2;
    
    [self.view addSubview:self.phoneLabel];
    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userImageView.mas_right).mas_offset(13);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).mas_offset(7);
    }];
    
    [self.view addSubview:self.eyeButton];
    [self.eyeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(17, 17));
        make.centerY.mas_equalTo(self.phoneLabel);
        make.left.mas_equalTo(self.phoneLabel.mas_right).mas_offset(4);
    }];
    
    [self.view addSubview:self.healthButton];
    [self.healthButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.centerY.mas_equalTo(self.phoneLabel);
        make.left.mas_equalTo(self.eyeButton.mas_right).mas_offset(4);
    }];

    
    [self.view addSubview:self.idButton];
    [self.idButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.centerY.mas_equalTo(self.phoneLabel);
        make.left.mas_equalTo(self.healthButton.mas_right).mas_offset(4);
    }];

    [self.view addSubview:self.serviceButton];
    [self.serviceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view).mas_offset(13);
        make.right.mas_equalTo(self.view).mas_offset(-13);
        make.height.mas_equalTo(44);
        if (@available(iOS 11.0, *)) {
            make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom).mas_offset(-18);
        } else {
            make.bottom.mas_equalTo(self.view).mas_offset(-18);
        }

    }];
    kViewBorderRadius(self.serviceButton, 4, 0, [UIColor whiteColor]);
    
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.userImageView.mas_bottom).mas_offset(19);
        make.bottom.mas_equalTo(self.serviceButton.mas_top).mas_offset(-5);
    }];
    
    [self.scrollView addSubview:self.headerView];

 
    kViewBorderRadius(self.headerView, 12, 0, [UIColor whiteColor]);
    
    [self.scrollView addSubview:self.cycleScrollView];
    kViewBorderRadius(self.cycleScrollView, 12, 0, [UIColor whiteColor]);
    
    
    [self.scrollView addSubview:self.jobView];
    [self.jobView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view).mas_offset(6);
        make.right.mas_equalTo(self.view).mas_offset(-6);
        make.top.mas_equalTo(self.cycleScrollView.mas_bottom).mas_offset(9);
        make.height.mas_equalTo(190);
    }];
    kViewBorderRadius(self.jobView, 12, 0, [UIColor whiteColor]);
    
    [self.jobView addSubview:self.jobLabel];
    self.jobLabel.frame = CGRectMake(7, 14, 70, 17);
    
    [self.scrollView addSubview:self.moreView];
    [self.moreView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view).mas_offset(6);
        make.right.mas_equalTo(self.view).mas_offset(-6);
        make.top.mas_equalTo(self.jobView.mas_bottom).mas_offset(9);
        make.height.mas_equalTo(114);
        make.bottom.mas_equalTo(self.scrollView);
    }];
    kViewBorderRadius(self.moreView, 12, 0, [UIColor whiteColor]);

    [self.moreView addSubview:self.moreLabel];
    self.moreLabel.frame = CGRectMake(7, 14, 70, 17);
    
    
    for (int i=0; i<self.dataArray.count; i++) {
        
        NSArray *array = self.dataArray[i];
        
        for (int j = 0; j< array.count; j++) {
            CGFloat x =  self.w / 4 * j;
            CGFloat y = i * 76 + (14+17);
            
            NSDictionary *dict = array[j];
            
            XVVerticalButton *button = [[XVVerticalButton alloc]initWithFrame:CGRectMake(x, y, self.w / 4, 76)];
            button.imageIsRound = NO;
            button.imageViewMaxSize = CGSizeMake(24, 24);
            button.imageTextSpace = 11;
            [button setImage:IMG(dict[@"imageName"]) forState:(UIControlStateNormal)];
            [button setTitle:dict[@"title"] forState:UIControlStateNormal];
            [button setTitleColor:F_606060_COLOR forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:12];
            
            [self.jobView addSubview:button];
        }
    }
    
    for (int i=0; i<self.moreArray.count; i++) {
        
        NSArray *array = self.dataArray[i];
        
        for (int j = 0; j< array.count; j++) {
            CGFloat x =  self.w / 4 * j;
            CGFloat y = i * 76 + (14+17);
            
            NSDictionary *dict = array[j];
            
            XVVerticalButton *button = [[XVVerticalButton alloc]initWithFrame:CGRectMake(x, y, self.w / 4, 76)];
            button.imageIsRound = NO;
            button.imageViewMaxSize = CGSizeMake(24, 24);
            button.imageTextSpace = 11;
            [button setImage:IMG(dict[@"imageName"]) forState:(UIControlStateNormal)];
            [button setTitle:dict[@"title"] forState:UIControlStateNormal];
            [button setTitleColor:F_606060_COLOR forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:12];
            
            [self.moreView addSubview:button];
        }
    }
    
}




- (BOOL)fd_prefersNavigationBarHidden {
    return NO;
}
- (UIImageView *)userImageView {
    if(_userImageView == nil) {
        _userImageView = [[UIImageView alloc]initWithImageName:@"qishi-head"];
    }
    return _userImageView;
}

- (UILabel *)nameLabel {
    if(_nameLabel == nil) {
        _nameLabel = [[UILabel alloc]initWithText:@"骑士" textColor:F_101010_COLOR textAlignment:(NSTextAlignmentLeft) fontSize:24];
    }
    return _nameLabel;
}

- (UILabel *)phoneLabel {
    if(_phoneLabel == nil) {
        _phoneLabel = [[UILabel alloc]initWithText:@"138****0018" textColor:F_101010_COLOR textAlignment:(NSTextAlignmentLeft) fontSize:20];
    }
    return _phoneLabel;
}

- (UIButton *)eyeButton {
    if (_eyeButton == nil) {
        _eyeButton = [[UIButton alloc]initWithNormalImageName:@"closeeyes" selectedImgName:@"openeyes"];
        [_eyeButton addTarget:self action:@selector(eyeButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _eyeButton;
}

- (UIButton *)healthButton {
    if (_healthButton == nil) {
        _healthButton =  [[UIButton alloc]initWithNormalImageName:@"fa5-address-card-fas 1 Copy 1" selectedImgName:@"fa5-address-card-fas 1"];
    }
    return _healthButton;
}

- (UIButton *)idButton {
    if (_idButton == nil) {
        _idButton =  [[UIButton alloc]initWithNormalImageName:@"riFill-shield-user-fill 1 Copy 1" selectedImgName:@"riFill-shield-user-fill 1"];
    }
    return _idButton;
}


- (UIImageView *)levelImageView {
    if (_levelImageView == nil) {
        _levelImageView = [[UIImageView alloc]initWithImageName:@"tongpai3x"];
        _levelImageView.userInteractionEnabled = YES;
        WS(weakSelf)
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
            XVMyViewController *myVc = [[XVMyViewController alloc]init];
            [weakSelf cw_pushViewController:myVc];
        }];
        [_levelImageView addGestureRecognizer:tap];
    }
    return _levelImageView;
}

- (UIButton *)settingButton {
    if (_settingButton == nil) {
        _settingButton = [[UIButton alloc]initWithNormalImageName:@"riLine-settings-line 1" selectedImgName:@"riLine-settings-line 1"];
        WS(weakSelf)
        [_settingButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
            XVSettingViewController *settingVc = [[XVSettingViewController alloc]init];
            [weakSelf cw_pushViewController:settingVc];
        }];
    }
    
    return _settingButton;
}
- (UIScrollView *)scrollView {
    if(_scrollView == nil) {
        _scrollView = [[UIScrollView alloc]init];
        _scrollView.backgroundColor = DEFAULT_BACKGROUND_COLOR;
        _scrollView.alwaysBounceVertical = YES;
    }
    return _scrollView;
}


- (XVPersonalHeaderView *)headerView {
    if(_headerView == nil) {
        _headerView = [[XVPersonalHeaderView alloc]initWithFrame:CGRectMake(6, 0, self.w, 99)];
        _headerView.backgroundColor = [UIColor whiteColor];
    }
    return _headerView;
}

- (SDCycleScrollView *)cycleScrollView {
    if (_cycleScrollView == nil) {
        _cycleScrollView = [[SDCycleScrollView alloc]initWithFrame:CGRectMake(6, 99+9, self.w, 74)];
        _cycleScrollView.delegate = self;
    }
    return _cycleScrollView;
}

- (UIView *)jobView {
    if (_jobView == nil) {
        _jobView = [[UIView alloc]init];
        _jobView.backgroundColor = [UIColor whiteColor];
    }
    return _jobView;
}

- (UILabel *)jobLabel {
    if (_jobLabel == nil) {
        _jobLabel = [[UILabel alloc]initWithText:@"工作必备" textColor:F_101010_COLOR textAlignment:(NSTextAlignmentLeft) fontSize:12];
    }
    return _jobLabel;
}

- (UIView *)moreView {
    if (_moreView == nil) {
        _moreView = [[UIView alloc]init];
        _moreView.backgroundColor = [UIColor whiteColor];
    }
    return _moreView;
}

- (UILabel *)moreLabel {
    if (_moreLabel == nil) {
        _moreLabel = [[UILabel alloc]initWithText:@"更多功能" textColor:F_101010_COLOR textAlignment:(NSTextAlignmentLeft) fontSize:12];
    }
    return _moreLabel;
}


- (NSArray *)dataArray {
    if(_dataArray == nil) {
        _dataArray = @[
        @[@{@"title":@"我的保障",@"imageName":@"antOutline-insurance J9bTxqJ Copy 1",@"className":@""},
                         @{@"title":@"服务宝典",@"imageName":@"antOutline-read wT8SVT4",@"className":@""},
                         @{@"title":@"接单排行",@"imageName":@"iconPark-ranking m7fyuQM",@"className":@""},
                         @{@"title":@"福利任务",@"imageName":@"iconPark-mark QpyYz9F",@"className":@""},],
                    @[
                        @{@"title":@"奖惩规则",@"imageName":@"iconPark-editor 1",@"className":@""},
                        @{@"title":@"常见问题",@"imageName":@"stLine-write-l 1",@"className":@""},
                        @{@"title":@"培训教程",@"imageName":@"antOutline-usergroup-delete 1",@"className":@""},
                        @{@"title":@"邀请有礼",@"imageName":@"antOutline-user-add 1",@"className":@""}]
        ];
    }
    return _dataArray;
}

- (NSArray *)moreArray {
    if (_moreArray == nil) {
        _moreArray = @[
                    @[@{@"title":@"推送测试",@"imageName":@"ceshi",@"className":@""},
                         @{@"title":@"录单",@"imageName":@"图标 19",@"className":@""},
                         @{@"title":@"服务评价",@"imageName":@"riLine-star-line 1",@"className":@""},
                    ]
        ];
    }
    return _moreArray;
}

- (UIButton *)serviceButton {
    if (_serviceButton == nil) {
        _serviceButton = [[UIButton alloc]initWithTitle:@"联系客服" fontSize:14 titleColor:[UIColor whiteColor] bgColor:kHexRGB(0xA4ADB3)];
    }
    return _serviceButton;
}
@end
