//
//  HdlCorpImageViewController.m
//  HDL_Widget_iOS
//
//  Created by HDL on 2019/10/17.
//  Copyright © 2019 JLChen. All rights reserved.
//

#import "HdlCorpImageViewController.h"
#import "UIImage+Crop.h"
#import "CropImageController.h"

#define WEAKSELF_AT __weak __typeof(&*self)weakSelf_AT = self;

@interface HdlCorpImageViewController ()<CropImageDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, strong) NSString *mCropPicturePath;  //图片路径
@property (nonatomic, strong) NSString *mCropPictureName;  //图片命名

@end

@implementation HdlCorpImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _mCropPictureName = @"hdl_headPicture.Png";
    if(_openType == 101){
        [self openTypeCamera];
    }else{
        [self openTypePhotoLibrary];
    }
    
    // Do any additional setup after loading the view.
}

#pragma mark - 上传头像
-(void)openTypeCamera{
    WEAKSELF_AT
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = weakSelf_AT;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    [self presentViewController:imagePickerController animated:YES completion:nil];
}

-(void)openTypePhotoLibrary;{
    WEAKSELF_AT
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = weakSelf_AT;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:imagePickerController animated:YES completion:nil];
    
}

#pragma mark -- UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
     [picker dismissViewControllerAnimated:YES completion:nil];
//    [picker dismissViewControllerAnimated:YES completion:^{
//
//    }];
//    [picker dismissViewControllerAnimated:YES completion:^{
//        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//        CropImageController * con = [[CropImageController alloc] initWithImage:image delegate:self];
//        [self.navigationController pushViewController:con animated:YES];
//    }];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = image.size.height * (width/image.size.width);
    UIImage * orImage = [image resizeImageWithSize:CGSizeMake(width, height)];
    CropImageController * con = [[CropImageController alloc] initWithImage:orImage delegate:self];
    con.ovalClip = NO;
    [self.navigationController pushViewController:con animated:YES];
}

// 取消图片选择调用此方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
     [self backAction:NO];
}



-(void)backAction:(BOOL)bGetPicture{
    if(bGetPicture){
        if(self.mSaveImageCallBack != NULL){
            [self.mSaveImageCallBack SaveImageCallBack:_mCropPicturePath];
            NSLog(@"关闭页面");
        }
    }
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark -- CropImageDelegate
- (void)cropImageDidFinishedWithImage:(NSString *)imageName bSuccess:(BOOL)bSuccess{

    if(bSuccess){
        //保存到本地并返回路径
//    _mCropPicturePath
//        NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
//        
//        NSString *filePath = [[paths objectAtIndex:0]stringByAppendingPathComponent:
//                              [NSString stringWithFormat:@"%@", _mCropPictureName]];  // 保存文件的名称
//        
//        BOOL result =[UIImagePNGRepresentation(image)writeToFile:filePath   atomically:YES]; // 保存成功会返回YES
//        if (result == YES) {
////            _mCropPicturePath =_mCropPictureName;
//             _mCropPicturePath =filePath;
//            NSLog(@"保存成功");
//        }
   
        _mCropPicturePath = imageName;
        [self backAction:bSuccess];
    }else{
        [self backAction:bSuccess];
    }

}

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