//
//  NvPIPEditViewController.m
//  SDKDemo
//
//  Created by 刘东旭 on 2018/10/12.
 //
//

#import "NvPIPEditViewController.h"
#import "NvCompileViewController.h"
#import "NvPIPPreviewViewController.h"
#import "NvAlbumViewController.h"
#import "NvsLiveWindow.h"
#import "NvsVideoTrack.h"
#import "NvsVideoFx.h"

#import "MSSysAlertUtil.h"
#import "NvTimelineUtils.h"

#import "NvPIPThemeItem.h"

#import "NvCustomButton.h"
#import "NvPIPOperationView.h"
#import "NvPIPThemeView.h"
#import "NvTipsView.h"
#import "NvPIPRectView.h"
#import "NvPIPWindowView.h"
#import "NvCutVideoViewController.h"

@interface NvPIPEditViewController ()<NvCompileViewControllerDelegate, NvPIPRectViewDelegate>

@property (nonatomic, strong) NvsStreamingContext *streamingContext;
@property (nonatomic, strong) NvsTimeline *timeline;
@property (nonatomic, strong) NvsLiveWindow *liveWindow;
@property (nonatomic, strong) NSString *compileFilePath;
@property (nonatomic, strong) NvCustomButton *themeButton,*preViewButton;
@property (nonatomic, strong) NvPIPOperationView *opView;
@property (nonatomic, strong) NvPIPThemeView *pipThemeView;
@property (nonatomic, strong) NvPIPRectView *pipRectView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, assign) NvEditMode editMode;
//当前轨道
@property (nonatomic, strong) NvsVideoTrack *currentTrack;
//选择的模版
@property (nonatomic, assign) NSInteger selectIndex;
//第一个轨道音量按钮
@property (nonatomic, strong) UIButton *firstTrackButton;
//第二个轨道音量按钮
@property (nonatomic, strong) UIButton *secondTrackButton;

@end

