//
//  PRTCSteamRecordFileManager.m
//
//  Created by super on 2021/8/17.
//  Copyright © 2021 RongCloud. All rights reserved.
//

#import "PRTCSteamRecordFileManager.h"

@interface PRTCSteamRecordFileManager()
@property (nonatomic ,copy) NSString *mediaFolderName;
@property (nonatomic ,copy) NSString *steamRecordAudioPath;
@end

@implementation PRTCSteamRecordFileManager

+ (PRTCSteamRecordFileManager *)shareManager{
    static dispatch_once_t once;
    static PRTCSteamRecordFileManager *instance;
    dispatch_once(&once, ^{
        instance = [[PRTCSteamRecordFileManager alloc] init];
        instance.mediaFolderName = @"RCRTCDemo";
    });
    return instance;
}

- (void)createMediaFileFolderName:(NSString *)folderName
                  audioFolderName:(NSString *)audioName {
    [self createMediaFileFolder:folderName];
    [self createAudioFolder:audioName];
}

- (NSString *)createMediaFileFolder:(NSString *)folderName {
    if (folderName && folderName.length > 0) {
        self.mediaFolderName = folderName;
        return folderName;
    }else {
        return self.mediaFolderName;
    }
}

- (NSString *)createAudioFolder:(NSString *)folderName {
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",self.mediaFolderName,folderName.length!=0?folderName:@"audio"]];
    if (![fileManager fileExistsAtPath:writablePath]) {//第一次 需要创建
        [fileManager createDirectoryAtPath:writablePath withIntermediateDirectories:YES attributes:nil error:nil];
    }else {
//        文件夹已经存在
    }
    self.steamRecordAudioPath = writablePath;
    return writablePath;
}

- (NSString *)fechAudioPath {
    if ([self.steamRecordAudioPath length]!=0) {
        return self.steamRecordAudioPath;
    }else {

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@",self.mediaFolderName,@"audio"]];
        return writablePath;
    }
}

- (void)deleteLocalMediaPath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",self.mediaFolderName]];
    [[NSFileManager defaultManager] removeItemAtPath:writablePath error:nil];
}
@end
