//
//  GuideViewController.m
//  双子星云手机
//
//  Created by APPLE on 2023/8/11.
//

#import "GuideViewController.h"
#import "Masonry.h"
#import "UIView+View.h"
#import "GuideView.h"
#import "GuideLastView.h"
#import "SetPWDFirstViewController.h"
#import "connectDeviceManager.h"
#import "inputPWDViewController.h"

@interface GuideViewController ()<UIScrollViewDelegate>
{
    UIScrollView *bgScrView;
    GuideView *guideView0;
    GuideView *guideView1;
    GuideView *guideView2;
    GuideView *guideView3;
    GuideLastView *guideLastView;
}

@end

@implementation GuideViewController
@synthesize delegate;
@synthesize sn;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationBar.hidden = YES;
    
    [self drawAnyView];
    
    
    if([[connectDeviceManager shareInstance] curConnectDeviceState] == DeviceConnectUnknown){
        [[connectDeviceManager shareInstance] getThridMsgBySN:sn needReconnect:YES didNetEnd:^(bool didSuc) {
            
        }];
    }
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self startMoveView];
    });
}

- (void)startMoveView{

    CGFloat x = bgScrView.contentOffset.x;
    NSInteger index = (x+1)/self.view.width;
    if (index == 4){
        /*记录已经用户引导*/
        [HWDataManager setBoolWithKey:Const_Have_Show_Guide value:YES];
        
       
        if(![connectDeviceManager shareInstance].DeviceThirdIdMod){
            
            HLog(@"没有拿到设备信息");
            
            KWeakSelf
            [[connectDeviceManager shareInstance] getThridMsgBySN:sn needReconnect:YES didNetEnd:^(bool didSuc) {
                if(didSuc){
                    
                }
                else{
                    [[iToast makeText:NSLocalizedString(@"guide_qrcoede_tips_get_SdnID_fail",nil)] show];
                }
                
                [weakSelf startMoveView];
            }];
            
            return;
        }

        //是否已经有密码了 有就是输入密码 没有就是设置密码
        NSString *curPwd = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.password;
        
        
        if(curPwd && curPwd.length > 0){
            BOOL haveOpenMask = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.isPrivacyMode;
            if(!haveOpenMask)
            {
                [connectDeviceManager shareInstance].isFirstInputPwdDone = YES;
                [[NSNotificationCenter defaultCenter] postNotificationName:GuideOkNotification object:nil];
               
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [[NSNotificationCenter defaultCenter] postNotificationName:ShowImgAndVoiceNotification object:nil];
                });
                
                return;
            }
            
            /*设置密码*/
            inputPWDViewController *nextVC = [[inputPWDViewController alloc] init];
            nextVC.isQRCodeType = YES;
            [self.navigationController pushViewController:nextVC animated:YES];
        }
        else{
            /*设置密码*/
            SetPWDFirstViewController *nextVC = [[SetPWDFirstViewController alloc] init];
            [self.navigationController pushViewController:nextVC animated:YES];
        }
        
        
    }else{
        [bgScrView setContentOffset:CGPointMake((index + 1)*self.view.width, 0) animated:YES];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self startMoveView];
        });
    }
}

- (void)drawAnyView{
    [self.view setBackgroundColor:HWF5F7FAColor];
    
    bgScrView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    [bgScrView setBackgroundColor:HWF5F7FAColor];
    [bgScrView setShowsVerticalScrollIndicator:NO];
    [bgScrView setShowsHorizontalScrollIndicator:NO];
    [bgScrView setPagingEnabled:YES];
//    [bgScrView setUserInteractionEnabled:NO];
    bgScrView.delegate = self;
    [bgScrView setContentSize:CGSizeMake(5*self.view.width,self.view.height)];
    [bgScrView setClipsToBounds:YES];
    [self.view addSubview:bgScrView];
    
    guideView0 = [[GuideView alloc] initWithTips:NSLocalizedString(@"guide_middle_tips_0",nil)
                                       imageName:@"guide_middle_0_icon"
                                           index:0];
    guideView1 = [[GuideView alloc] initWithTips:NSLocalizedString(@"guide_middle_tips_1",nil)
                                       imageName:@"guide_middle_1_icon"
                                           index:1];
    guideView2 = [[GuideView alloc] initWithTips:NSLocalizedString(@"guide_middle_tips_2",nil)
                                       imageName:@"guide_middle_2_icon"
                                           index:2];
    guideView3 = [[GuideView alloc] initWithTips:NSLocalizedString(@"guide_middle_tips_3",nil)
                                       imageName:@"guide_middle_3_icon"
                                           index:3];
    
    guideView0.frame = CGRectMake(0*self.view.width, 0, self.view.width,self.view.height);
    guideView1.frame = CGRectMake(1*self.view.width, 0, self.view.width,self.view.height);
    guideView2.frame = CGRectMake(2*self.view.width, 0, self.view.width,self.view.height);
    guideView3.frame = CGRectMake(3*self.view.width, 0, self.view.width,self.view.height);
    
    [bgScrView addSubview:guideView0];
    [bgScrView addSubview:guideView1];
    [bgScrView addSubview:guideView2];
    [bgScrView addSubview:guideView3];
    
    guideLastView = [[GuideLastView alloc] initWithFrame:CGRectMake(4*self.view.width, 0, self.view.width,self.view.height)];
    [bgScrView addSubview:guideLastView];
    
    KWeakSelf
    guideView0.didClickButton = ^{
        [weakSelf didClickSkipFun];
    };
    
    guideView1.didClickButton = ^{
        [weakSelf didClickSkipFun];
    };
    
    guideView2.didClickButton = ^{
        [weakSelf didClickSkipFun];
    };
    
    guideView3.didClickButton = ^{
        [weakSelf didClickSkipFun];
    };
}

#pragma mark 点击了跳过
- (void)didClickSkipFun
{
    [bgScrView setContentOffset:CGPointMake(4*self.view.width, 0) animated:NO];
}

#pragma mark 监听滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat x = scrollView.contentOffset.x;
    NSInteger index = (x+1)/self.view.width;
    if (index == 4){
        bgScrView.userInteractionEnabled = NO;
    }
}
@end
