//
//  NvEditPictureViewController.m
//  SDKDemo
//
//  Created by ms20180425 on 2018/7/21.
 //
//

#import "NvEditPictureViewController.h"
#import "NvEditClipLiveWindow.h"
#import "NvTipsView.h"
#import "NvsVideoFx.h"
#import "NvsVideoTrack.h"
#import "NvsAudioClip.h"
#import "NvTimelineImageUtils.h"
#import "NvSwitchView.h"
#import "EditPictureDragView.h"
#import "NvAlbumViewController.h"

@interface NvEditPictureViewController ()<EditPictureDragViewDelegate>

//所有模块统一控件
@property (nonatomic, strong) NvEditClipLiveWindow *clipLivewindow;     //播放控件
@property (nonatomic, strong) UIButton *finshBtn;                       //完成按钮

//时长模块
@property (nonatomic, strong) UILabel *timeLabel;        //调节之后的时间

//运动模块
@property (nonatomic, strong) UIButton *areaBtn;         //区域显示按钮
@property (nonatomic, strong) UIView *areaView;          //区域显示按钮选中蒙层
@property (nonatomic, strong) UIButton *fullBtn;         //全图显示按钮
@property (nonatomic, strong) UIView *fullView;          //全图显示按钮选中蒙层
@property (nonatomic, strong) UIImageView *currentImageView;    //当前预览图片
@property (nonatomic, strong) NvSwitchView *switchView;   //画面运动
@property (nonatomic, strong) UIView *previewView;        //预览按钮点击之后弹出的视图
@property (nonatomic, strong) EditPictureDragView *startDragView; //开始拖动框
@property (nonatomic, strong) EditPictureDragView *endDragView;   //结束拖动框
@property (nonatomic, strong) EditPictureDragView *areaDragView;  //区域拖动框
@property (nonatomic, assign) CGFloat imageWidth;    //图片宽
@property (nonatomic, assign) CGFloat imageHeight;    //图片高
@property (nonatomic, strong) UIImage *currentImage;   //当前图片
@property (nonatomic, assign) BOOL movementState;           //是否运动

//-----------------sdk相关----------------//
@property (nonatomic, strong) NvsStreamingContext *streamingContext;    //流媒体上下文
@property (nonatomic, strong) NvTimelineData *timelineData;             //数据结构
@property (nonatomic, strong) NvsTimeline *currentTimeline;             //根据当前片段创建timeline，并操作
@property (nonatomic, strong) NvEditDataModel *currentDataModel;        //copy一份新的model，保存起来
@property (nonatomic, strong) NvsVideoClip * videoClip;                 //当前片段
@end

@implementation NvEditPictureViewController{
    NvsRect startRectImage;      //开始运动的位置
    NvsRect endRectImage;      //结束运动的位置
    NvsRect areaRectImage;     //区域位置
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [NvTimelineUtils removeTimeline:self.currentTimeline];
    NSLog(@"%s",__func__);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.streamingContext = [NvsStreamingContext sharedInstance];
    self.currentTimeline = [NvTimelineUtils createTimeline:self.editMode];
    self.timelineData = [NvTimelineData sharedInstance];
    [NvTimelineUtils resetEditData:self.currentTimeline editDataArray:[NSArray arrayWithObject:self.model]];
    self.videoClip = [[self.currentTimeline getVideoTrackByIndex:0] getClipWithIndex:0];
    self.currentDataModel = [self.model copy];
    self.movementState = self.model.hasMotion;
    
    [self addSubViews];
    self.areaBtn.selected = self.model.isArea;
    self.fullBtn.selected = !self.model.isArea;
    self.areaView.hidden = !self.areaBtn.selected;
    self.fullView.hidden = !self.fullBtn.selected;
    [self configDragView];
    // Do any additional setup after loading the view.
}

