//
//  WPDetailViewController.m

#import "WPDetailViewController.h"
#import "AppDelegate.h"
#import "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 WPDetailViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIButton *backItem;
@property(nonatomic,strong)UIButton *navImage;
@property(nonatomic,strong)UIScrollView *wpscroll;
@property(nonatomic,assign)NSInteger index;

@end

@implementation WPDetailViewController
- (void)backItemEvent{
    __weak typeof(self) weakSelf = self;
    [[WallPapaerADManager shareInstance] showIntersitialAD_Back:^(NSInteger actionType) {
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
//    [self.navigationController popViewControllerAnimated:YES];
}
- (void)saveItemEvent{
    UIImageWriteToSavedPhotosAlbum([UIImage imageNamed:self.data[self.index]], self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
    AppDelegate *app = (AppDelegate *)[[UIApplication  sharedApplication] delegate];
    [app.saveImages addObject:self.data[self.index]];
}
- (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]);
    }
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar addSubview:self.backItem];
    [self.navigationController.navigationBar addSubview:self.navImage];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.backItem removeFromSuperview];
    [self.navImage removeFromSuperview];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.hidesBackButton = YES;
    self.view.backgroundColor = WP_COLOR_WITH_HEX(0xF2C4D5);
    UIButton *backItem = [[UIButton alloc] initWithFrame:CGRectMake(20, 0, 74, 44)];
    [backItem setImage:[UIImage imageNamed:@"backitem"] forState:UIControlStateNormal];
    [backItem addTarget:self action:@selector(backItemEvent) forControlEvents:UIControlEventTouchUpInside];
    self.backItem = backItem;
    
    UIButton *navImage = [[UIButton alloc] initWithFrame:CGRectMake(WPScreen_w-100, 2, 74, 40)];
    [navImage setImage:[UIImage imageNamed:@"save"] forState:UIControlStateNormal];
    [navImage addTarget:self action:@selector(saveItemEvent) forControlEvents:UIControlEventTouchUpInside];
    self.navImage = navImage;
    
    
    UIScrollView *wpscroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 150, WPScreen_w, WPScreen_h-350)];
    wpscroll.pagingEnabled = YES;
    wpscroll.delegate = self;
    [self.view addSubview:wpscroll];
    self.wpscroll = wpscroll;
    
    for (int i = 0; i < self.data.count; i ++) {
        UIImageView *iconImg = [[UIImageView alloc] initWithFrame:CGRectMake(i*wpscroll.frame.size.width, 0, wpscroll.frame.size.width, wpscroll.frame.size.height)];
        iconImg.image = [UIImage imageNamed:self.data[i]];
        iconImg.contentMode = UIViewContentModeScaleAspectFill;
        [wpscroll addSubview:iconImg];
    }
    
    [wpscroll setContentSize:CGSizeMake(WPScreen_w*self.data.count, wpscroll.frame.size.height)];
    
    
    
    UIButton *shangItem = [[UIButton alloc] initWithFrame:CGRectMake((WPScreen_w-240)/3, WPScreen_h-170, 120, 50)];
    [shangItem setImage:[UIImage imageNamed:@"shang"] forState:UIControlStateNormal];
    [shangItem addTarget:self action:@selector(shangItemEvent) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:shangItem];
    
    UIButton *xiaItem = [[UIButton alloc] initWithFrame:CGRectMake((WPScreen_w-240)/3*2+120, WPScreen_h-170, 120, 50)];
    [xiaItem setImage:[UIImage imageNamed:@"xia"] forState:UIControlStateNormal];
    [xiaItem addTarget:self action:@selector(xiaItemEvent) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:xiaItem];

}
- (void)shangItemEvent{
    if (self.index == 0) {
        UIAlertView *iconAl = [[UIAlertView alloc] initWithTitle:@"" message:@"It's already the first one" delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok", nil];
        [iconAl show];
        return;
    }
    self.index --;
    [self.wpscroll setContentOffset:CGPointMake(self.index*WPScreen_w, 0)];
}
- (void)xiaItemEvent{
    if (self.index == self.data.count-1) {
        UIAlertView *iconAl = [[UIAlertView alloc] initWithTitle:@"" message:@"It's the last one" delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok", nil];
        [iconAl show];
        return;
    }
    self.index ++;
    [self.wpscroll setContentOffset:CGPointMake(self.index*WPScreen_w, 0)];

}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    self.index = scrollView.contentOffset.x;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.index = scrollView.contentOffset.x/WPScreen_w;
    NSLog(@"...  %d",self.index);
}
@end
