//
//  FileDownloadTool.m
//  ArcGis
//
//  Created by Mac Book DQ02 on 16/4/5.
//  Copyright © 2016年 Xu Mengzhu. All rights reserved.
//

#import "FileDownloadTool.h"

#define RGB(R,G,B) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1]

@implementation FileDownloadTool

+(void)OpenZip:(NSString*)zipPath  unzipto:(NSString*)_unzipto
{
    ZipArchive* zip = [[ZipArchive alloc] init];
    if( [zip UnzipOpenFile:zipPath] )
    {
        BOOL ret = [zip UnzipFileTo:_unzipto overWrite:YES];
        if( NO==ret )
        {
            return;
        }
        [zip UnzipCloseFile];
    }
    
}

+(NSString *)getTheCachePath
{
     //Get caches directory
    NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains
    (NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesPath = [directoryPaths objectAtIndex:0];
    return cachesPath;
}
+(NSString *)getTheTmpPath
{
    NSString *tmpDir = NSTemporaryDirectory();
    return tmpDir;
}

//判断文件是否存在
+(BOOL)whetherFileExist:(NSString *)filePath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
//    NSLog(@"filemanager=%@",fileManager);
    if ([fileManager fileExistsAtPath:filePath]) {
     //   NSLog(@"File exists");
        return YES;
    }
    return NO;
}

#pragma mark - 动态设置label的宽高
+(CGSize)font:(UIFont *)font  str:(NSString *)str{
    CGSize size =CGSizeMake(300,60);
    
    // label可设置的最大高度和宽度
    //    CGSize size = CGSizeMake(300.f, MAXFLOAT);
    
    //    获取当前文本的属性
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil];
    
    //ios7方法，获取文本需要的size，限制宽度
    
    CGSize  actualsize =[str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin  attributes:tdic context:nil].size;
    return actualsize;
    
}


#pragma mark - 自定义弹出框
+(void)alert:(NSString *)str view:(UIView*)view{
    
    float width=[self font:[UIFont systemFontOfSize:14] str:str].width;
    
    UILabel *alertLabel=[[UILabel alloc]init];
    alertLabel.center=CGPointMake(view.center.x,view.center.y+20);
    alertLabel.bounds=CGRectMake(0, 0,width+20, 40);
    alertLabel.backgroundColor=RGB(38, 38, 38);
    
    alertLabel.textAlignment=NSTextAlignmentCenter;
    alertLabel.font=[UIFont systemFontOfSize:14];
    alertLabel.text=str;
    alertLabel.textColor=[UIColor whiteColor];
    alertLabel.layer.cornerRadius=5;
    alertLabel.layer.masksToBounds=YES;
    [view addSubview:alertLabel];
    alertLabel.alpha=1;
    [UIView beginAnimations:nil context:nil];
    //设定动画持续时间
    [UIView setAnimationDuration:3];
    //动画的内容
    alertLabel.alpha=0;
    //动画结束
    [UIView commitAnimations];
}


//遍历文件夹获得文件夹大小，返回多少M
+ (float )folderSizeAtPath:(NSString*) folderPath{
    
    NSFileManager* manager = [NSFileManager defaultManager];
    
    if (![manager fileExistsAtPath:folderPath]) return 0;
    
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    
    NSString* fileName;
    
    long long folderSize = 0;
    
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        
        folderSize += [FileDownloadTool fileSizeAtPath:fileAbsolutePath];
        
    }
    return folderSize/(1024.0*1024.0);
}


//单个文件的大小
+(long long) fileSizeAtPath:(NSString*) filePath{
    
    NSFileManager* manager = [NSFileManager defaultManager];
    
    if ([manager fileExistsAtPath:filePath]){
        
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

#pragma mark 删除zip和解压缩文件
+(void)delZipFiles
{
    NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains
    (NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesPath = [directoryPaths objectAtIndex:0];
    NSString *zipPath = [cachesPath stringByAppendingPathComponent:@"create_scenic_area_info.zip"];
    BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:zipPath];//判断文件是否存在
    if(isExist){
        BOOL sucess4 = [[NSFileManager defaultManager] removeItemAtPath:zipPath error:nil];
        if(sucess4){
            //删除成功
        }else{
            //删除失败
        }
    }
}

@end