@implementation NvPIPEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"画中画编辑";
    self.streamingContext = [NvsStreamingContext sharedInstanceWithFlags:NvsStreamingContextFlag_Support4KEdit];
    
    [self initTimeline:NvEditMode9v16];
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"生成" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnClicked)];
    [rightButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NvUtils fontWithSize:16], NSFontAttributeName, [UIColor nv_colorWithHexRGB:@"#4A90E2"], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    
    self.liveWindow = [[NvsLiveWindow alloc] initWithFrame:CGRectMake(64*SCREANSCALE, 0, SCREANWIDTH - 2*64*SCREANSCALE, (SCREANWIDTH - 2*64*SCREANSCALE) * 16.0/9)];
    [self.view addSubview:self.liveWindow];
    
    NvPIPWindowView *pipWindow = [[NvPIPWindowView alloc] initWithFrame:self.liveWindow.frame];
    pipWindow.delegate = self;
    [self.view addSubview:pipWindow];

    self.themeButton = [[NvCustomButton alloc] initWithFrame:CGRectMake(49*SCREANSCALE, SCREANHEIGHT- 44 - NV_STATUSBARHEIGHT - 80*SCREANSCALE - INDICATOR, 49*SCREANSCALE, 75*SCREANSCALE)];
    self.themeButton.image = [NvUtils imageWithName:@"pip-format"];
    self.themeButton.name = @"主题";
    [self.view addSubview:self.themeButton];
    [self.themeButton addTarget:self action:@selector(themeClick:) forControlEvents:UIControlEventTouchUpInside];
    self.themeButton.fontSize = 12;
    self.preViewButton = [[NvCustomButton alloc] initWithFrame:CGRectMake(SCREANWIDTH-2*49*SCREANSCALE, SCREANHEIGHT- 44 - NV_STATUSBARHEIGHT - 80*SCREANSCALE - INDICATOR, 49*SCREANSCALE, 75*SCREANSCALE)];
    self.preViewButton.image = [NvUtils imageWithName:@"pip-preview"];
    self.preViewButton.name = @"预览";
    [self.view addSubview:self.preViewButton];
    [self.preViewButton addTarget:self action:@selector(preViewClick:) forControlEvents:UIControlEventTouchUpInside];
    self.preViewButton.fontSize = 12;
    self.opView = [[NvPIPOperationView alloc] initWithFrame:CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 194*SCREANSCALE, 50*SCREANSCALE)];
    self.opView.delegate = self;
    self.opView.clipsToBounds = YES;
    [self.view addSubview:self.opView];
    self.opView.hidden = YES;
    
    self.pipRectView = [[NvPIPRectView alloc] initWithFrame:CGRectMake(0, 0, self.liveWindow.width, self.liveWindow.height/2)];
    self.pipRectView.delegate = self;
    [self.liveWindow addSubview:self.pipRectView];
    self.pipRectView.hidden = YES;
    
    self.pipThemeView = [[NvPIPThemeView alloc] initWithFrame:CGRectMake(0, SCREANHEIGHT-44 - NV_STATUSBARHEIGHT-138*SCREANSCALE, SCREANWIDTH, 138*SCREANSCALE)];
    [self.view addSubview:self.pipThemeView];
    self.pipThemeView.delegate = self;
    self.pipThemeView.hidden = YES;
    
    self.firstTrackButton = [UIButton nv_buttonWithTitle:nil textColor:nil fontSize:-1 image:[NvUtils imageWithName:@"volume_up - material"]];
    [self.firstTrackButton setImage:[NvUtils imageWithName:@"volume_off - material"] forState:UIControlStateSelected];
    self.firstTrackButton.frame = CGRectMake(11*SCREANSCALE, 15*SCREANSCALE, 25*SCREANSCALE, 25*SCREANSCALE);
    [self.firstTrackButton addTarget:self action:@selector(firstTrackButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [pipWindow addSubview:self.firstTrackButton];
    self.secondTrackButton = [UIButton nv_buttonWithTitle:nil textColor:nil fontSize:-1 image:[NvUtils imageWithName:@"volume_up - material"]];
    [self.secondTrackButton setImage:[NvUtils imageWithName:@"volume_off - material"]forState:UIControlStateSelected];
    self.secondTrackButton.frame = CGRectMake(11*SCREANSCALE, self.liveWindow.height/2+15*SCREANSCALE, 25*SCREANSCALE, 25*SCREANSCALE);
    [self.secondTrackButton addTarget:self action:@selector(secondTrackButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [pipWindow addSubview:self.secondTrackButton];
    
    //给NvPIPThemeView加载数据并刷新
    [self readData];
}
#pragma mark - 重新连接livewindow
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.streamingContext connectTimeline:self.timeline withLiveWindow:self.liveWindow];
    [self.streamingContext seekTimeline:self.timeline timestamp:0 videoSizeMode:NvsVideoPreviewSizeModeLiveWindowSize flags:0];
}

- (void)readData {
    self.dataSource = [NSMutableArray array];

    NSString *packagePath = [[[NSBundle bundleForClass:[self class]] bundlePath] stringByAppendingPathComponent:@"PIPPackage"];
    NSString *jsonPath = [packagePath stringByAppendingPathComponent:@"pipFileInfo.json"];
    NSString *jsontext = [NSString stringWithContentsOfFile:jsonPath encoding:NSUTF8StringEncoding error:nil];
    NSData *data =[jsontext dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *array =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    for (int i = 0; i < array.count; i++) {
        NSDictionary *dic = array[i];
        NvPIPThemeItem *item = [NvPIPThemeItem new];
        item.packageId1 = dic[@"pipPackage1"];
        item.packageId2 = dic[@"pipPackage2"];
        item.bundleImagePath = dic[@"bundleImagePath"];
        item.name = dic[@"name"];
        item.imageUrl = dic[@"imageName"];
        item.isSelect = NO;
        item.isInstall = YES;
        [self.dataSource addObject:item];
    }
    
    NvPIPThemeItem *item = self.dataSource.firstObject;
    item.isSelect = YES;
    
    NSFileManager *fm = [NSFileManager defaultManager];
    if (![fm fileExistsAtPath:PIPPACKAGE_PATH]) {
        [fm createDirectoryAtPath:PIPPACKAGE_PATH withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    NSString *jsonPath2 = [PIPPACKAGE_PATH stringByAppendingPathComponent:@"pipFileInfo.json"];
    NSString *jsontext2 = [NSString stringWithContentsOfFile:jsonPath2 encoding:NSUTF8StringEncoding error:nil];
    NSData *data2 =[jsontext2 dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *array2 =[NSJSONSerialization JSONObjectWithData:data2 options:0 error:nil];
    for (int i = 0; i < array2.count; i++) {
        NSDictionary *dic = array2[i];
        NvPIPThemeItem *item = [NvPIPThemeItem new];
        item.packageId1 = dic[@"pipPackage1"];
        item.packageId2 = dic[@"pipPackage2"];
        item.bundleImagePath = dic[@"bundleImagePath"];
        item.name = dic[@"name"];
        item.imageUrl = dic[@"imageName"];
        item.isSelect = NO;
        item.isInstall = YES;
        [self.dataSource addObject:item];
    }
        
    self.pipThemeView.dataSource = self.dataSource;
    [self nvPIPThemeView:self.pipThemeView applyTemplate:self.dataSource.firstObject];

    CGRect topRect = CGRectMake(0, 0, self.liveWindow.bounds.size.width, self.liveWindow.bounds.size.height/2);
    self.currentTrack = [self.timeline getVideoTrackByIndex:0];
    NvsVideoClip *clip = [self.currentTrack getClipWithIndex:0];
    if (clip.videoType == NvsVideoClipType_AV) {
        self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 194*SCREANSCALE, 50*SCREANSCALE);
    } else {
        self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 150*SCREANSCALE, 50*SCREANSCALE);
    }
    self.pipRectView.hidden = NO;
    self.pipRectView.frame = topRect;
    self.opView.hidden = NO;
    self.opView.bottom = self.pipRectView.bottom;
    self.opView.left = self.pipRectView.left;
}

- (void)themeClick:(NvCustomButton *)customButton {
    self.pipThemeView.hidden = NO;
}

- (void)preViewClick:(NvCustomButton *)customButton {
    NvPIPPreviewViewController *pipPreview = [NvPIPPreviewViewController new];
    pipPreview.timeline = self.timeline;
    pipPreview.editMode = self.editMode;
    [self.navigationController pushViewController:pipPreview animated:YES];
}

//返回按钮
- (UIView *)leftNavigationBarItemView {
    UIButton *backButton;
    backButton = [UIButton nv_buttonWithTitle:nil textColor:nil fontSize:-1 image:[NvUtils imageWithName:@"icon_back"]];
    backButton.frame = CGRectMake(0, 0, 30, 44);
    backButton.imageEdgeInsets = UIEdgeInsetsMake(0, -15*SCREANSCALE, 0, 0);
    __weak typeof(self)weakSelf = self;
    [backButton nv_BtnClickHandler:^{
        [weakSelf.navigationController popToRootViewControllerAnimated:YES];
    }];
    return backButton;
}
//生成按钮
- (void)rightBtnClicked {
    _compileFilePath = [VIDEO_PATH(@"Compile") stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4", [NvUtils currentDateAndTime]]];
    NvCompileViewController *compileViewController = [NvCompileViewController new];
    compileViewController.delegate = self;
    compileViewController.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [self presentViewController:compileViewController animated:NO completion:nil];
    [compileViewController compileTimeline:_timeline outputPath:_compileFilePath];
}
//添加数据
- (void)initTimeline:(NvEditMode)model {
    self.editMode = model;
    self.timeline = [NvTimelineUtils createTimeline:model];
    [self.timeline appendVideoTrack];
    
    __block BOOL isshowToast = NO;

    NvAlbumAsset * _Nonnull obj = self.selectAsset.firstObject;
    [self appendClip:obj ForTrack:[self.timeline getVideoTrackByIndex:0] containiCloud:&isshowToast];
    NvAlbumAsset * _Nonnull obj1 = self.selectAsset.lastObject;
    [self appendClip:obj1 ForTrack:[self.timeline getVideoTrackByIndex:1] containiCloud:&isshowToast];
    [self adjustTowTracks:[self.timeline getVideoTrackByIndex:0] otherVideoTrack:[self.timeline getVideoTrackByIndex:1]];
    if (isshowToast) {
        [MSSysAlertUtil presentAlertViewWithTitle:@"提示" message:@"所选资源在iCloud中" confirmTitle:@"确定" handler:NULL];
    } 
}
- (void)initTimeline:(NvEditMode)model withCurrentTimeline:(NvsTimeline *)timeline {
    NvsVideoClip *currentClip = [[timeline getVideoTrackByIndex:0] getClipWithIndex:0];
    NSString *currentClipPath = currentClip.filePath;
    
    NvsVideoClip *otherClip = [[timeline getVideoTrackByIndex:1] getClipWithIndex:0];
    NSString *otherClipPath = otherClip.filePath;
    
    self.editMode = model;
    [_streamingContext removeTimeline:self.timeline];
    self.timeline = [NvTimelineUtils createTimeline:model];
    [self.timeline appendVideoTrack];
    NvsVideoTrack *firstTrack = [self.timeline getVideoTrackByIndex:0];
    NvsVideoTrack *secondTrack = [self.timeline getVideoTrackByIndex:1];
    NvsVideoClip *firstClip = [firstTrack appendClip:currentClipPath];
    firstClip.imageMotionAnimationEnabled = NO;
    NvsVideoClip *secondClip = [secondTrack appendClip:otherClipPath];
    secondClip.imageMotionAnimationEnabled = NO;
}
#pragma mark - 添加clip
- (void)appendClip:(NvAlbumAsset *)obj ForTrack:(NvsVideoTrack *)track containiCloud:(BOOL *)contain {
    if (obj.asset.mediaType == PHAssetMediaTypeVideo) {
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
        PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
        options.version = PHVideoRequestOptionsVersionOriginal;
        options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
        [[PHImageManager defaultManager] requestAVAssetForVideo:obj.asset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
            if (asset && [asset isKindOfClass:[AVURLAsset class]]) {
                NvsVideoClip *clip = [track appendClip:obj.asset.localIdentifier];
                [clip appendBuiltinFx:@"Transform 2D"];
            } else {
                *contain = YES;
            }
            
            dispatch_semaphore_signal(semaphore);
        }];
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    } else {
        PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
        options.synchronous = YES;
        options.resizeMode   = PHImageRequestOptionsResizeModeExact;
        options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
        [[PHImageManager defaultManager] requestImageForAsset:obj.asset targetSize:CGSizeMake(80, 80) contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            BOOL isIcloud =  [[info valueForKeyPath:@"PHImageResultIsInCloudKey"] boolValue];
            if (isIcloud) {
                *contain = YES;
            } else {
                NvsVideoClip *clip = [track appendClip:obj.asset.localIdentifier];
                clip.imageMotionAnimationEnabled = NO;
                [clip appendBuiltinFx:@"Transform 2D"];
            }
        }];
    }
}

#pragma mark 裁剪视频后调整对齐两个视频轨道
- (void)adjustTowTracks:(NvsVideoTrack *)currentVideoTrack otherVideoTrack:(NvsVideoTrack *)secondVideoTrack {
    NvsVideoClip *currentClip = [currentVideoTrack getClipWithIndex:0];
    NvsVideoClip *otherClip = [secondVideoTrack getClipWithIndex:0];
    NSString *currentClipFilePath = currentClip.filePath;
    NSString *otherClipFilePath = otherClip.filePath;
    //先把每个轨道上多余的片段去除掉
    for (int i = 1; i < currentVideoTrack.clipCount; ++i) {
        NvsVideoClip *clip = [currentVideoTrack getClipWithIndex:i];
        [currentVideoTrack removeClip:clip.index keepSpace:NO];
        --i;
    }
    
    for (int i = 1; i < secondVideoTrack.clipCount; ++i) {
        NvsVideoClip *clip = [secondVideoTrack getClipWithIndex:i];
        [secondVideoTrack removeClip:clip.index keepSpace:NO];
        --i;
    }
    
    //比较两个视频轨道的duration进行补齐
    if (currentVideoTrack.duration > secondVideoTrack.duration) {
        int64_t a = currentVideoTrack.duration/secondVideoTrack.duration;
        for (int i = 1; i < a; ++i) {
            NvsVideoClip *clip = [secondVideoTrack appendClip:otherClipFilePath trimIn:otherClip.trimIn trimOut:otherClip.trimOut];
            clip.imageMotionAnimationEnabled = NO;
        }
        int64_t lastDuration = currentVideoTrack.duration - secondVideoTrack.duration;
        NvsVideoClip *clip = [secondVideoTrack appendClip:otherClipFilePath trimIn:otherClip.trimIn trimOut:otherClip.trimIn + lastDuration];
        clip.imageMotionAnimationEnabled = NO;
    } else {
        int64_t a = secondVideoTrack.duration/currentVideoTrack.duration;
        for (int i = 1; i < a; ++i) {
            NvsVideoClip *clip = [currentVideoTrack appendClip:currentClipFilePath trimIn:currentClip.trimIn trimOut:currentClip.trimOut];
            clip.imageMotionAnimationEnabled = NO;
        }
        int64_t lastDuration = secondVideoTrack.duration - currentVideoTrack.duration;
        NvsVideoClip *clip = [currentVideoTrack appendClip:currentClipFilePath trimIn:currentClip.trimIn trimOut:currentClip.trimIn + lastDuration];
        clip.imageMotionAnimationEnabled = NO;
    }
    
    //应用模版特效
    NSString *fxIdFirst = (NSString *)[currentVideoTrack getAttachment:@"fxId"];
    for (int i = 1; i < currentVideoTrack.clipCount; i++) {
        NvsVideoClip *clip = [currentVideoTrack getClipWithIndex:i];
        
        NvsVideoFx *fx = [self getTransform2DWithClip:clip];
        if (!fx) {
            [clip appendBuiltinFx:@"Transform 2D"];
        }
        
        [clip appendPackagedFx:fxIdFirst];
    }
    
    NSString *fxIdSecond = (NSString *)[secondVideoTrack getAttachment:@"fxId"];
    for (int i = 1; i < secondVideoTrack.clipCount; i++) {
        NvsVideoClip *clip = [secondVideoTrack getClipWithIndex:i];
        
        NvsVideoFx *fx = [self getTransform2DWithClip:clip];
        if (!fx) {
            [clip appendBuiltinFx:@"Transform 2D"];
        }
        
        [clip appendPackagedFx:fxIdSecond];
    }
    
    //应用Transform数据
    NvsVideoFx *currentfx = [self getTransform2DWithClip:currentClip];
    for (int i = 1; i < currentVideoTrack.clipCount; ++i) {
        NvsVideoClip *clip = [currentVideoTrack getClipWithIndex:i];
        NvsVideoFx *fx = [self getTransform2DWithClip:clip];
        float transx = [currentfx getFloatVal:@"Trans X"];
        float transy = [currentfx getFloatVal:@"Trans Y"];
        [fx setFloatVal:@"Trans X" val:transx];
        [fx setFloatVal:@"Trans Y" val:transy];
        
        float scaleX = [currentfx getFloatVal:@"Scale X"];
        [fx setFloatVal:@"Scale X" val:scaleX];
        [fx setFloatVal:@"Scale Y" val:scaleX];
        
        float rotation = [currentfx getFloatVal:@"Rotation"];
        [fx setFloatVal:@"Rotation" val:rotation];
    }
    
    NvsVideoFx *otherfx = [self getTransform2DWithClip:otherClip];
    for (int i = 1; i < secondVideoTrack.clipCount; ++i) {
        NvsVideoClip *clip = [secondVideoTrack getClipWithIndex:i];
        NvsVideoFx *fx = [self getTransform2DWithClip:clip];
        float transx = [otherfx getFloatVal:@"Trans X"];
        float transy = [otherfx getFloatVal:@"Trans Y"];
        [fx setFloatVal:@"Trans X" val:transx];
        [fx setFloatVal:@"Trans Y" val:transy];
        
        float scaleX = [otherfx getFloatVal:@"Scale X"];
        [fx setFloatVal:@"Scale X" val:scaleX];
        [fx setFloatVal:@"Scale Y" val:scaleX];
        
        float rotation = [otherfx getFloatVal:@"Rotation"];
        [fx setFloatVal:@"Rotation" val:rotation];
    }
}

#pragma mark - 第一个音量按钮回调
- (void)firstTrackButtonClick:(UIButton *)button {
    NvsVideoTrack *firstTrack = [self.timeline getVideoTrackByIndex:0];
    if (button.selected) {
        [firstTrack setVolumeGain:1 rightVolumeGain:1];
    } else {
        [firstTrack setVolumeGain:0 rightVolumeGain:0];
    }
    
    button.selected = !button.selected;
}
#pragma mark - 第二个音量按钮回调
- (void)secondTrackButtonClick:(UIButton *)button {
    NvsVideoTrack *secondTrack = [self.timeline getVideoTrackByIndex:1];
    if (button.selected) {
        [secondTrack setVolumeGain:1 rightVolumeGain:1];
    } else {
        [secondTrack setVolumeGain:0 rightVolumeGain:0];
    }
    
    button.selected = !button.selected;
}

- (void)rectView:(NvPIPWindowView *)rectView touchBeganPoint:(CGPoint)point {
    //根据当前模式获取点击时的track
    if (self.selectIndex == 0) {
        CGRect topRect = CGRectMake(0, 0, self.liveWindow.bounds.size.width, self.liveWindow.bounds.size.height/2);
        CGRect bottomRect = CGRectMake(0, self.liveWindow.bounds.size.height/2, self.liveWindow.bounds.size.width, self.liveWindow.bounds.size.height/2);
        if (CGRectContainsPoint(topRect, point)) {
            self.currentTrack = [self.timeline getVideoTrackByIndex:0];
            NvsVideoClip *clip = [self.currentTrack getClipWithIndex:0];
            if (clip.videoType == NvsVideoClipType_AV) {
                self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 194*SCREANSCALE, 50*SCREANSCALE);
            } else {
                self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 150*SCREANSCALE, 50*SCREANSCALE);
            }
            self.pipRectView.hidden = NO;
            self.pipRectView.frame = topRect;
            self.opView.hidden = NO;
            self.opView.bottom = self.pipRectView.bottom;
            self.opView.left = self.pipRectView.left;
        } else if (CGRectContainsPoint(bottomRect, point)) {
            self.currentTrack = [self.timeline getVideoTrackByIndex:1];
            NvsVideoClip *clip = [self.currentTrack getClipWithIndex:0];
            if (clip.videoType == NvsVideoClipType_AV) {
                self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 194*SCREANSCALE, 50*SCREANSCALE);
            } else {
                self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 150*SCREANSCALE, 50*SCREANSCALE);
            }
            self.pipRectView.hidden = NO;
            self.pipRectView.frame = bottomRect;
            self.opView.hidden = NO;
            self.opView.bottom = self.pipRectView.bottom;
            self.opView.left = self.pipRectView.left;
        }
    } else if (self.selectIndex == 1) {
        CGRect leftRect = CGRectMake(0, 0, self.liveWindow.bounds.size.width/2, self.liveWindow.bounds.size.height);
        CGRect rightRect = CGRectMake(self.liveWindow.bounds.size.width/2, 0, self.liveWindow.bounds.size.width, self.liveWindow.bounds.size.height);
        if (CGRectContainsPoint(leftRect, point)) {
            self.currentTrack = [self.timeline getVideoTrackByIndex:0];
            NvsVideoClip *clip = [self.currentTrack getClipWithIndex:0];
            if (clip.videoType == NvsVideoClipType_AV) {
                self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 194*SCREANSCALE, 50*SCREANSCALE);
            } else {
                self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 150*SCREANSCALE, 50*SCREANSCALE);
            }
            self.pipRectView.hidden = NO;
            self.pipRectView.frame = leftRect;
            self.opView.hidden = NO;
            self.opView.bottom = self.pipRectView.bottom;
            self.opView.left = self.pipRectView.left;
        } else if (CGRectContainsPoint(rightRect, point)) {
            self.currentTrack = [self.timeline getVideoTrackByIndex:1];
            NvsVideoClip *clip = [self.currentTrack getClipWithIndex:0];
            if (clip.videoType == NvsVideoClipType_AV) {
                self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 194*SCREANSCALE, 50*SCREANSCALE);
            } else {
                self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 150*SCREANSCALE, 50*SCREANSCALE);
            }
            self.pipRectView.hidden = NO;
            self.pipRectView.frame = rightRect;
            self.opView.hidden = NO;
            self.opView.bottom = self.pipRectView.bottom;
            self.opView.left = self.pipRectView.left;
        }
    } else if (self.selectIndex == 2) {
        CGRect leftTopRect = CGRectMake(0, 0, self.liveWindow.bounds.size.width*0.3, self.liveWindow.bounds.size.height*0.3);
        CGRect allRect = CGRectMake(0, 0, self.liveWindow.bounds.size.width, self.liveWindow.bounds.size.height);
        if (CGRectContainsPoint(leftTopRect, point)) {
            self.currentTrack = [self.timeline getVideoTrackByIndex:1];
            NvsVideoClip *clip = [self.currentTrack getClipWithIndex:0];
            if (clip.videoType == NvsVideoClipType_AV) {
                self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 194*SCREANSCALE, 50*SCREANSCALE);
            } else {
                self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 150*SCREANSCALE, 50*SCREANSCALE);
            }
            self.pipRectView.hidden = NO;
            self.pipRectView.frame = leftTopRect;
            self.opView.hidden = NO;
            self.opView.bottom = self.pipRectView.bottom;
            self.opView.left = self.pipRectView.left;
        } else if (CGRectContainsPoint(allRect, point)) {
            self.currentTrack = [self.timeline getVideoTrackByIndex:0];
            NvsVideoClip *clip = [self.currentTrack getClipWithIndex:0];
            if (clip.videoType == NvsVideoClipType_AV) {
                self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 194*SCREANSCALE, 50*SCREANSCALE);
            } else {
                self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 150*SCREANSCALE, 50*SCREANSCALE);
            }
            self.pipRectView.hidden = NO;
            self.pipRectView.frame = allRect;
            self.opView.hidden = NO;
            self.opView.bottom = self.pipRectView.bottom;
            self.opView.left = self.pipRectView.left;
        }
    } else if (self.selectIndex == 3) {
        CGRect leftTopRect = CGRectMake(12*SCREANSCALE, 6, self.liveWindow.bounds.size.width/2, self.liveWindow.bounds.size.height/2);
        CGRect rightBottomRect = CGRectMake(self.liveWindow.bounds.size.width/2-12*SCREANSCALE, self.liveWindow.bounds.size.height/2-6, self.liveWindow.bounds.size.width/2, self.liveWindow.bounds.size.height/2);
        if (CGRectContainsPoint(leftTopRect, point)) {
            self.currentTrack = [self.timeline getVideoTrackByIndex:0];
            NvsVideoClip *clip = [self.currentTrack getClipWithIndex:0];
            if (clip.videoType == NvsVideoClipType_AV) {
                self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 194*SCREANSCALE, 50*SCREANSCALE);
            } else {
                self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 150*SCREANSCALE, 50*SCREANSCALE);
            }
            self.pipRectView.hidden = NO;
            self.pipRectView.frame = leftTopRect;
            self.opView.hidden = NO;
            self.opView.bottom = self.pipRectView.bottom;
            self.opView.left = self.pipRectView.left;
        } else if (CGRectContainsPoint(rightBottomRect, point)) {
            self.currentTrack = [self.timeline getVideoTrackByIndex:1];
            NvsVideoClip *clip = [self.currentTrack getClipWithIndex:0];
            if (clip.videoType == NvsVideoClipType_AV) {
                self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 194*SCREANSCALE, 50*SCREANSCALE);
            } else {
                self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 150*SCREANSCALE, 50*SCREANSCALE);
            }
            self.pipRectView.hidden = NO;
            self.pipRectView.frame = rightBottomRect;
            self.opView.hidden = NO;
            self.opView.bottom = self.pipRectView.bottom;
            self.opView.left = self.pipRectView.left;
        }
    }
}

