//
//  NvEditMaterialCollectionViewCell.m
//  SDKDemo
//
//  Created by ms20180425 on 2018/6/11.
 //
//

#import "NvEditMaterialCollectionViewCell.h"
#import "NVHeader.h"
#import <Photos/Photos.h>
#import "NvSDKUtils.h"

@interface NvEditMaterialCollectionViewCell ()

@property (nonatomic, strong) UIImageView *previewView;

@end


@implementation NvEditMaterialCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubViews];
    }
    return self;
}

- (void)setCoverImage:(UIImage *)coverImage{
    _coverImage = coverImage;
    self.previewView.image = coverImage;
}

- (void)setModel:(NvEditDataModel *)model{
    _model = model;
    if (model.isLoading && model.thumImage) {
        self.previewView.image = model.thumImage;
        return;
    }
    if (model.isImage) {
        if (model.isPhotoAlbum) {
            PHFetchResult<PHAsset *> *phresult = [PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObject:model.localIdentifier] options:nil];
            if (phresult.count > 0) {
                PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];
                requestOptions.synchronous = NO;
                PHAsset *phasset = phresult.lastObject;
                [[PHImageManager defaultManager] requestImageForAsset:phasset
                                                           targetSize:CGSizeMake(180, 320)
                                                          contentMode:PHImageContentModeAspectFill
                                                              options:requestOptions
                                                        resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
                                                            self.previewView.image = result;
                                                            model.thumImage = result;
                                                            model.isLoading = YES;
                                                        }
                 ];
            }
        }else{
            model.isLoading = YES;
            self.previewView.image = [UIImage imageWithContentsOfFile:model.localIdentifier];
            model.thumImage = self.previewView.image;
        }
    }else{
        model.isLoading = YES;
        NvsVideoFrameRetriever *Retriever = [[NvSDKUtils getSDKContext] createVideoFrameRetriever:model.videoPath];
        self.previewView.image = [Retriever getFrameAtTime:model.trimIn videoFrameHeightGrade:NvsVideoFrameHeightGrade480];
        model.thumImage = self.previewView.image;
    }
}

- (void)addSubViews{
    
    self.previewView = [[UIImageView alloc]init];
    self.previewView.image = [NvUtils imageWithName:@"EditMaterialImageDefault"];
    self.previewView.contentMode = UIViewContentModeScaleAspectFill;
    self.previewView.clipsToBounds = YES;
    
    self.leftView = [[UIImageView alloc]init];
    self.leftView.image = [NvUtils imageWithName:@"NvEditpreviewLine"];
    
    self.rightView = [[UIImageView alloc]init];
    self.rightView.image = [NvUtils imageWithName:@"NvEditpreviewLine"];
    
    self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.leftBtn setImage:[NvUtils imageWithName:@"NvEditpreviewAdd"] forState:UIControlStateNormal];
    
    
    self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.rightBtn setImage:[NvUtils imageWithName:@"NvEditpreviewAdd"] forState:UIControlStateNormal];
    
    [self addSubview:self.previewView];
    [self.previewView addSubview:self.leftView];
    [self.previewView addSubview:self.rightView];
    [self addSubview:self.leftBtn];
    [self addSubview:self.rightBtn];
    
    [self.previewView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.mas_width);
        make.height.equalTo(self.mas_height);
        make.left.equalTo(self.mas_left);
    }];
    
    [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self.previewView.mas_height);
        make.width.offset(8 * SCREANSCALE);
        make.left.equalTo(self.previewView.mas_left);
    }];
    
    [self.rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self.previewView.mas_height);
        make.width.offset(8 * SCREANSCALE);
        make.right.equalTo(self.previewView.mas_right);
    }];
    
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(40 * SCREANSCALE);
        make.width.offset(40 * SCREANSCALE);
        make.centerY.equalTo(self.leftView.mas_centerY);
        make.centerX.equalTo(self.leftView.mas_centerX);
    }];
    
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(40 * SCREANSCALE);
        make.width.offset(40 * SCREANSCALE);
        make.centerY.equalTo(self.rightView.mas_centerY);
        make.centerX.equalTo(self.rightView.mas_centerX);
    }];
    __weak typeof(self)weakSelf = self;
    [self.leftBtn nv_BtnClickHandler:^{
        if ([weakSelf.delegate respondsToSelector:@selector(addClipForIndex:)]) {
            [weakSelf.delegate addClipForIndex:weakSelf.index];
        }
    }];
    
    [self.rightBtn nv_BtnClickHandler:^{
        if ([weakSelf.delegate respondsToSelector:@selector(addClipForIndex:)]) {
            [weakSelf.delegate addClipForIndex:weakSelf.index+1];
        }
    }];
    
    if (self.currentIndex == self.index) {
        self.leftView.hidden = NO;
        self.rightView.hidden = NO;
        self.leftBtn.hidden = NO;
        self.rightBtn.hidden = NO;
    } else {
        self.leftView.hidden = YES;
        self.rightView.hidden = YES;
        self.leftBtn.hidden = YES;
        self.rightBtn.hidden = YES;
    }
}

- (void)setAddButtonHidden:(Boolean)hidden {
    self.leftView.hidden = hidden;
    self.rightView.hidden = hidden;
    self.leftBtn.hidden = hidden;
    self.rightBtn.hidden = hidden;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [super hitTest:point withEvent:event];
    if (view == nil) {
        for (UIView *subView in self.subviews) {
            CGPoint tp = [subView convertPoint:point fromView:self];
            if (CGRectContainsPoint(subView.bounds, tp)) {
                view = subView;
            }
        }
    }
    return view;
}


@end
