//
//  RDSSelectDeviceMainVC.m
//  Temperature
//
//  Created by 姓二名哈字富贵儿 on 2023/8/17.
//

#import "RDSSelectDeviceMainVC.h"
#import "RDSSelectDeviceSubVC.h"
#import <TYPagerController/TYPagerController.h>
#import <TYPagerController/TYTabPagerBar.h>
#import "RDSDeviceTypeModel.h"

@interface RDSSelectDeviceMainVC () <TYTabPagerBarDataSource, TYTabPagerBarDelegate, TYPagerControllerDelegate, TYPagerControllerDataSource>

@property (nonatomic, strong) RDSDeviceTypeModel *deviceTypeModel;

@property (nonatomic, strong) TYTabPagerBar *pagerBar;
@property (nonatomic, strong) TYPagerController *pagerController;

@end

@implementation RDSSelectDeviceMainVC

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

#pragma mark - Load Data
- (void)p_getDeviceTypes {
    [RDSHudShower showWithStatus:@"加载中..."];
    [self.deviceTypeModel rds_fetchDeviceTypeListFinished:^(NSError * _Nullable error) {
        [RDSHudShower dismissHUD];
        if (error) {
            [RDSHudShower showBottomToast:error.localizedDescription];
        } else {
            [self p_configSubviews];
        }
    }];
}

- (void)p_configSubviews {
    self.navigationItem.title = @"选择设备";
    
    self.pagerBar = [[TYTabPagerBar alloc] init];
    self.pagerBar.backgroundColor = [UIColor colorWithHexString:@"#FBFBFB"];
    self.pagerBar.layout.barStyle = TYPagerBarStyleProgressView;
    self.pagerBar.layout.normalTextColor = [UIColor colorWithHexString:@"#333333"];
    self.pagerBar.layout.selectedTextColor = [UIColor colorWithHexString:@"#14C9C8"];
    self.pagerBar.layout.normalTextFont = [UIFont pingFangSCMediumOfSize:15];
    self.pagerBar.layout.selectedTextFont = [UIFont pingFangSCMediumOfSize:15];
    self.pagerBar.layout.progressHeight = 2;
    self.pagerBar.layout.progressWidth = 40;
    self.pagerBar.layout.progressColor = [UIColor colorWithHexString:@"#14C9C8"];
    self.pagerBar.layout.cellSpacing = 0;
    self.pagerBar.layout.cellEdging = 0;
    self.pagerBar.layout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
    self.pagerBar.layout.adjustContentCellsCenter = NO;
    self.pagerBar.dataSource = self;
    self.pagerBar.delegate = self;
    [self.pagerBar registerClass:[TYTabPagerBarCell class] forCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier]];
    [self.view addSubview:self.pagerBar];
    [self.pagerBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        make.height.mas_equalTo(44);
    }];
    
    self.pagerController = [[TYPagerController alloc]init];
    self.pagerController.layout.addVisibleItemOnlyWhenScrollAnimatedEnd = YES;
    self.pagerController.automaticallySystemManagerViewAppearanceMethods = NO;
    self.pagerController.dataSource = self;
    self.pagerController.delegate = self;
    self.pagerController.view.backgroundColor = [UIColor colorWithHexString:@"#FBFBFB"];
    self.pagerController.scrollView.backgroundColor = [UIColor colorWithHexString:@"#FBFBFB"];
    self.pagerController.scrollView.bounces = NO;
    [self addChildViewController:self.pagerController];
    [self.view addSubview:self.pagerController.view];
    [self.pagerController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(self.pagerBar.mas_bottom);
        make.bottom.equalTo(self.view);
    }];
        
    [self.pagerBar reloadData];
    [self.pagerController reloadData];
}


#pragma mark - TYTabPagerBarDataSource
- (NSInteger)numberOfItemsInPagerTabBar {
    return self.deviceTypeModel.deviceTypeList.count;
}

- (UICollectionViewCell<TYTabPagerBarCellProtocol> *)pagerTabBar:(TYTabPagerBar *)pagerTabBar cellForItemAtIndex:(NSInteger)index {
    UICollectionViewCell<TYTabPagerBarCellProtocol> *cell = [pagerTabBar dequeueReusableCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier] forIndex:index];
    NSString *title = [self.deviceTypeModel getDeviceNameWithIndex:index];
    cell.titleLabel.text = title;
    return cell;
}

#pragma mark - TYTabPagerBarDelegate
- (CGFloat)pagerTabBar:(TYTabPagerBar *)pagerTabBar widthForItemAtIndex:(NSInteger)index {
    return 61;
}

- (void)pagerTabBar:(TYTabPagerBar *)pagerTabBar didSelectItemAtIndex:(NSInteger)index {
    [self.pagerController scrollToControllerAtIndex:index animate:YES];
}

#pragma mark - TYPagerControllerDataSource
- (NSInteger)numberOfControllersInPagerController {
    return self.deviceTypeModel.deviceTypeList.count;
}

- (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index prefetching:(BOOL)prefetching {
    RDSSelectDeviceSubVC *selectDeviceSubVC = [[RDSSelectDeviceSubVC alloc] init];
    RDSDeviceTypeModel *deviceTypeModel = self.deviceTypeModel.deviceTypeList[index];
    [selectDeviceSubVC reloadDataWithDataSource:deviceTypeModel.children];
    return selectDeviceSubVC;
}

#pragma mark - TYPagerControllerDelegate
- (void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex animated:(BOOL)animated {
    [self.pagerBar scrollToItemFromIndex:fromIndex toIndex:toIndex animate:animated];
}

- (void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex progress:(CGFloat)progress {
    [self.pagerBar scrollToItemFromIndex:fromIndex toIndex:toIndex progress:progress];
}

#pragma mark - Getter
- (RDSDeviceTypeModel *)deviceTypeModel {
    if (!_deviceTypeModel) {
        _deviceTypeModel = [[RDSDeviceTypeModel alloc] init];
    }
    return _deviceTypeModel;
}

@end