- (void)replace {
    NvAlbumViewController *album = [NvAlbumViewController new];
    album.delegate = self;
    album.mutableSelect = NO;
    album.hiddenSelectAll = YES;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:album];
    [self presentViewController:nav animated:YES completion:NULL];
}

- (void)zoomIn {
    NvsVideoTrack *track = self.currentTrack;
    [self zoomInWith:track scale:1.25];
}

- (void)zoomOut {
    NvsVideoTrack *track = self.currentTrack;
    [self zoomOutWith:track scale:0.8];
}

- (void)rotate {
    NvsVideoTrack *track = self.currentTrack;
    [self rotationWith:track rotation:90];
}

- (void)cutVideo {
    NSLog(@"cutVideo");
    NvsVideoClip *clip = [self.currentTrack getClipWithIndex:0];
    NvCutVideoViewController *trimVideoVC = [[NvCutVideoViewController alloc] init];
    trimVideoVC.delegate = self;
    NvsTimeline *timeline = [NvTimelineUtils createTimeline:NvEditMode9v16];
    NvsVideoTrack *videoTrack = [timeline getVideoTrackByIndex:0];
    trimVideoVC.editMode = NvEditMode9v16;
    NvsVideoClip *trimVideoClip = [videoTrack appendClip:clip.filePath];
    trimVideoClip.imageMotionAnimationEnabled = NO;
    trimVideoVC.timeline = timeline;
    trimVideoVC.trimIn = clip.trimIn;
    trimVideoVC.trimOut = clip.trimOut;

    [self.navigationController pushViewController:trimVideoVC animated:YES];
}