#pragma mark 添加子视图
- (void)addSubViews{
    if ([self.title isEqualToString:@"时长"]) {
        [self addTimeModuleView];
    }else if ([self.title isEqualToString:@"运动"]){
        if (self.model.isPhotoAlbum) {
            PHFetchResult<PHAsset *> *phresult = [PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObject:self.model.localIdentifier] options:nil];
            PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];
            requestOptions.synchronous = YES;
            PHAsset *asset = [phresult objectAtIndex:0];
            [[PHImageManager defaultManager] requestImageForAsset:asset
                                                       targetSize:CGSizeMake(asset.pixelWidth, asset.pixelHeight)
                                                      contentMode:PHImageContentModeAspectFit
                                                          options:requestOptions
                                                    resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
                                                        self.currentImage= result;
                                                        [self adapterWidthAndHeight];
                                                        [self addMovementModelView];
                                                        [self addDragView];
                                                    }];
        }else{
            self.currentImage = [UIImage imageWithContentsOfFile:self.model.localIdentifier];
            [self adapterWidthAndHeight];
            [self addMovementModelView];
            [self addDragView];
        }
    }
    
    
    UIButton *finshBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [finshBtn setImage:[NvUtils imageWithName:@"Nvcheck - material"] forState:UIControlStateNormal];
    [finshBtn addTarget:self action:@selector(finshClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:finshBtn];
    [finshBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        if (KIsiPhoneX) {
            make.bottom.equalTo(self.view).offset(-39 * SCREANSCALE);
        }else{
            make.bottom.equalTo(self.view).offset(-15 * SCREANSCALE);
        }
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    if ([self.title isEqualToString:@"运动"]) {
        [self addPreviewView];
    }
    
    UIView *line = [UIView new];
    line.backgroundColor = [UIColor nv_colorWithHexARGB:@"#1AFFFFFF"];
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.height.equalTo(@1);
        make.bottom.equalTo(finshBtn.mas_top).offset(-12*SCREANSCALE);
    }];
}

#pragma mark 时长模块控件
- (void)addTimeModuleView{
    self.clipLivewindow = [[NvEditClipLiveWindow alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.width)];
    [self.view addSubview:self.clipLivewindow];
    [self.clipLivewindow connectTimeline:self.currentTimeline];
    self.clipLivewindow.editMode = self.editMode;
    [self.clipLivewindow seekTimeline:0];
    [self.clipLivewindow setPlayRangeIn:0 rangeOut:self.currentTimeline.duration];
    [self.clipLivewindow play];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.textColor = UIColor.whiteColor;
    titleLabel.text = @"拖动滑块调整时长";
    titleLabel.font = [NvUtils fontWithSize:12 * SCREANSCALE];
    
    UISlider * timeLengSlider = [UISlider new];
    [timeLengSlider setMinimumValue:1];
    [timeLengSlider setMaximumValue:10];
    timeLengSlider.minimumTrackTintColor = [UIColor nv_colorWithHexRGB:@"#979797"];
    timeLengSlider.maximumTrackTintColor = [UIColor nv_colorWithHexRGB:@"#979797"];
    [timeLengSlider setThumbImage:[NvUtils imageWithName:@"Nvslider"] forState:UIControlStateNormal];
    [timeLengSlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
    [timeLengSlider addTarget:self action:@selector(sliderValueEnd:) forControlEvents:UIControlEventTouchUpInside];
    [timeLengSlider addTarget:self action:@selector(sliderValueEnd:) forControlEvents:UIControlEventTouchUpOutside];
    timeLengSlider.value = (int)(self.currentTimeline.duration/NV_TIME_BASE);
    
    self.timeLabel = [[UILabel alloc]init];
    self.timeLabel.text = [NSString stringWithFormat:@"%llds",(self.currentTimeline.duration/NV_TIME_BASE)];
    self.timeLabel.textColor = UIColor.whiteColor;
    self.timeLabel.font = [NvUtils fontWithSize:12 * SCREANSCALE];
    
    [self.view addSubview:titleLabel];
    [self.view addSubview:timeLengSlider];
    [self.view addSubview:self.timeLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.clipLivewindow.mas_bottom).offset(34 * SCREANSCALE);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    
    [timeLengSlider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(34 * SCREANSCALE);
        make.left.equalTo(self.view.mas_left).offset(13 * SCREANSCALE);
        make.width.offset(321 * SCREANSCALE);
        make.height.offset(10 * SCREANSCALE);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(timeLengSlider.mas_centerY);
        make.right.equalTo(self.view.mas_right).offset(-13 * SCREANSCALE);
    }];
}

