//
//  SearchDeviceViewController.m
//  Unity-iPhone
//
//  Created by leon on 2021/2/4.
//

#import "SearchDeviceViewController.h"

#import "ShoesCarouselView.h"

@interface SearchDeviceViewController ()<ConnectDeviceDelegate>
//
@property (strong, nonatomic)ShoesCarouselView * shoesCarouselView;
//button
@property(nonatomic,strong)UIButton * nextButton;//
@property(nonatomic,strong)UIButton * lastButton;//
@property(nonatomic,strong)UIButton * cancelButton;//
@property(nonatomic,strong)UIButton * connectButton;//
@property (nonatomic,strong)UIButton * clearnButton;//清理缓存

//留白
@property(nonatomic,strong)NoDeviceTip * tipView;

@end

@implementation SearchDeviceViewController

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

#pragma mark -- create UI
-(void)initUI{
    
    //创建需要的毛玻璃特效类型
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    //创建毛玻璃视图
    UIVisualEffectView *visualV = [[UIVisualEffectView alloc] initWithEffect:blur];
    visualV.frame = self.view.bounds;
    //可以通过透明度来达到模糊程序的改变.
    visualV.effect = nil;
    visualV.backgroundColor = [UIColor blackColor];
    visualV.alpha = 0.5;
    [UIView animateWithDuration:1 animations:^{
        visualV.effect = blur;
    }];
    [self.view addSubview:visualV];
    
    //无设备
    [self.view addSubview:self.tipView];
        
    //设备列表
    self.shoesCarouselView  = [[ShoesCarouselView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-SCALEoefficient(200), SCREEN_HEIGHT-SCALEoefficient(100))];
    self.shoesCarouselView.centerX = self.view.centerX;
    self.shoesCarouselView.centerY = self.view.centerY-20;
    [self.view addSubview:self.shoesCarouselView];
    self.shoesCarouselView.delegate = self;
    
    //上一个
    self.nextButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.nextButton.frame = CGRectMake(0, 0, SCALEoefficient(16), SCALEoefficient(32));
    self.nextButton.centerY = self.shoesCarouselView.centerY;
    self.nextButton.right = self.shoesCarouselView.left+10;
    [self.nextButton setBackgroundImage:[MYFactoryManager imageString:@"gamepop_leftarrow"] forState:UIControlStateNormal];
    [self.view addSubview:self.nextButton];
    [self.nextButton addTarget:self action:@selector(lastAction) forControlEvents:UIControlEventTouchUpInside];
    self.nextButton.backgroundColor = [UIColor clearColor];

    //下一个
    self.lastButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.lastButton.frame = CGRectMake(0, 0, SCALEoefficient(16), SCALEoefficient(32));
    self.lastButton.centerY = self.shoesCarouselView.centerY;
    self.lastButton.left = self.shoesCarouselView.right-10;
    [self.lastButton setBackgroundImage:[MYFactoryManager imageString:@"gamepop_rihtarrow"] forState:UIControlStateNormal];
    [self.view addSubview:self.lastButton];
    [self.lastButton addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
    self.lastButton.backgroundColor = [UIColor clearColor];
    
    //关闭弹窗
    self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancelButton.frame = CGRectMake(SCREEN_WIDTH-SCALEoefficient(100+20),SCALEoefficient(15), SCALEoefficient(100), SCALEoefficient(30));
    [self.cancelButton setTitle:@"左踮脚 · 返回" forState:UIControlStateNormal];
    [self.cancelButton setImage:[MYFactoryManager imageString:@"gamepop_close_notes"] forState:UIControlStateNormal];
    [self.cancelButton setFont: [UIFont boldSystemFontOfSize: SCALEoefficient(12)]];
    [self.cancelButton.titleLabel setTextColor:[UIColor whiteColor]];
    self.cancelButton.backgroundColor = [UIColor clearColor];
    [self.cancelButton setImageEdgeInsets:UIEdgeInsetsMake(0, SCALEoefficient(-10), 0, 0)];
    [self.cancelButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.cancelButton];
    
    //隐藏的清理缓存的按钮
    self.clearnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.clearnButton.frame = CGRectMake(0, SCREEN_HEIGHT-SCALEoefficient(30)-SCALEoefficient(25), SCALEoefficient(60), SCALEoefficient(25));
    self.clearnButton.backgroundColor = [UIColor clearColor];
    [self.clearnButton addTarget:self action:@selector(clearCacheWithFilePath) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.clearnButton];

    NSArray * array = @[@"gamepop_leftarrow_notes",@"gamepop_rihgtarrow_notes",@"gamepop_determine_notes"];
    NSArray * titleArray = @[@"左踏 · 上一个",@"右踏 · 下一个",@"右踮脚 · 确认"];
    for (int i =0; i<3; i++){
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
//        button.frame = CGRectMake(SCREEN_WIDTH-SCALEoefficient(62+20+20)*(4-i),SCALEoefficient(23), SCALEoefficient(62+20), SCALEoefficient(20));
        button.frame = CGRectMake(SCREEN_WIDTH/3*i,SCREEN_HEIGHT-SCALEoefficient(30), SCREEN_WIDTH/3, SCALEoefficient(30));
        [button setTitle:titleArray[i] forState:UIControlStateNormal];
        [button setImage:[MYFactoryManager imageString:array[i]] forState:UIControlStateNormal];
        [button setFont: [UIFont boldSystemFontOfSize: SCALEoefficient(10)]];
        [button.titleLabel setTextColor:[UIColor whiteColor]];
//        button.tag = i;
        button.backgroundColor = [UIColor blackColor];
        [button setImageEdgeInsets:UIEdgeInsetsMake(0, SCALEoefficient(-10), 0, 0)];
        //        [searchButton addTarget:self action:@selector(mapBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    }
    
}

#pragma mark -------------- 外部事件
//刷新蓝牙数据
-(void)reloadData{
    
    NSMutableArray * dataArr = [NSMutableArray new];
    for (int i = 0; i<self.deviceArray.count; i++){
        ShoesPeripheralMolde *model = [[ShoesPeripheralMolde alloc]init];
        model.numberId = i;
        model.peripheral = self.deviceArray[i];
//        NSLog(@"model.peripheral === >> %@",model.peripheral.name);
        [dataArr addObject:model];
    }
    [self.shoesCarouselView setArrayData:dataArr];
    
    if (dataArr.count==0){
        self.tipView.hidden = NO;
    }else{
        self.tipView.hidden = YES;
    }
    
}

//蓝牙传过来的脚步交互动作
-(void)getInteraction:(int)interaction{
    
    if (interaction == 0){//原地 -- 复位
        
    }else if(interaction == 3){//左踩 -- 选中上一个设备
        
        [self lastAction];

    }else if(interaction == 4){//右踩 -- 选中下一个设备
        
        [self nextAction];

    }else if(interaction == 6){//左垫脚 -- 关闭弹窗
        
        [self closeAction];
        
    }else if(interaction == 5){//右垫脚 -- 链接设备
     
        if (self.connectDeviceBlock){
            self.connectDeviceBlock(self.currentPeripheral);
        }
        [self closeAction];
        
    }
    
}

////点击搜索按钮（暂留）
//-(void)searchAction{
//
//    self.tipView.hidden = NO;
//    if(self.searchDeviceTask){
//        [BTDataInstance initCBCentralManager];
//    }
//
//}

#pragma mark -------------- 点击界面交互Action
//选中上一个设备
- (void)lastAction{
    [_shoesCarouselView yw_gotoNext:-1];
}

//选中下一个设备
-(void)nextAction{
    [_shoesCarouselView yw_gotoNext:1];
}

//确认选中的设备 开始蓝牙连接
-(void)selectedDeviceAction{
    
    if (self.connectDeviceBlock){
        self.connectDeviceBlock(self.currentPeripheral);
    }
    [self closeAction];
    
}

//关闭弹窗
- (void)closeAction{
    
    if (self.closeBlock){
        self.closeBlock();
    }

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.25 animations:^{
            self.view.alpha = 0;
            [self.view  removeFromSuperview];
        }completion:^(BOOL finished){
            [self.view  removeFromSuperview];
        }];
    });
    
}

//清理缓存
-(void)clearCacheWithFilePath{
    [DebugViewInstance clearCacheWithFilePath];
}

#pragma mark -------------- shoesCarouseView delegate
-(void)setCurrentperipheral:(CBPeripheral*)peripheral{
    self.currentPeripheral = peripheral;
}
//来自shoesCarouseView被选中的当前蓝牙
-(void)connectDevice:(CBPeripheral*)peripheral{
    
    self.currentPeripheral = peripheral;
    //链接蓝牙
    if (self.connectDeviceBlock){
        self.connectDeviceBlock(self.currentPeripheral);
    }
    [self closeAction];
    
}


#pragma mark -------------- lazy
-(NSMutableArray*)deviceArray{

    if (!_deviceArray){
        _deviceArray = [NSMutableArray new];
    }
    return _deviceArray;
    
}

-(NoDeviceTip*)tipView{
    
    if (!_tipView){
        _tipView = [[NoDeviceTip alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-50, SCREEN_HEIGHT/2-100, 100, 150)];
        _tipView.hidden = NO;
//        _tipView.backgroundColor = [UIColor redColor];
    }
    return _tipView;
    
}

@end