- (void)zoomInWith:(NvsVideoTrack *)track scale:(float)scale {
    for (int i = 0; i < track.clipCount; i++) {
        NvsVideoClip *clip = [track getClipWithIndex:i];
        NvsVideoFx *fx = [self getTransform2DWithClip:clip];
        if (!fx) {
            fx = [clip appendBuiltinFx:@"Transform 2D"];
        }
        float scale1 = [fx getFloatVal:@"Scale X"];
        [fx setFloatVal:@"Scale X" val:scale1*scale];
        [fx setFloatVal:@"Scale Y" val:scale1*scale];
    }
    [self.streamingContext seekTimeline:self.timeline timestamp:0 videoSizeMode:NvsVideoPreviewSizeModeLiveWindowSize flags:0];
}

- (void)zoomOutWith:(NvsVideoTrack *)track scale:(float)scale {
    for (int i = 0; i < track.clipCount; i++) {
        NvsVideoClip *clip = [track getClipWithIndex:i];
        NvsVideoFx *fx = [self getTransform2DWithClip:clip];
        if (!fx) {
            fx = [clip appendBuiltinFx:@"Transform 2D"];
        }
        
        float scale1 = [fx getFloatVal:@"Scale X"];
        if (scale1 <= 1) {
            scale1 = 1;
            [fx setFloatVal:@"Scale X" val:scale1];
            [fx setFloatVal:@"Scale Y" val:scale1];
        } else {
            [fx setFloatVal:@"Scale X" val:scale1*scale];
            [fx setFloatVal:@"Scale Y" val:scale1*scale];
        }
    }
    [self.streamingContext seekTimeline:self.timeline timestamp:0 videoSizeMode:NvsVideoPreviewSizeModeLiveWindowSize flags:0];
}

