//
//  MessageModel.m
//  SLAiELTS
//
//  Created by Gusont on 2023/3/3.
//

#import "MessageModel.h"
#import <AVFoundation/AVFoundation.h>

@implementation MessageModel

+ (instancetype)initWithSendText:(NSString *)sendText isReceive:(BOOL)isReceive {
    MessageModel *model = [[MessageModel alloc] init];
    model.sendText = sendText;
    model.isReceive = isReceive;
    return model;
}

+ (instancetype)initWithSendText:(NSString *)sendText
                       isReceive:(BOOL)isReceive
                        showMike:(BOOL)showMike
                        showText:(BOOL)showText {
    MessageModel *model = [[MessageModel alloc] init];
    model.sendText = sendText;
    model.isReceive = isReceive;
    model.showMike = showMike;
    model.showText = showText;
    CGFloat sendTime = [[NSDate date] timeIntervalSince1970];
    model.sendTime = sendTime;
    return model;
}

+ (instancetype)initWithSendText:(NSString *)sendText
                       isReceive:(BOOL)isReceive
                        showMike:(BOOL)showMike
                        showText:(BOOL)showText
                          isMike:(BOOL)isMike {
    MessageModel *model = [[MessageModel alloc] init];
    model.sendText = sendText;
    model.isReceive = isReceive;
    model.showMike = showMike;
    model.showText = showText;
    model.isMike = isMike;
    CGFloat sendTime = [[NSDate date] timeIntervalSince1970];
    model.sendTime = sendTime;
    return model;
}

+ (instancetype)initWithSendText:(NSString *)sendText
                       isReceive:(BOOL)isReceive
                        showMike:(BOOL)showMike
                        showText:(BOOL)showText
                          isMike:(BOOL)isMike
                       voicePath:(NSString *)voicePath {
    MessageModel *model = [[MessageModel alloc] init];
    model.sendText = sendText;
    model.isReceive = isReceive;
    model.showMike = showMike;
    model.showText = showText;
    model.isMike = isMike;
    model.voicePath = voicePath;
    CGFloat sendTime = [[NSDate date] timeIntervalSince1970];
    model.sendTime = sendTime;
    return model;
}

+ (instancetype)initWithSendText:(NSString *)sendText
                       isReceive:(BOOL)isReceive
                        showMike:(BOOL)showMike
                        showText:(BOOL)showText
                          isMike:(BOOL)isMike
                        showTime:(BOOL)showTime
                        sendTime:(CGFloat)sendTime
                       voicePath:(NSString *)voicePath
                     sendFailure:(BOOL)sendFailure
                   sendTextTrans:(NSString *)sendTextTrans {
    MessageModel *model = [[MessageModel alloc] init];
    model.sendText = sendText;
    model.isReceive = isReceive;
    model.showMike = showMike;
    model.showText = showText;
    model.isMike = isMike;
    model.voicePath = voicePath;
    model.sendTime = sendTime;
    model.showTime = showTime;
    model.sendFailure = sendFailure;
    model.sendTextTrans = sendTextTrans;
    return model;
}

- (void)setVoicePath:(NSString *)voicePath {
    _voicePath = voicePath;
    if (_voicePath.length) {
        _audioDuration = [self calcuLateAudioDuration];
    }
}
- (NSTimeInterval)calcuLateAudioDuration {
    AVURLAsset *audioAsset = nil;
    NSDictionary *dic = @{AVURLAssetPreferPreciseDurationAndTimingKey:@(YES)};
    audioAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:_voicePath] options:dic];
    CMTime audioDuration = audioAsset.duration;
    float audioDurationSeconds = CMTimeGetSeconds(audioDuration);
    return audioDurationSeconds;
}

@end


@implementation SLMessageSendModel

@end

@implementation SLMessageDetail

@end
