//
//  CSTabBarController.m
//  zhuxun
//
//  Created by winsoft on 17/6/22.
//
//

#import "CSTabBarController.h"
#import "IWNavigationController.h"

#import "CSFirstPageController.h"
#import "ShareListViewController.h"
#import "CSFileTransListController.h"
#import "PreferencesViewController.h"

#import "CloudStorageDownloadCenter.h"
#import "CloudStorageUploadCenter.h"

#import "HttpRequest.h"
@implementation CSTabBarController
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    BaseViewController *vc = [CSFirstPageController csFirstPageControllerWithCSFileSourceType:CSFileSourceTypeSelf pid:@"0" shareId:nil];
    IWNavigationController *nv1 = [self allocNavgationController:vc];
    
    UIImage* nv1Img = [UIImage imageNamed:@"msg_icon"];
    UIImage* nv1SelImg = [[UIImage imageNamed:@"msg_icon_sel"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nv1.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"云盘" image:nv1Img selectedImage:nv1SelImg];
    nv1.tabBarItem.tag=0;//113, 200, 242
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:RGB(111, 200, 244) forKey:NSForegroundColorAttributeName];
    [nv1.tabBarItem setTitleTextAttributes:attributes forState:UIControlStateSelected];
    
    BaseViewController *vc2 = [[ShareListViewController alloc] init];
    IWNavigationController *nv2 = [self allocNavgationController:vc2];
    
    UIImage* nv2Img = [UIImage imageNamed:@"msg_icon"];
    UIImage* nv2SelImg = [[UIImage imageNamed:@"msg_icon_sel"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nv2.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"分享" image:nv2Img selectedImage:nv2SelImg];
    nv2.tabBarItem.tag=1;//113, 200, 242
    [nv2.tabBarItem setTitleTextAttributes:attributes forState:UIControlStateSelected];
  
    BaseViewController *vc3 = [[CSFileTransListController alloc] init];
    IWNavigationController *nv3 = [self allocNavgationController:vc3];
    
    UIImage* nv3Img = [UIImage imageNamed:@"msg_icon"];
    UIImage* nv3SelImg = [[UIImage imageNamed:@"msg_icon_sel"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nv3.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"传输列表" image:nv3Img selectedImage:nv3SelImg];
    nv3.tabBarItem.tag=2;//112, 200, 242
    [nv3.tabBarItem setTitleTextAttributes:attributes forState:UIControlStateSelected];
    
    BaseViewController *vc4 = [[PreferencesViewController alloc] init];
    IWNavigationController *nv4 = [self allocNavgationController:vc4];
    
    UIImage* nv4Img = [UIImage imageNamed:@"msg_icon"];
    UIImage* nv4SelImg = [[UIImage imageNamed:@"msg_icon_sel"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nv4.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"更多" image:nv4Img selectedImage:nv4SelImg];
    nv4.tabBarItem.tag=3;//113, 200, 242
    [nv4.tabBarItem setTitleTextAttributes:attributes forState:UIControlStateSelected];
    self.viewControllers = @[nv1, nv2,nv3,nv4];
    
    
    //认证
    __weak typeof(self)weakSelf = self;
    //临时传入自己的号
    [userDefaults setObject:@"hxb" forKey:USERDEFAULT_LOGIN_NAME];
    [userDefaults setObject:@"e9bc0e13a8a16cbb07b175d92a113126" forKey:USERDEFAULT_PASSWORD];
    [userDefaults setObject:@"WSPROD" forKey:USERDEFAULT_SERVER_ID];
    [self doLoginResultBlock:^(bool result){
        
        if (result) {
            
            
            [weakSelf resumeDownUpTask];
        }else{
            
        }
        
    }];

    [self resumeDownUpTask];
  
}

- (void)resumeDownUpTask
{
    
    [kCloudStorageDB getAllProcessOrWaitFileWithResultBlock:^(NSArray *downModels,NSArray *upModels) {
        
        for (CSFileDownUpModel *downFileDownUpModel in downModels ) {

            [[CloudStorageDownloadCenter sharedInstance]addOneDownloadWithFileDownUpModel:downFileDownUpModel];
        }
        
        for (CSFileDownUpModel *upFileDownUpModel in upModels ) {
            
            [[CloudStorageUploadCenter sharedInstance]addOneFileToUpdateWithCSFileDownUpModel:upFileDownUpModel];
        }

        
    }];
    
}

- (void)doLoginResultBlock:(void(^)(bool result))resultBlock
{
    NSDictionary *dict = @{@"username":Login_Name,
                           @"password":Login_Word,
                           @"server_id":Server_ID,
                           @"hashPassword":@(YES)//是否加密 若不加密  则不用不传改该字段
                           };
    //[[HttpRequest sharedManager]cloudStoragePostLoginWithParams:@{@"username":Login_Name,@"password":@"Ab123456",@"server_id":@"002682BECE10"}
//[[HttpRequest sharedManager]cloudStoragePostLoginWithParams:@{@"username":Login_Name,@"password":@"12345678",@"server_id":Server_ID}
 [[HttpRequest sharedManager]cloudStoragePostLoginWithParams:dict complete:^(BOOL success, id data, NSError *error) {
        if (success && data) {
            resultBlock(success);
        }else resultBlock(false);
    }];
}

-(IWNavigationController*)allocNavgationController:(UIViewController*)vc{
    IWNavigationController* nav = [[IWNavigationController alloc] initWithRootViewController:vc];
    
        UIImage* navbg = [UIImage imageNamed:@"top_nav"] ;
        navbg = [navbg stretchableImageWithLeftCapWidth:0 topCapHeight:5];
        [nav.navigationBar setBackgroundImage:navbg forBarMetrics:UIBarMetricsDefault];
    return nav;
}
@end