- (void)rotationWith:(NvsVideoTrack *)track rotation:(float)rotation {
    for (int i = 0; i < track.clipCount; i++) {
        NvsVideoClip *clip = [track getClipWithIndex:i];
        NvsVideoFx *fx = [self getTransform2DWithClip:clip];
        if (!fx) {
            fx = [clip appendBuiltinFx:@"Transform 2D"];
        }
        
        float rotation1 = [fx getFloatVal:@"Rotation"];
        rotation1 = rotation1-rotation;
        if (rotation1 <= -360) {
            rotation1 = rotation1 + 360;
        }
        [fx setFloatVal:@"Rotation" val:rotation1];
    }
    [self.streamingContext seekTimeline:self.timeline timestamp:0 videoSizeMode:NvsVideoPreviewSizeModeLiveWindowSize flags:0];
}

- (NvsVideoFx *)getTransform2DWithClip:(NvsVideoClip *)clip {
    for(int i = 0;i<clip.fxCount;i++) {
        NvsVideoFx *fx = [clip getFxWithIndex:i];
        if ([fx.bultinVideoFxName isEqualToString:@"Transform 2D"] && fx.videoFxType == NvsVideoFxType_Builtin) {
            return fx;
        }
    }
    return nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark 裁剪视频回调
- (void)cutVideoViewController:(NvCutVideoViewController *)cutVideoViewController trimIn:(int64_t)trimIn trimOut:(int64_t)trimOut {
    NvsVideoClip *clip = [self.currentTrack getClipWithIndex:0];
    [clip changeTrimInPoint:trimIn affectSibling:YES];
    [clip changeTrimOutPoint:trimOut affectSibling:YES];
    NvsVideoTrack *track = [self.timeline getVideoTrackByIndex:0];
    NvsVideoTrack *secondTrack;
    if (track == self.currentTrack) {
        secondTrack = [self.timeline getVideoTrackByIndex:1];
    } else {
        secondTrack = [self.timeline getVideoTrackByIndex:0];
        track = [self.timeline getVideoTrackByIndex:1];
    }
    [self adjustTowTracks:track otherVideoTrack:secondTrack];
    
    [self.streamingContext seekTimeline:self.timeline timestamp:0 videoSizeMode:NvsVideoPreviewSizeModeLiveWindowSize flags:0];
}

#pragma mark - NvPIPRectView回调
- (void)rectView:(NvPIPRectView*)rectView currentPoint:(CGPoint)currentPoint previousPoint:(CGPoint)previousPoint {
    CGPoint p1 = [self.liveWindow mapViewToCanonical:currentPoint];
    CGPoint p2 = [self.liveWindow mapViewToCanonical:previousPoint];
    for (int i = 0; i < self.currentTrack.clipCount; i++) {
        NvsVideoClip *clip = [self.currentTrack getClipWithIndex:i];
        NvsVideoFx *fx = [self getTransform2DWithClip:clip];
        float transx = [fx getFloatVal:@"Trans X"];
        float transy = [fx getFloatVal:@"Trans Y"];
        [fx setFloatVal:@"Trans X" val:transx+(p1.x-p2.x)];
        [fx setFloatVal:@"Trans Y" val:transy+(p1.y-p2.y)];
    }
    
    [self.streamingContext seekTimeline:self.timeline timestamp:0 videoSizeMode:NvsVideoPreviewSizeModeLiveWindowSize flags:0];
}

#pragma mark - 相册回调
- (void)nvAlbumViewController:(NvAlbumViewController *)albumViewController selectAlbumAssets:(NSMutableArray <NvAlbumAsset *>*)assets {
    [self dismissViewControllerAnimated:YES completion:NULL];
    NvAlbumAsset *asset = assets.firstObject;
    
//    两个轨道都先移除后添加
    [self.currentTrack removeAllClips];
    
    BOOL isshowToast = NO;
    [self appendClip:asset ForTrack:self.currentTrack containiCloud:&isshowToast];
    NvsVideoClip *clip = [self.currentTrack getClipWithIndex:0];
    NSString *fxIdFirst = (NSString *)[self.currentTrack getAttachment:@"fxId"];
    [clip appendPackagedFx:fxIdFirst];
    
    [self adjustTowTracks:[self.timeline getVideoTrackByIndex:0] otherVideoTrack:[self.timeline getVideoTrackByIndex:1]];
    
    if (clip.videoType == NvsVideoClipType_AV) {
        self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 194*SCREANSCALE, 50*SCREANSCALE);
    } else {
        self.opView.frame = CGRectMake(100*SCREANSCALE, 100*SCREANSCALE, 150*SCREANSCALE, 50*SCREANSCALE);
    }
    
    [self.streamingContext seekTimeline:self.timeline timestamp:0 videoSizeMode:NvsVideoPreviewSizeModeLiveWindowSize flags:0];
}

