//
//  NvCustomStickerShapeViewController.m
//  SDKDemo
//
//  Created by dx on 2018/6/21.
 //
//

#import "NvCustomStickerShapeViewController.h"
#import "NvCustomStickerViewController.h"
#import "NvDragView.h"
#import "NvShapeButton.h"

@interface NvCustomStickerShapeViewController ()<NvDragViewDelegate, NvShapeButtonDelegate>
//图片展示视图
@property (nonatomic, strong) UIImageView *photoView;
//拖拽框
@property (nonatomic, strong) NvDragView *dragView;
//内容视图
@property (nonatomic, strong) NvShapeButton *freeShapeButton;//自由形状
@property (nonatomic, strong) NvShapeButton *roundShapeButton;//圆形
@property (nonatomic, strong) NvShapeButton *squreShapeButton;//正方形

@property (nonatomic, strong) UIButton *finishButton;//完成按钮

@property (nonatomic, strong) UIView *line;

@end

@implementation NvCustomStickerShapeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"选择内容";
    [self addSubViews];
}

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

- (void)addSubViews {
    [self initPhotoView];
    [self initSeplineView];
    [self initFreeShapeButton];
    [self initRoundShapeButton];
    [self initSqureShapeButton];
    [self initFinishButton];
}

- (void)initPhotoView {
    self.photoView = [[UIImageView alloc] init];
    [self.photoView setImage:self.selectedImage];
    self.photoView.userInteractionEnabled = YES;
    [self.view addSubview:self.photoView];
    [self setPhotoViewFrame];
    
    self.dragView = [[NvDragView alloc] initWithFrame:CGRectMake(self.photoView.bounds.size.width/4,
                                                                 self.photoView.bounds.size.height/4,
                                                                 self.photoView.bounds.size.width/2,
                                                                 self.photoView.bounds.size.width/2)];
    self.dragView.delegate = self;
    [self.dragView addDragBar];
    [self.photoView addSubview:self.dragView];
    [self.photoView bringSubviewToFront:self.dragView];
}

- (void)initFinishButton {
    self.finishButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.finishButton setImage:[NvUtils imageWithName:@"Nvcheck - material"] forState:UIControlStateNormal];
    [self.finishButton addTarget:self action:@selector(finishButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.finishButton.frame = CGRectMake(175*SCREANSCALE, 568*SCREANSCALE, 25*SCREANSCALE, 20*SCREANSCALE);
    
    [self.view addSubview:self.finishButton];
    
    self.line = [UIView new];
    self.line.backgroundColor = [UIColor nv_colorWithHexARGB:@"#1AFFFFFF"];
    [self.view addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.height.equalTo(@1);
        make.bottom.equalTo(self.finishButton.mas_top).offset(-12*SCREANSCALE);
    }];
}

- (void)initFreeShapeButton {
    NvShapeButtonItem *freeItem = NvShapeButtonItem.new;
    freeItem.imagePath = @"NvStickerCustom";
    freeItem.text = @"自由";
    freeItem.selected = YES;
    freeItem.shape = NV_SHAPE_FREE;
    self.freeShapeButton = [[NvShapeButton alloc] initWithFrame:CGRectMake(44*SCREANSCALE, 469*SCREANSCALE, 50*SCREANSCALE, 75*SCREANSCALE) item:freeItem];
    self.freeShapeButton.delegate = self;
    
    
    [self.view addSubview:self.freeShapeButton];
}

- (void)initRoundShapeButton {
    NvShapeButtonItem *roundItem = NvShapeButtonItem.new;
    roundItem.imagePath = @"NvStickerCircle";
    roundItem.text = @"圆形";
    roundItem.selected = NO;
    roundItem.shape = NV_SHAPE_ROUND;
    self.roundShapeButton = [[NvShapeButton alloc] initWithFrame:CGRectMake(166*SCREANSCALE, 469*SCREANSCALE, 50*SCREANSCALE, 75*SCREANSCALE) item:roundItem];
    self.roundShapeButton.delegate = self;
    
    [self.view addSubview:self.roundShapeButton];
}

- (void)initSqureShapeButton {
    NvShapeButtonItem *squreItem = NvShapeButtonItem.new;
    squreItem.imagePath = @"NvStickerRect";
    squreItem.text = @"正方";
    squreItem.selected = NO;
    squreItem.shape = NV_SHAPE_SQUARE;
    self.squreShapeButton = [[NvShapeButton alloc] initWithFrame:CGRectMake(287*SCREANSCALE, 469*SCREANSCALE, 50*SCREANSCALE, 75*SCREANSCALE) item:squreItem];
    self.squreShapeButton.delegate = self;
    
    [self.view addSubview:self.squreShapeButton];
}

