//
//  FWZFileGetTool.m
//  smartRhino
//
//  Created by taidi on 2019/12/10.
//  Copyright © 2019 tederen. All rights reserved.
//

#import "FWZFileGetTool.h"
@interface FWZFileGetTool()<UIDocumentPickerDelegate>{
    ChoosFileBlock _choosFileBlock;
    ShowFileBlock _showFileBlock;
    UIImagePickerControllerSourceType _photoType;
    BOOL _crop;
}
@end

@implementation FWZFileGetTool
static FWZFileGetTool *tool = nil;

+ (instancetype)sharedTool
{
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        tool = [FWZFileGetTool new];
        
    });
    
    return tool;
}
- (void)openFileWithCrop:(BOOL)crop showImgBlock:(ShowFileBlock)showFileBlock choosFileBlock:(ChoosFileBlock)choosFileBlock{
                
    UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc]
                                                                       initWithDocumentTypes:@[@"public.image",@"com.adobe.pdf",@"com.microsoft.excel.xls",@"public.data",@"public.movie",@"public.text",@"public.audio",@"public.archive",@"com.microsoft.powerpoint.ppt",@"public.item",@"com.apple.application public.data"] inMode:UIDocumentPickerModeImport];
    
    documentPicker.delegate = self;
    
    documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
    
    documentPicker.modalTransitionStyle = [self getCurrentViewController].navigationController.modalTransitionStyle;
    
    documentPicker.modalPresentationStyle = [UtilsTools modalPresentationStyle:[self getCurrentViewController].navigationController.modalPresentationStyle];

    [[self getCurrentViewController] presentViewController:documentPicker animated:YES completion:^{
        
    }];
 
    _choosFileBlock = choosFileBlock;
    _showFileBlock = showFileBlock;
    
}
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
    
    if (controller.documentPickerMode == UIDocumentPickerModeImport) {
        NSString *alertMessage = [NSString stringWithFormat:@"Successfully imported %@", [[urls lastObject] lastPathComponent]];
        
          NSLog(@"%@",alertMessage);
        NSData *data = [NSData dataWithContentsOfURL:urls.lastObject];
        NSLog(@"%@",data);
          _showFileBlock([[urls lastObject] lastPathComponent]);
          _choosFileBlock(data,[[urls lastObject] lastPathComponent]);


    }
    
}
- (UIViewController *)getCurrentViewController {
    __block UIWindow *normalWindow = [[UIApplication sharedApplication].delegate window];
    if (normalWindow.windowLevel != UIWindowLevelNormal) {
        [[UIApplication sharedApplication].windows enumerateObjectsUsingBlock:^(__kindof UIWindow * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.windowLevel == UIWindowLevelNormal) {
                normalWindow = obj;
                *stop        = YES;
            }
        }];
    }
    
    return [self nextTopForViewController:normalWindow.rootViewController];
}

- (UIViewController *)nextTopForViewController:(UIViewController *)inViewController {
    while (inViewController.presentedViewController) {
        inViewController = inViewController.presentedViewController;
    }
    
    if ([inViewController isKindOfClass:[UITabBarController class]]) {
        UIViewController *selectedVC = [self nextTopForViewController:((UITabBarController *)inViewController).selectedViewController];
        return selectedVC;
    } else if ([inViewController isKindOfClass:[UINavigationController class]]) {
        UIViewController *selectedVC = [self nextTopForViewController:((UINavigationController *)inViewController).visibleViewController];
        return selectedVC;
    } else {
        return inViewController;
    }
}
@end