#pragma mark 运动模块控件
- (void)addMovementModelView{
    UIView *imageBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 387 * SCREANSCALE)];
    imageBackView.backgroundColor = UIColor.clearColor;
    
    self.currentImageView = [[UIImageView alloc]init];
    self.currentImageView.userInteractionEnabled = YES;
    self.currentImageView.image = self.currentImage;
    self.currentImageView.contentMode = UIViewContentModeScaleAspectFit;
    self.switchView = [[NvSwitchView alloc]initWithFrame:CGRectMake(0, 0, 62 * SCREANSCALE, 27 * SCREANSCALE) withType:1 withState:self.movementState];
    self.switchView.selected = self.movementState;
    [self.switchView addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *huamianyundong = [[UILabel alloc]init];
    huamianyundong.text = @"画面运动";
    huamianyundong.font = [NvUtils fontWithSize:12];
    huamianyundong.textColor = UIColor.whiteColor;
    
    UIButton *previewBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [previewBtn setImage:[NvUtils imageWithName:@"NvEditPicturePreview"] forState:UIControlStateNormal];
    [previewBtn addTarget:self action:@selector(previewBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    self.areaBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.areaBtn setImage:[NvUtils imageWithName:@"NvEditPictureArea"] forState:UIControlStateNormal];
    [self.areaBtn addTarget:self action:@selector(areaBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    self.areaView = [[UIView alloc]init];
    self.areaView.layer.cornerRadius = 4 * SCREANSCALE;
    self.areaView.backgroundColor = [[UIColor nv_colorWithHexRGB:@"#52D3FF"] colorWithAlphaComponent:.7];
    
    UILabel *areaLabel = [[UILabel alloc]init];
    areaLabel.text = @"区域";
    areaLabel.textColor = UIColor.whiteColor;
    areaLabel.font = [NvUtils fontWithSize:12 * SCREANSCALE];
    
    self.fullBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.fullBtn setImage:[NvUtils imageWithName:@"NvEditPictureFull"] forState:UIControlStateNormal];
    [self.fullBtn addTarget:self action:@selector(fullBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    self.fullView = [[UIView alloc]init];
    self.fullView.layer.cornerRadius = 4 * SCREANSCALE;
    self.fullView.backgroundColor = [[UIColor nv_colorWithHexRGB:@"#52D3FF"] colorWithAlphaComponent:.7];
    
    UILabel *fullLabel = [[UILabel alloc]init];
    fullLabel.text = @"全图";
    fullLabel.textColor = UIColor.whiteColor;
    fullLabel.font = [NvUtils fontWithSize:12 * SCREANSCALE];
    
    [self.view addSubview:imageBackView];
    [imageBackView addSubview:self.currentImageView];
    [self.view addSubview:self.switchView];
    [self.view addSubview:huamianyundong];
    [self.view addSubview:previewBtn];
    [self.view addSubview:self.areaBtn];
    [self.view addSubview:self.fullBtn];
    [self.areaBtn addSubview:self.areaView];
    [self.fullBtn addSubview:self.fullView];
    [self.view addSubview:areaLabel];
    [self.view addSubview:fullLabel];
    
    [self.currentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(imageBackView.mas_centerY);
        make.centerX.equalTo(imageBackView.mas_centerX);
        make.width.offset(self.imageWidth);
        make.height.offset(self.imageHeight);
    }];
    
    [self.switchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageBackView.mas_bottom).offset(23 * SCREANSCALE);
        make.left.equalTo(self.view.mas_left).offset(13 * SCREANSCALE);
        make.width.offset(62 * SCREANSCALE);
        make.height.offset(27 * SCREANSCALE);
    }];
    
    [huamianyundong mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.switchView.mas_right).offset(12 * SCREANSCALE);
        make.centerY.equalTo(self.switchView.mas_centerY);
    }];
    
    [previewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageBackView.mas_bottom).offset(23 * SCREANSCALE);
        make.right.equalTo(self.view.mas_right).offset(-13 * SCREANSCALE);
    }];

    [self.areaBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageBackView.mas_bottom).offset(70 * SCREANSCALE);
        make.left.equalTo(self.view.mas_left).offset(103 * SCREANSCALE);
    }];

    [self.areaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(self.areaBtn);
        make.width.equalTo(self.areaBtn.mas_width);
        make.height.equalTo(self.areaBtn.mas_height);
    }];
    
    [areaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.areaBtn.mas_centerX);
        make.top.equalTo(self.areaBtn.mas_bottom).offset(7 * SCREANSCALE);
    }];

    [self.fullBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageBackView.mas_bottom).offset(70 * SCREANSCALE);
        make.right.equalTo(self.view.mas_right).offset(-103 * SCREANSCALE);
    }];
    
    [self.fullView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(self.fullBtn);
        make.width.equalTo(self.fullBtn.mas_width);
        make.height.equalTo(self.fullBtn.mas_height);
    }];

    [fullLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.fullBtn.mas_centerX);
        make.top.equalTo(self.fullBtn.mas_bottom).offset(7 * SCREANSCALE);
    }];
    
    [self.switchView switchSelected:self.movementState];
}

