//
//  HWDataManager.m
//  VclustersGemini
//
//  Created by 余衡武 on 2021/11/23.
//  Copyright © 2021 APPLE. All rights reserved.
//

#import "HWDataManager.h"

static HWDataManager *shareInstance = nil;

@implementation HWDataManager

+ (instancetype)shareInstance {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareInstance = [[HWDataManager alloc] init];
    });
    return shareInstance;
}

#pragma mark NSUserDefaults
+ (BOOL)getBoolWithKey:(NSString*)key{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults boolForKey:key];
}

+ (void)setBoolWithKey:(NSString*)key value:(BOOL)value{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:value forKey:key];
    [defaults synchronize];
}

+ (NSInteger)getIntegerWithKey:(NSString*)key{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults integerForKey:key];
}

+ (void)setIntegerWithKey:(NSString*)key value:(NSInteger)value{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:value forKey:key];
    [defaults synchronize];
}

+ (NSNumber *)getNumberWithKey:(NSString* _Nonnull)key {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults valueForKey:key];
}

+ (void)setNumberWithKey:(NSString* _Nonnull)key value:(NSNumber *)value {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setValue:value forKey:key];
    [defaults synchronize];
}

+ (NSString*)getStringWithKey:(NSString*)key{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults stringForKey:key];
}

+ (void)setStringWithKey:(NSString*)key value:(NSString*)value{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:value forKey:key];
    [defaults synchronize];
}

+ (nullable id)getObjectWithKey:(NSString* _Nonnull)key {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:key];
}

+ (void)setObjectWithKey:(NSString* _Nonnull)key value:(nullable id)value {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:value forKey:key];
    [defaults synchronize];
}

#pragma mark Plist
// 读取Plist缓存数据
+ (nullable id)readObjectFromPlistFolder:(NSString *)plistFolder {
    // 文件完整路径
    NSString *fileName = [NSString stringWithFormat:@"%@.plist", plistFolder.lastPathComponent];
    NSString *filePlist = [plistFolder stringByAppendingPathComponent:fileName];
    // 文件夹路径安全校验
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePlist]) {
        return [NSMutableArray array];
    }
    // 读取plist数据
    id object = [NSKeyedUnarchiver unarchiveObjectWithFile:filePlist];
    HLog(@"plist数据读取成功：%@", plistFolder);
    return object;
}

// 储存Plist缓存数据
+ (void)archiveObject:(nullable id)object toPlistFolder:(NSString *)plistFolder {
    
    if (!object) {
        return;
    }
    
    // 文件夹路径安全校验
    NSError *error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistFolder]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:plistFolder withIntermediateDirectories:YES attributes:nil error:&error];
    }
    // 文件完整路径
    NSString *fileName = [NSString stringWithFormat:@"%@.plist", plistFolder.lastPathComponent];
    NSString *filePlist = [plistFolder stringByAppendingPathComponent:fileName];
    // 存储plist数据
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:object];
    BOOL finish = [data writeToFile:filePlist atomically:YES];
    if (finish) {
        HLog(@"plist数据缓存成功：%@", plistFolder);
    }else {
        HLog(@"plist数据缓存失败：%@", plistFolder);
    }
}

+ (nullable id)readObjectFromPlistPath:(NSString *)plistPath {
    // 读取plist数据
    id object = [NSKeyedUnarchiver unarchiveObjectWithFile:plistPath];
    HLog(@"plist数据读取成功：%@", plistPath);
    return object;
}

+ (void)archiveObject:(nullable id)object toPlistPath:(NSString *)plistPath {
    
    if (!object) {
        return;
    }
    // 存储plist数据
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:object];
    BOOL finish = [data writeToFile:plistPath atomically:YES];
    if (finish) {
        HLog(@"plist数据缓存成功：%@", plistPath);
    }else {
        HLog(@"plist数据缓存失败：%@", plistPath);
    }
}

+ (NSString *)pathForDeviceDriftWithAccount:(NSString *)account sn:(NSString *)sn {
    NSString *fileFolder = [NSString stringWithFormat:@"%@/Drift",sn];
    return [self documentPathForAccount:account fileFolder:fileFolder];
}

/**
 * 文件路径：DocumentPath/account/fileFolder
 * account 路径不需要拼接账号时 传@""
 */
+ (NSString *)documentPathForAccount:(NSString *)account fileFolder:(NSString *)fileFolder {
    
    NSString *path = DocumentPath;
    if (account.length != 0) {
        path = [NSString stringWithFormat:@"%@/%@/",DocumentPath,account];
    }
        
    if ([fileFolder containsString:@"/"]) {
        NSString *path1 = [path stringByAppendingPathComponent:fileFolder];
        path1 = [path1 stringByDeletingLastPathComponent];
        NSError *error;
        if (![[NSFileManager defaultManager] fileExistsAtPath:path1]) {
            [[NSFileManager defaultManager] createDirectoryAtPath:path1 withIntermediateDirectories:YES attributes:nil error:&error];
        }
    }else{
        NSError *error;
        if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
            [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
        }
    }
    
    return [path stringByAppendingPathComponent:fileFolder];
}


#pragma mark 截图
/**保存截图保存路径*/
+ (NSString *)writeScreenShotImageToLocal:(UIImage *)image withName:(NSString *)fileName {
    
    // 文件夹路径安全校验
    NSString *fileFolder = kPath_ScreenShot;
    NSError *error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:fileFolder]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:fileFolder withIntermediateDirectories:YES attributes:nil error:&error];
    }
    
    // 截图写入本地
    NSString *sandboxPath = [NSString stringWithFormat:@"%@/%@.png",fileFolder,fileName];
    NSData *imageData = UIImagePNGRepresentation(image);
    BOOL isSuccess = [imageData writeToFile:sandboxPath atomically:YES];
    HLog(@"保存相册图片文件到Caches是否成功:%d fileName:%@", isSuccess,fileName);
    if (isSuccess) {
        return sandboxPath;
    }else {
        return @"";
    }
}


#pragma mark 沙盒路径
// 读取指定路径下的文件or文件夹
+ (NSArray *)getFileOrFolderAtPath:(NSString *)basePath  {
    NSError *error = nil;
    NSArray *tempArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:basePath error:&error];
    return tempArr;
}

+ (NSString *)pathForDevicePhotoWithAccount:(NSString *)account deviceId:(NSString *)deviceId {
    NSString *fileFolder = [NSString stringWithFormat:@"%@/Thumbnail",deviceId];
    return [self documentPathForAccount:account fileFolder:fileFolder];
}
#pragma mark FMDB数据库 - Model

@end