- (void)initSeplineView {
    float navbarHeight = 64*SCREANSCALE;
    UIView *sepline = [[UIView alloc] initWithFrame:CGRectMake(0, 521*SCREANSCALE - navbarHeight, SCREANWIDTH, SCREANSCALE)];
    sepline.backgroundColor = [UIColor nv_colorWithHexARGB:@"#1AFFFFFF"];
    [self.view addSubview:sepline];
}

- (void)setPhotoViewFrame {
    float centerPartHeight = 455*SCREANSCALE;
    if (self.selectedImage.size.width < self.selectedImage.size.height) {
        float height = SCREANWIDTH;
        float width = self.selectedImage.size.width/self.selectedImage.size.height*height;
        float x = (SCREANWIDTH - width)/2;
        float y = (centerPartHeight - height)/2;
        self.photoView.frame = CGRectMake(x, y, width, height);
    } else if (self.selectedImage.size.width > self.selectedImage.size.height) {
        float width = SCREANWIDTH;
        float height = self.selectedImage.size.height/self.selectedImage.size.width*width;
        float x = 0;
        float y = (centerPartHeight - height)/2;
        self.photoView.frame = CGRectMake(x, y, width, height);
    } else {
        float width = SCREANWIDTH;
        float height = SCREANWIDTH;
        float x = 0;
        float y = (centerPartHeight - height)/2;
        self.photoView.frame = CGRectMake(x, y, width, height);
    }
}

- (void)onButtonClicked:(NvShapeEnum)shape {
    if (shape == NV_SHAPE_FREE) {
        self.dragView.originRect = CGRectMake(0,0,self.photoView.bounds.size.width/2,self.photoView.bounds.size.width/2);
        [self.dragView setDragMode:freeMode];
        self.dragView.frame = CGRectMake(self.photoView.bounds.size.width/4,self.photoView.bounds.size.height/4,self.photoView.bounds.size.width/2,self.photoView.bounds.size.width/2);
        [self.freeShapeButton setSelect:YES];
        [self.roundShapeButton setSelect:NO];
        [self.squreShapeButton setSelect:NO];
    } else if (shape == NV_SHAPE_ROUND) {
        self.dragView.originRect = CGRectMake(0,0,self.photoView.bounds.size.width/2,self.photoView.bounds.size.width/2);
        [self.dragView setDragMode:roundMode];
        self.dragView.frame = CGRectMake(self.photoView.bounds.size.width/4,self.photoView.bounds.size.height/4,self.photoView.bounds.size.width/2,self.photoView.bounds.size.width/2);
        [self.freeShapeButton setSelect:NO];
        [self.roundShapeButton setSelect:YES];
        [self.squreShapeButton setSelect:NO];
    } else if (shape == NV_SHAPE_SQUARE) {
        self.dragView.originRect = CGRectMake(0,0,self.photoView.bounds.size.width/2,self.photoView.bounds.size.width/2);
        [self.dragView setDragMode:squreMode];
        self.dragView.frame = CGRectMake(self.photoView.bounds.size.width/4,self.photoView.bounds.size.height/4,self.photoView.bounds.size.width/2,self.photoView.bounds.size.width/2);
        [self.freeShapeButton setSelect:NO];
        [self.roundShapeButton setSelect:NO];
        [self.squreShapeButton setSelect:YES];
    }
}

-(void) finishButtonClicked:(UIButton *)sender{
    NvCustomStickerViewController *vc = [[NvCustomStickerViewController alloc] init];
    UIImage *photoImage = self.photoView.image;
    float x,y,w,h;
    x = self.dragView.frame.origin.x/self.photoView.bounds.size.width*photoImage.size.width;
    y = self.dragView.frame.origin.y/self.photoView.bounds.size.height*photoImage.size.height;
    w = self.dragView.frame.size.width/self.photoView.bounds.size.width*photoImage.size.width;
    h = self.dragView.frame.size.height/self.photoView.bounds.size.height*photoImage.size.height;
    UIImage *image = [self imageFromImage:self.photoView.image inRect:CGRectMake(x,y,w,h) isRound:self.dragView.mode == roundMode];
    vc.image = image;
    [self.navigationController pushViewController:vc animated:YES];
}

//裁剪方法
- (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect isRound:(BOOL)isRound {
    
    //将UIImage转换成CGImageRef
    CGImageRef sourceImageRef = [image CGImage];
    
    //按照给定的矩形区域进行剪裁
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
    
    //将CGImageRef转换成UIImage
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    CGImageRelease(newImageRef);
    if (isRound) {
        UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
        
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        CGRect rectNew = CGRectMake(0, 0, rect.size.width, rect.size.height);
        CGContextAddEllipseInRect(context, rectNew);
        CGContextClip(context);
        
        [newImage drawInRect:rectNew];
        
        newImage = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
    }
    
    //返回剪裁后的图片
    return newImage;
}

//根据拖拽框位置刷新开始和结束画面位置
- (void)updateRect:(CGRect) rect {

    
}
/*
#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