#pragma mark 预览视图
- (void)addPreviewView{
    self.previewView = [[UIView alloc]initWithFrame:self.view.frame];
    self.previewView.hidden = YES;
    self.previewView.backgroundColor = [UIColor nv_colorWithHexARGB:@"#99000000"];
    
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 387 * SCREANSCALE)];
    backView.backgroundColor = self.view.backgroundColor;
    
    self.clipLivewindow = [[NvEditClipLiveWindow alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 387 * SCREANSCALE)];
    [self.view addSubview:self.clipLivewindow];
    [self.clipLivewindow connectTimeline:self.currentTimeline];
    self.clipLivewindow.editMode = self.editMode;
    [self.clipLivewindow seekTimeline:0];
    [self.clipLivewindow setPlayRangeIn:0 rangeOut:self.currentTimeline.duration];
    
    UIButton *finshBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [finshBtn setImage:[NvUtils imageWithName:@"NvEditPictureFinsh"] forState:UIControlStateNormal];
    [finshBtn addTarget:self action:@selector(finshBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.previewView];
    [self.previewView addSubview:backView];
    [self.previewView addSubview:self.clipLivewindow];
    [self.previewView addSubview:finshBtn];
    
    [finshBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.clipLivewindow.mas_bottom).offset(128 * SCREANSCALE);
        make.centerX.equalTo(self.previewView.mas_centerX);
    }];
    
}

#pragma mark 添加拖动框
- (void)addDragView{
    //初始化拖拽框
    if (_startDragView == nil){
        _startDragView = [EditPictureDragView new];
    }
    if (_endDragView == nil){
        _endDragView = [EditPictureDragView new];
    }
    if (_areaDragView == nil){
        _areaDragView = [EditPictureDragView new];
    }
    
    [self.currentImageView addSubview:_startDragView];
    [self.currentImageView addSubview:_endDragView];
    [self.currentImageView addSubview:_areaDragView];
    
    _startDragView.delegate = self;
    _startDragView.mode = startMode;
    _startDragView.imageSize = CGSizeMake(self.imageWidth, self.imageHeight);
    
    _endDragView.delegate = self;
    _endDragView.mode = endMode;
    _endDragView.imageSize = CGSizeMake(self.imageWidth, self.imageHeight);
    
    _areaDragView.delegate = self;
    _areaDragView.mode = areaMode;
    _areaDragView.imageSize = CGSizeMake(self.imageWidth, self.imageHeight);
    
    CGRect startRect = [self NormalizedToView:self.videoClip.startROI];
    CGRect endRect = [self NormalizedToView:self.videoClip.endROI];
    CGRect areaRect = [self NormalizedToView:self.videoClip.startROI];
    [self.startDragView setFrame:startRect];
    [self.endDragView setFrame:endRect];
    [self.areaDragView setFrame:areaRect];
    [self.startDragView setText:@"开始画面"];
    [self.endDragView setText:@"结束画面"];
    
    //为拖拽框设置横纵比
    CGFloat scale = startRect.size.width / startRect.size.height;
    _startDragView.scale = scale;
    _endDragView.scale = scale;
    _areaDragView.scale = scale;
    
    [self.startDragView addDragBar];
    [self.endDragView addDragBar];
    [self.areaDragView addDragBar];
}

