//
//  AudioRawDataOutputConfigViewController.m
//  HWMSDKDemo
//
//

#import "AudioRawDataOutputConfigViewController.h"
#import <HWMUISDK/HWMBizSdk.h>
#import "GlobalHandlerImpl.h"

@interface AudioRawDataOutputConfigViewController ()

@property (weak, nonatomic) IBOutlet UISwitch *enableAudioRawDataOutputSwitch;
@property (weak, nonatomic) IBOutlet UITextField *intervalTF;

@end

@implementation AudioRawDataOutputConfigViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"开启获取音频流";
    [UIView addBottomLineWithView:self.intervalTF parentView:self.view];
    self.enableAudioRawDataOutputSwitch.on = [SDKEnvConfig sharedInstance].enableAudioRawDataOutput;
}

- (IBAction)onSaveBtnClick {
    NSInteger interval = self.intervalTF.text.integerValue;
    HWMSDKRawAudioFrameSubscribeInfoModel *model = [[HWMSDKRawAudioFrameSubscribeInfoModel alloc] init];
    model.enable = self.enableAudioRawDataOutputSwitch.isOn;
    model.interval = interval;
    HWMSDKERR ret = [[HWMBizSdk getBizOpenApi] setAudioRawDataOutputConfig:model];
    if (ret == HWMSDKSdkerrSuccess) {
        [UIUtil showMessage:@"保存成功"];
        [SDKEnvConfig sharedInstance].enableAudioRawDataOutput = model.enable;
        
        if (model.enable) {
            NSString *audioFilePath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"/log/AudioData.log"];
            if ([[NSFileManager defaultManager] fileExistsAtPath:audioFilePath]) {
                [[NSFileManager defaultManager] removeItemAtPath:audioFilePath error:NULL];
            }
            [[NSFileManager defaultManager] createFileAtPath:audioFilePath contents:nil attributes:nil];
            [GlobalHandlerImpl sharedInstance].audioFrameDataFile = audioFilePath;
        }
    } else {
        [UIUtil showMessage:[NSString stringWithFormat:@"保存失败: %ld", ret]];
    }
}


@end