- (void)nvAlbumViewControllerCancelClick:(NvAlbumViewController *)albumViewController {
    [albumViewController dismissViewControllerAnimated:YES completion:NULL];
}

#pragma mark - NvPIPThemeViewDelegate
- (void)nvPIPThemeViewOkClick:(NvPIPThemeView *)pipThemeView {
    self.pipThemeView.hidden = YES;
}
- (void)nvPIPThemeView:(NvPIPThemeView *)pipThemeView applyTemplate:(NvPIPThemeItem *)item {
    self.selectIndex = [self.dataSource indexOfObject:item];
    
    //隐藏矩形框和操作面板
    self.pipRectView.hidden = YES;
    self.opView.hidden = YES;
    
    if ([item.name  isEqualToString:@"上下"]) {
        [self initTimeline:NvEditMode9v16 withCurrentTimeline:self.timeline];
        self.liveWindow.frame = CGRectMake(64*SCREANSCALE, 0, SCREANWIDTH - 2*64*SCREANSCALE, (SCREANWIDTH - 2*64*SCREANSCALE) * 16.0/9);
    } else if ([item.name isEqualToString:@"左右"]) {
        [self initTimeline:NvEditMode1v1 withCurrentTimeline:self.timeline];
        self.liveWindow.frame = CGRectMake(0, 0, SCREANWIDTH, SCREANWIDTH);
    } else {
        [self initTimeline:NvEditMode9v16 withCurrentTimeline:self.timeline];
        self.liveWindow.frame = CGRectMake(64*SCREANSCALE, 0, SCREANWIDTH - 2*64*SCREANSCALE, (SCREANWIDTH - 2*64*SCREANSCALE) * 16.0/9);
    }
    [self adjustTowTracks:[self.timeline getVideoTrackByIndex:0] otherVideoTrack:[self.timeline getVideoTrackByIndex:1]];
    //根据被选择的index更新音量按钮坐标
    if (self.selectIndex == 0) {
        self.firstTrackButton.frame = CGRectMake(11*SCREANSCALE, 15*SCREANSCALE, 25*SCREANSCALE, 25*SCREANSCALE);
        self.secondTrackButton.frame = CGRectMake(11*SCREANSCALE, self.liveWindow.height/2+15*SCREANSCALE, 25*SCREANSCALE, 25*SCREANSCALE);
    } else if (self.selectIndex == 1) {
        self.firstTrackButton.frame = CGRectMake(11*SCREANSCALE, 15*SCREANSCALE, 25*SCREANSCALE, 25*SCREANSCALE);
        self.secondTrackButton.frame = CGRectMake(self.liveWindow.width/2 + 11*SCREANSCALE, 15*SCREANSCALE, 25*SCREANSCALE, 25*SCREANSCALE);
    } else if (self.selectIndex == 2) {
        self.firstTrackButton.frame = CGRectMake(self.liveWindow.width/2 + 11*SCREANSCALE, 15*SCREANSCALE, 25*SCREANSCALE, 25*SCREANSCALE);
        self.secondTrackButton.frame = CGRectMake(11*SCREANSCALE, 15*SCREANSCALE, 25*SCREANSCALE, 25*SCREANSCALE);
    } else if (self.selectIndex == 3) {
        self.firstTrackButton.frame = CGRectMake(self.liveWindow.width/2 + 11*SCREANSCALE, self.liveWindow.height/2 + 15*SCREANSCALE, 25*SCREANSCALE, 25*SCREANSCALE);
        self.secondTrackButton.frame = CGRectMake(11*SCREANSCALE, 15*SCREANSCALE, 25*SCREANSCALE, 25*SCREANSCALE);
    }
    //更新音量按钮状态
    NvsVideoTrack *firstTrack = [self.timeline getVideoTrackByIndex:0];
    NvsVideoTrack *secondTrack = [self.timeline getVideoTrackByIndex:1];
    float firstTrackValum = 0;
    float secondTrackValum = 0;
    [firstTrack getVolumeGain:&firstTrackValum rightVolumeGain:&firstTrackValum];
    [secondTrack getVolumeGain:&secondTrackValum rightVolumeGain:&secondTrackValum];
    self.firstTrackButton.selected = firstTrackValum>0?NO:YES;
    self.secondTrackButton.selected = secondTrackValum>0?NO:YES;
    
    [self.streamingContext connectTimeline:self.timeline withLiveWindow:self.liveWindow];
    [self.streamingContext seekTimeline:self.timeline timestamp:0 videoSizeMode:NvsVideoPreviewSizeModeLiveWindowSize flags:0];
    
    //轨道2在轨道1上面
    NvsVideoTrack *firstVideoTrack = [self.timeline getVideoTrackByIndex:0];
    NvsVideoTrack *lastVideoTrack = [self.timeline getVideoTrackByIndex:1];
    for (int i = 0; i < firstVideoTrack.clipCount; i++) {
        NvsVideoClip *clip1 = [firstVideoTrack getClipWithIndex:i];
        [clip1 removeAllFx];
        [clip1 appendBuiltinFx:@"Transform 2D"];
        [clip1 appendPackagedFx:item.packageId1];
    }
    [firstVideoTrack setAttachment:item.packageId1 forKey:@"fxId"];
    
    for (int i = 0; i < lastVideoTrack.clipCount; i++) {
        NvsVideoClip *clip2 = [lastVideoTrack getClipWithIndex:i];
        [clip2 removeAllFx];
        [clip2 appendBuiltinFx:@"Transform 2D"];
        [clip2 appendPackagedFx:item.packageId2];
    }
    [lastVideoTrack setAttachment:item.packageId2 forKey:@"fxId"];
    
    
    [self.streamingContext seekTimeline:self.timeline timestamp:0 videoSizeMode:NvsVideoPreviewSizeModeLiveWindowSize flags:0];
}

#pragma mark - NvCompileViewControllerDelegate
- (void)compileFinished:(BOOL)needDelete {
    if (needDelete) {
        [[NSFileManager defaultManager] removeItemAtPath:_compileFilePath error:nil];
    } else {
        UISaveVideoAtPathToSavedPhotosAlbum(_compileFilePath, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
    }
}

//保存相册的回调
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NvTipsView *tip = [[NvTipsView alloc]initWithFrame:self.view.frame withTitle:@"保存成功！" withColor:[UIColor nv_colorWithHexRGB:@"#4D4F51"] withCenter:NO];
    [self.view addSubview:tip];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [tip removeFromSuperview];
    });
    //    [MSSysAlertUtil presentAlertViewWithTitle:@"提示" message:@"视频已保存至相册" confirmTitle:@"确定" handler:NULL];
    
}

@end