#pragma mark 归一化坐标转换为控件坐标
- (CGRect) NormalizedToView:(NvsRect) rect {
    CGRect newRect;
    NSUInteger width = self.imageWidth;
    NSUInteger height = self.imageHeight;
    newRect.origin.x = (rect.left + 1)/2 * width;
    newRect.origin.y = (1 - rect.top)/2 * height;
    newRect.size.width = (rect.right - rect.left)/2 * width;
    newRect.size.height =  (rect.top - rect.bottom)/2 * height;
    return newRect;
}

#pragma mark 控件坐标转换为归一化坐标
- (NvsRect) ViewToNormalized:(CGRect) rect {
    NvsRect newRect;
    NSUInteger width = self.currentImageView.frame.size.width;
    NSUInteger height = self.currentImageView.frame.size.height;
    newRect.left = rect.origin.x / width * 2 - 1;
    newRect.right = newRect.left + rect.size.width / width * 2;
    newRect.top = 1 - rect.origin.y / height * 2;
    newRect.bottom = newRect.top - rect.size.height / height *2;
    if (newRect.left < -1)
        newRect.left = -1;
    if (newRect.right > 1)
        newRect.right = 1;
    if (newRect.bottom < -1)
        newRect.bottom = -1;
    if (newRect.top > 1)
        newRect.top = 1;
    return newRect;
}

#pragma mark 为图片展示控件计算合理宽度和高度
- (void)adapterWidthAndHeight{
    CGFloat width,height;
    CGFloat viewWidth = self.view.frame.size.width;
    CGFloat viewHeight = 387 * SCREANSCALE;
    CGFloat imageWidth = self.currentImage.size.width;
    CGFloat imageHeight = self.currentImage.size.height;
    NSUInteger widthScale = ceilf(imageWidth / viewWidth);
    NSUInteger heightScale = ceilf(imageHeight / viewHeight);
    if (widthScale == 1 && heightScale == 1) {
        width = imageWidth;
        height = imageHeight;
    } else {
        if (widthScale > heightScale) {
            width = viewWidth;
            height = viewWidth / (imageWidth/imageHeight);
//            width = imageWidth / widthScale;
//            height = imageHeight / widthScale;
        } else {
//            width = imageWidth / heightScale;
//            height = imageHeight / heightScale;
            width = viewHeight * (imageWidth/imageHeight);
            height = viewHeight;
        }
    }
    self.imageWidth = width;
    self.imageHeight = height;
}


#pragma mark EditPictureDragViewDelegate
- (void)updateRect:(CGRect)rect withMode:(DragViewMode)mode{
    if (mode == startMode) {
        self.startDragView.frame = rect;
    }else if (mode == endMode){
        self.endDragView.frame = rect;
    }else{
        self.areaDragView.frame = rect;
    }
}

#pragma mark 调整时长滑动事件
//滑动更改
- (void)sliderValueChanged:(UISlider *)slider{
    self.timeLabel.text = [NSString stringWithFormat:@"%ds",(int)slider.value];
    int64_t time = [self.timeLabel.text intValue] * NV_TIME_BASE;
    [self.videoClip changeTrimOutPoint:time affectSibling:true];
    
}
//滑动结束
- (void)sliderValueEnd:(UISlider *)slider{
    [self.clipLivewindow seekTimeline:0];
    [self.clipLivewindow setPlayRangeIn:0 rangeOut:self.currentTimeline.duration];
    [self.clipLivewindow play];
}

#pragma mark 预览按钮点击事件
- (void)previewBtn:(UIButton *)sender{
    [self updateTimelineModel];
    [self.clipLivewindow seekTimeline:0];
    [self.clipLivewindow play];
    self.previewView.hidden = NO;
}

