// ViewController.m
// Tencent is pleased to support the open source community by making vap available.

#import "ViewController.h"
#import <QGVAPlayer.h>
#import <QGVAPlayer/QGVAPlayer-Swift.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController () <LNVAPPlaybackDelegate, LNVAPWrapPlaybackDelegate>

@property (nonatomic, strong) UIButton *vapButton;
@property (nonatomic, strong) UIButton *vapWrapButton;
@property (nonatomic, strong) UIButton *vapFailButton;
@property (nonatomic, strong) LNVAPPlayerView *playerView;
@property (nonatomic, strong) LNVAPWrapView *wrapView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupAudioSession];

    _vapButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, CGRectGetWidth(self.view.frame), 90)];
    _vapButton.backgroundColor = [UIColor lightGrayColor];
    [_vapButton setTitle:@"LN PlayerView" forState:UIControlStateNormal];
    [_vapButton addTarget:self action:@selector(playVap) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_vapButton];

    _vapWrapButton = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_vapButton.frame) + 60, CGRectGetWidth(self.view.frame), 90)];
    _vapWrapButton.backgroundColor = [UIColor lightGrayColor];
    [_vapWrapButton setTitle:@"LN WrapView" forState:UIControlStateNormal];
    [_vapWrapButton addTarget:self action:@selector(playWithWrapView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_vapWrapButton];

    _vapFailButton = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_vapWrapButton.frame) + 60, CGRectGetWidth(self.view.frame), 90)];
    _vapFailButton.backgroundColor = [UIColor lightGrayColor];
    [_vapFailButton setTitle:@"LN Fail Path" forState:UIControlStateNormal];
    [_vapFailButton addTarget:self action:@selector(playInvalidPath) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_vapFailButton];
}

- (void)setupAudioSession {
    AVAudioSession *session = [AVAudioSession sharedInstance];
    NSError *error = nil;
    [session setCategory:AVAudioSessionCategoryPlayback withOptions:0 error:&error];
    [session setActive:YES error:&error];
}

- (void)playVap {
    [self.playerView removeFromSuperview];
    self.playerView = [LNVAPFacade lnMakePlayerViewWithFrame:CGRectMake(0, 0, 752 / 2.0, 752 / 2.0)];
    self.playerView.center = self.view.center;
    self.playerView.delegate = self;
    self.playerView.enterBackgroundOperation = LNEnterBackgroundOperationStop;
    [self.playerView lnEnableOldVersion:YES];
    [LNVAPFacade lnSetMutePlayer:self.playerView mute:YES];
    [self.playerView lnAddTapGestureWithTarget:self action:@selector(onPlayerTap:)];
    [self.view addSubview:self.playerView];

    NSString *resPath = [NSString stringWithFormat:@"%@/Resource/demo.mp4", NSBundle.mainBundle.resourcePath];
    [LNVAPFacade lnPlayPlayer:self.playerView filePath:resPath repeatCount:-1 delegate:self];
}

- (void)playWithWrapView {
    [self.wrapView removeFromSuperview];
    self.wrapView = [LNVAPFacade lnMakeWrapViewWithFrame:self.view.bounds];
    self.wrapView.contentModeOption = LNVAPWrapContentModeAspectFit;
    self.wrapView.autoDestoryAfterFinish = YES;
    self.wrapView.delegate = self;
    [LNVAPFacade lnSetMuteWrap:self.wrapView mute:YES];
    [self.view addSubview:self.wrapView];

    __weak typeof(self) weakSelf = self;
    [self.wrapView lnAddVapTapGesture:^(UIGestureRecognizer *gesture, BOOL insideSource, LNVAPSourceDisplayItem *source) {
        [LNVAPFacade lnStopWrap:weakSelf.wrapView];
        [weakSelf.wrapView removeFromSuperview];
    }];

    NSString *resPath = [NSString stringWithFormat:@"%@/Resource/vap.mp4", NSBundle.mainBundle.resourcePath];
    [LNVAPFacade lnPlayWrap:self.wrapView filePath:resPath repeatCount:-1 delegate:self];
}

- (void)onPlayerTap:(UIGestureRecognizer *)gesture {
    [LNVAPFacade lnStopPlayer:self.playerView];
    [gesture.view removeFromSuperview];
}

- (void)playInvalidPath {
    [self.playerView removeFromSuperview];
    self.playerView = [LNVAPFacade lnMakePlayerViewWithFrame:CGRectMake(0, 0, 752 / 2.0, 752 / 2.0)];
    self.playerView.center = self.view.center;
    self.playerView.delegate = self;
    [self.view addSubview:self.playerView];
    [LNVAPFacade lnPlayPlayer:self.playerView filePath:@"/tmp/not_exist_vap.mp4" repeatCount:0 delegate:self];
}

- (void)lnPlayerDidStart:(LNVAPPlayerView *)playerView {
    NSLog(@"[LN Demo OC] player did start");
}

- (void)lnPlayerDidPlay:(LNVAPPlayerView *)playerView frame:(LNMP4AnimatedImageFrame *)frame {
    NSLog(@"[LN Demo OC] player did play frame=%ld", (long)frame.frameIndex);
}

- (void)lnPlayerDidFinish:(LNVAPPlayerView *)playerView totalFrameCount:(NSInteger)totalFrameCount {
    NSLog(@"[LN Demo OC] player did finish total=%ld", (long)totalFrameCount);
}

- (void)lnPlayerDidStop:(LNVAPPlayerView *)playerView {
    NSLog(@"[LN Demo OC] player did stop");
    dispatch_async(dispatch_get_main_queue(), ^{
        [playerView removeFromSuperview];
    });
}

- (void)lnPlayerDidFail:(LNVAPPlayerView *)playerView error:(NSError *)error {
    NSLog(@"[LN Demo OC] player did fail error=%@", error);
}

- (void)lnWrapViewDidStart:(LNVAPWrapView *)wrapView {
    NSLog(@"[LN Demo OC] wrap did start");
}

- (void)lnWrapViewDidPlay:(LNVAPWrapView *)wrapView frame:(LNMP4AnimatedImageFrame *)frame {
    NSLog(@"[LN Demo OC] wrap did play frame=%ld", (long)frame.frameIndex);
}

- (void)lnWrapViewDidFinish:(LNVAPWrapView *)wrapView totalFrameCount:(NSInteger)totalFrameCount {
    NSLog(@"[LN Demo OC] wrap did finish total=%ld", (long)totalFrameCount);
}

- (void)lnWrapViewDidStop:(LNVAPWrapView *)wrapView {
    NSLog(@"[LN Demo OC] wrap did stop");
    dispatch_async(dispatch_get_main_queue(), ^{
        [wrapView removeFromSuperview];
    });
}

- (void)lnWrapViewDidFail:(LNVAPWrapView *)wrapView error:(NSError *)error {
    NSLog(@"[LN Demo OC] wrap did fail error=%@", error);
}

@end
