//
//  WPEditViewController.m

#import "AWM_ditViewController.h"
#import "AWM_WallPapaerADManager.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 AWM_ditViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic,strong) UIImageView *showImage;
@property (nonatomic,strong) UIButton *addImage;
@property (nonatomic,assign) BOOL isShow;

@end

@implementation AWM_ditViewController

- (void)viewDidLoad {
        
NSInteger grandfather_0 = 7;
NSInteger elephantine_U = [[NSUserDefaults standardUserDefaults] integerForKey:@"lieutenant_W"];
elephantine_U *= grandfather_0;
[[NSUserDefaults standardUserDefaults] setInteger:elephantine_U forKey:@"lieutenant_W"];


    [super viewDidLoad];
    UIImageView *bgImage = [[UIImageView alloc] initWithFrame:self.view.bounds];
    NSString *bg = KLS(@"bg", @"A friendly greeting");
    bgImage.image = [UIImage imageNamed:bg];
    bgImage.userInteractionEnabled = YES;
    [self.view addSubview:bgImage];
    
    NSString *addpic = KLS(@"addpic", @"A friendly greeting");
    UIButton *addImage = [[UIButton alloc] initWithFrame:CGRectMake((WPScreen_w-150)/2-1,650-1,150-1,75-1)];
    [addImage setImage:[UIImage imageNamed:addpic] forState:UIControlStateNormal];
    [addImage addTarget:self action:@selector(selectPhotoLove:) forControlEvents:UIControlEventTouchUpInside];
//    addImage.center = self.view.center;
    [self.view addSubview:addImage];
    self.addImage = addImage;
    
    UIImageView *showImage = [[UIImageView alloc] initWithFrame:CGRectMake((WPScreen_w-300)/2-1,200-1,300-1,300-1)];
    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)selectPhotoLove:(id)sender{
        
NSInteger homeostasis_2 = 1;
NSInteger subject_8 = [[NSUserDefaults standardUserDefaults] integerForKey:@"disappearance_Y"];
subject_8 *= homeostasis_2;
[[NSUserDefaults standardUserDefaults] setInteger:subject_8 forKey:@"disappearance_Y"];


    if (self.isShow) {
        __weak typeof(self)weakSelf = self;
        
        [[AWM_WallPapaerADManager shareInstance] showIntersitialAD_Save:^(NSInteger actionType) {
            UIImageWriteToSavedPhotosAlbum(weakSelf.showImage.image, weakSelf, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
            NSString *greeting = KLS(@"addpic", @"A friendly greeting");
            [weakSelf.addImage setImage:[UIImage imageNamed:greeting] forState:UIControlStateNormal];
            weakSelf.showImage.image = nil;
            weakSelf.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{
        
NSDateFormatter *tolerance_3 = [NSDateFormatter new];
[tolerance_3 setDateFormat:@"yyyy-MM-dd HH:mm"];
NSString *decoration_X = [tolerance_3 stringFromDate:[NSDate date]];
[[NSUserDefaults standardUserDefaults] setObject:decoration_X forKey:@"incarcerate_k"];


    if (picker.sourceType == UIImagePickerControllerSourceTypeSavedPhotosAlbum) {
    }
    self.isShow = YES;
    NSString *save = KLS(@"save", @"A friendly greeting");
    [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