#pragma mark 根据数据修改timeline，达到预览效果
- (void)updateTimelineModel{
    if (!self.fullBtn.selected && self.areaBtn.selected) {
        self.videoClip.imageMotionMode = NvsStreamingEngineImageClipMotionMode_ROI;
        //区域显示
        if (self.movementState) {
            startRectImage = [self ViewToNormalized:self.startDragView.frame];
            endRectImage = [self ViewToNormalized:self.endDragView.frame];
            [self.videoClip setImageMotionROI:&startRectImage endROI:&endRectImage];
            self.videoClip.imageMotionAnimationEnabled = YES;
        }else{
            self.videoClip.imageMotionAnimationEnabled = NO;
            areaRectImage = [self ViewToNormalized:self.areaDragView.frame];
            [self.videoClip setImageMotionROI:&areaRectImage endROI:&areaRectImage];
        }
    }else if (self.fullBtn.selected && !self.areaBtn.selected){
        self.videoClip.imageMotionMode = NvsStreamingEngineImageClipMotionMode_LetterBoxZoomIn;
        //全图显示
        if (self.movementState) {
            self.videoClip.imageMotionAnimationEnabled = YES;
        }else{
            self.videoClip.imageMotionAnimationEnabled = NO;
        }
    }
}

#pragma mark 预览结束按钮点击事件
- (void)finshBtn:(UIButton *)sender{
    self.previewView.hidden = YES;
    [self.clipLivewindow seekTimeline:0];
}

#pragma mark 区域显示按钮点击事件
- (void)areaBtn:(UIButton *)sender{
    self.areaView.hidden = NO;
    self.fullView.hidden = YES;
    self.fullBtn.selected = NO;
    if (sender.selected) {
        sender.selected = NO;
    }else{
        sender.selected = YES;
    }
    [self configDragView];
}

#pragma mark 全图显示按钮点击事件
- (void)fullBtn:(UIButton *)sender{
    self.areaView.hidden = YES;
    self.fullView.hidden = NO;
    self.areaBtn.selected = NO;
    if (sender.selected) {
        sender.selected = NO;
    }else{
        sender.selected = YES;
    }
    [self configDragView];
}

#pragma mark 画面运动按钮点击事件
- (void)switchAction:(NvSwitchView *)sender{
    if (sender.selected) {
        sender.selected = NO;
        //关闭运动
        self.movementState = NO;
        [sender switchSelected:NO];
        [self configDragView];
        
    }else {
        sender.selected = YES;
        //开启运动
        self.movementState = YES;
        [sender switchSelected:YES];
        [self configDragView];
    }
}

#pragma mark 根据选中效果显示不同的拖动框
- (void)configDragView{
    if (!self.fullBtn.selected && self.areaBtn.selected) {
        //区域显示
        self.currentImageView.layer.borderWidth = 0;
        if (self.movementState) {
            self.areaDragView.hidden = YES;
            self.startDragView.hidden = NO;
            self.endDragView.hidden = NO;
        }else{
            self.areaDragView.hidden = NO;
            self.startDragView.hidden = YES;
            self.endDragView.hidden = YES;
        }
    }else if (self.fullBtn.selected && !self.areaBtn.selected){
        //全图显示
        self.areaDragView.hidden = YES;
        self.startDragView.hidden = YES;
        self.endDragView.hidden = YES;
        self.currentImageView.layer.borderColor = [UIColor nv_colorWithHexRGB:@"#D0021B"].CGColor;
        self.currentImageView.layer.borderWidth = 3;
    }
}

#pragma mark 完成按钮点击
- (void)finshClick:(UIButton *)sender{
    if ([self.title isEqualToString:@"时长"]) {
        self.model.trimOut = self.currentTimeline.duration;
        NvTimeFilterInfoModel *filterInfo = [[[NvTimelineData sharedInstance] videoFxDataArray] firstObject];
        filterInfo.outPoint = filterInfo.outPoint + self.model.trimOut;
    }else{
        [self updateTimelineModel];
        self.model.isDefault = YES;
        self.model.hasMotion = self.movementState;
        self.model.isArea = self.areaBtn.selected;
        self.model.motionMode = self.videoClip.imageMotionMode;
        if (self.model.isArea) {
            if (self.movementState) {
                self.model.startRect = startRectImage;
                self.model.endRect = endRectImage;
            }else{
                self.model.startRect = areaRectImage;
                self.model.endRect = areaRectImage;
            }
        }
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (UIView *)leftNavigationBarItemView {
    return [UIView new];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
