//
//  RDSRootControl.m
//  SmartLightForBigFish
//
//  Created by coderYK on 2017/8/7.
//  Copyright © 2017年 RD-iOS. All rights reserved.
//

#import "RDSRootControl.h"
#import "RDSNavController.h"
#import "RDSLoginPwdCodeVC.h"
#import "RDSLoginVerifyCodeVC.h"
#import "RDSTabbarController.h"



@interface RDSRootControl ()

@property (nonatomic, strong) UIViewController *rootViewController;
@property (nonatomic, strong) UIViewController *loginVC;
//@property (nonatomic, strong) UIViewController *indexVC;
@property (nonatomic, assign) BOOL needPushToHome; // ** <#注释#>*/

@end

@implementation RDSRootControl

@synthesize isLoginSuccess = _isLoginSuccess;

+ (instancetype)shareControl {
    
    static RDSRootControl *_instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}

- (UIViewController *)rootViewController {
    
    if (self.isLoginSuccess) {
        _rootViewController = [[RDSTabbarController alloc] init];
        _haveLogined = YES;
    }
    else {
        // 是否有设置过密码
        if(TheDataManager.isSetPassword){
            _rootViewController = [[RDSNavController alloc] initWithRootViewController:[[RDSLoginPwdCodeVC alloc] init]];
        }else{
            _rootViewController = [[RDSNavController alloc] initWithRootViewController:[[RDSLoginVerifyCodeVC alloc] init]];
        }
        
    }
    return _rootViewController;
}

- (UIViewController *)resetRootController{
    _rootViewController = [[RDSTabbarController alloc] init];
    _haveLogined = YES;
    return _rootViewController;
}


- (BOOL)isLoginSuccess{
//    _isLoginSuccess = TheDataManager.isStartAgain;
    return _isLoginSuccess;
}

- (void)setIsLoginSuccess:(BOOL)isLoginSuccess {
    
    if(isLoginSuccess == NO){
        [TheDataManager rds_cacheClean];
    }
    
    if (_isLoginSuccess != isLoginSuccess) {
        _isLoginSuccess = isLoginSuccess;
        
//        _rootViewController = self.indexVC;
        
        _needPushToHome = YES;
        [self p_chanceRootViewController:UIViewAnimationOptionTransitionCrossDissolve];
    }
}

- (void)p_chanceRootViewController:(UIViewAnimationOptions )options {
    
    typedef void (^Animation)(void);
    UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
    
//    self.rootViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    RDS_WEAKSELF(weakSelf)
    Animation animation = ^{
        BOOL oldState = [UIView areAnimationsEnabled];
        [UIView setAnimationsEnabled:NO];
        window.rootViewController = weakSelf.rootViewController;
        [UIView setAnimationsEnabled:oldState];
    };
    
    [UIView transitionWithView:window
                      duration:0.3f
                       options:options
                    animations:animation
                    completion:nil];
}

@end
