/*
 * Copyright (c) 2021 Taner Sener
 *
 * This file is part of FFmpegKit.
 *
 * FFmpegKit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * FFmpegKit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General License for more details.
 *
 *  You should have received a copy of the GNU Lesser General License
 *  along with FFmpegKit.  If not, see <http://www.gnu.org/licenses/>.
 */

#import "ExecuteCallback.h"
#import "FFmpegSession.h"
#import "FFmpegKitConfig.h"
#import "LogCallback.h"
#import "StatisticsCallback.h"

@implementation FFmpegSession {
    StatisticsCallback _statisticsCallback;
    NSMutableArray* _statistics;
    NSRecursiveLock* _statisticsLock;
}

+ (void)initialize {
    // EMPTY INITIALIZE
}

- (instancetype)init:(NSArray*)arguments {

    self = [super init:arguments withExecuteCallback:nil withLogCallback:nil withLogRedirectionStrategy:[FFmpegKitConfig getLogRedirectionStrategy]];

    if (self) {
        _statisticsCallback = nil;
        _statistics = [[NSMutableArray alloc] init];
        _statisticsLock = [[NSRecursiveLock alloc] init];
    }

    return self;
}

- (instancetype)init:(NSArray*)arguments withExecuteCallback:(ExecuteCallback)executeCallback {

    self = [super init:arguments withExecuteCallback:executeCallback withLogCallback:nil withLogRedirectionStrategy:[FFmpegKitConfig getLogRedirectionStrategy]];

    if (self) {
        _statisticsCallback = nil;
        _statistics = [[NSMutableArray alloc] init];
        _statisticsLock = [[NSRecursiveLock alloc] init];
    }

    return self;
}

- (instancetype)init:(NSArray*)arguments withExecuteCallback:(ExecuteCallback)executeCallback withLogCallback:(LogCallback)logCallback withStatisticsCallback:(StatisticsCallback)statisticsCallback {

    self = [super init:arguments withExecuteCallback:executeCallback withLogCallback:logCallback withLogRedirectionStrategy:[FFmpegKitConfig getLogRedirectionStrategy]];

    if (self) {
        _statisticsCallback = statisticsCallback;
        _statistics = [[NSMutableArray alloc] init];
        _statisticsLock = [[NSRecursiveLock alloc] init];
    }

    return self;
}

- (instancetype)init:(NSArray*)arguments withExecuteCallback:(ExecuteCallback)executeCallback withLogCallback:(LogCallback)logCallback withStatisticsCallback:(StatisticsCallback)statisticsCallback withLogRedirectionStrategy:(LogRedirectionStrategy)logRedirectionStrategy {

    self = [super init:arguments withExecuteCallback:executeCallback withLogCallback:logCallback withLogRedirectionStrategy:logRedirectionStrategy];

    if (self) {
        _statisticsCallback = statisticsCallback;
        _statistics = [[NSMutableArray alloc] init];
        _statisticsLock = [[NSRecursiveLock alloc] init];
    }

    return self;
}

- (StatisticsCallback)getStatisticsCallback {
    return _statisticsCallback;
}

- (NSArray*)getAllStatisticsWithTimeout:(int)waitTimeout {
    [self waitForAsynchronousMessagesInTransmit:waitTimeout];

    if ([self thereAreAsynchronousMessagesInTransmit]) {
        NSLog(@"getAllStatisticsWithTimeout was called to return all statistics but there are still statistics being transmitted for session id %ld.", [self getSessionId]);
    }

    return [self getStatistics];
}

- (NSArray*)getAllStatistics {
    return [self getAllStatisticsWithTimeout:AbstractSessionDefaultTimeoutForAsynchronousMessagesInTransmit];
}

- (NSArray*)getStatistics {
    [_statisticsLock lock];
    NSArray* statisticsCopy = [_statistics copy];
    [_statisticsLock unlock];
    
    return statisticsCopy;
}

- (Statistics*)getLastReceivedStatistics {
    Statistics* lastStatistics = nil;

    [_statisticsLock lock];
    if ([_statistics count] > 0) {
        lastStatistics = [_statistics objectAtIndex:0];
    }
    [_statisticsLock unlock];

    return lastStatistics;
}

- (void)addStatistics:(Statistics*)statistics {
    [_statisticsLock lock];
    [_statistics addObject:statistics];
    [_statisticsLock unlock];
}

- (BOOL)isFFmpeg {
    return true;
}

- (BOOL)isFFprobe {
    return false;
}

@end

