//
//  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

@implementation SLSocketMessage : NSObject

@end

@implementation SLMessageManager : NSObject

+ (instancetype)SharedInstance {
    static SLMessageManager *sharedInstance=nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (NSString *)replaceHasPrefix:(NSString *)fixStr str:(NSString *)str {
    if ([str hasPrefix:fixStr]) {
        NSString *newStr = [str substringFromIndex:fixStr.length];
        return [self replaceHasPrefix:fixStr str:newStr];
    }
    return str;
}

- (void)saveSocketChatMessageWith:(NSDictionary *)resultDic {
    dispatch_async(dispatch_queue_create("sl.cache.chat.date", DISPATCH_QUEUE_CONCURRENT), ^{
        SLSocketMessage *msgModel = [SLSocketMessage mj_objectWithKeyValues:[resultDic objectForKey:@"Data"]];
        NSString *str = [self replaceHasPrefix:@"\n" str:msgModel.Message];
        MessageModel *model = [MessageModel initWithSendText:str isReceive:YES showMike:msgModel.ChatType showText:!msgModel.ChatType isMike:msgModel.ChatType];
        NSString *key = [NSString stringWithFormat:@"%@--to--%@",[SLGlobalInfo SharedInstance].loginInfo.user.userId, msgModel.AIId];
        NSArray *arr = [[NSUserDefaults standardUserDefaults] objectForKey:key];
        NSMutableArray *marr = [NSMutableArray arrayWithArray:arr];
        [marr addObject:model];
        NSArray *dictArr = [NSObject mj_keyValuesArrayWithObjectArray:marr];
        [[NSUserDefaults standardUserDefaults] setObject:dictArr forKey:key];
        [[NSUserDefaults standardUserDefaults] synchronize];
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([[UIViewController getCurrentShowVC] isKindOfClass:NSClassFromString(@"SLHomeViewController")]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"SLChatListUpdate" object:nil];
            }
        });
    });
}

@end


@implementation SLMessageReceiveModel : NSObject

@end
