//
//  WPEditViewController.m

#import "WPEditViewController.h"
#define WPScreen_w  [UIScreen mainScreen].bounds.size.width
#define WPScreen_h [UIScreen mainScreen].bounds.size.height
#define WP_COLOR_WITH_HEX(HEX) [UIColor colorWithRed:((HEX >> 16) & 0xFF) / 255.0f green:((HEX >> 8) & 0xFF) / 255.0f blue:((HEX) & 0xFF) / 255.0f alpha:1.0f]

@interface WPEditViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic,strong) UIImageView *showImage;
@property (nonatomic,strong) UIButton *addImage;
@property (nonatomic,assign) BOOL isShow;

@end

@implementation WPEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *bgImage = [[UIImageView alloc] initWithFrame:self.view.bounds];
    bgImage.image = [UIImage imageNamed:@"bg"];
    bgImage.userInteractionEnabled = YES;
    [self.view addSubview:bgImage];
    
    UIButton *addImage = [[UIButton alloc] initWithFrame:CGRectMake((WPScreen_w-150)/2, 650, 150, 75)];
    [addImage setImage:[UIImage imageNamed:@"addpic"] forState:UIControlStateNormal];
    [addImage addTarget:self action:@selector(selectPhoto) forControlEvents:UIControlEventTouchUpInside];
//    addImage.center = self.view.center;
    [self.view addSubview:addImage];
    self.addImage = addImage;
    
    UIImageView *showImage = [[UIImageView alloc] initWithFrame:CGRectMake((WPScreen_w-300)/2, 200, 300, 300)];
    showImage.userInteractionEnabled = YES;
    [self.view addSubview:showImage];
    showImage.layer.cornerRadius = 20;
    showImage.layer.masksToBounds = YES;
    showImage.layer.borderColor = WP_COLOR_WITH_HEX(0xF2C4D5).CGColor;
    showImage.layer.borderWidth = 1;
    self.showImage = showImage;
    
}
- (void)selectPhoto{
    if (self.isShow) {
        UIImageWriteToSavedPhotosAlbum(self.showImage.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
        [self.addImage setImage:[UIImage imageNamed:@"addpic"] forState:UIControlStateNormal];
        self.showImage.image = nil;
        self.isShow = NO;

    }else{
        UIImagePickerController *imgPicker = [[UIImagePickerController alloc]init];
        imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        imgPicker.delegate = self;
        imgPicker.allowsEditing = YES;
        [self presentViewController:imgPicker animated:YES completion:nil];
    }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    if (picker.sourceType == UIImagePickerControllerSourceTypeSavedPhotosAlbum) {
    }
    self.isShow = YES;
    [self.addImage setImage:[UIImage imageNamed:@"save"] forState:UIControlStateNormal];
    self.showImage.image = [info objectForKey:(UIImagePickerControllerEditedImage)];
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imageSavedToPhotosAlbum:(UIImage *)image
       didFinishSavingWithError:(NSError *)error
                    contextInfo:(void *)contextInfo
{
    if (!error) {
        UIAlertView *iconAl = [[UIAlertView alloc] initWithTitle:@"" message:@"Save Success" delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok", nil];
        [iconAl show];
    } else {
        NSLog(@"%@", [error description]);
    }
}
@end
