//
//  StoreQRCodeViewController.m
//  ALand
//
//  Created by Z on 16/4/5.
//  Copyright © 2016年 Z. All rights reserved.
//

#import "StoreQRCodeViewController.h"
#import "UIImage+QRCode.h"
@interface StoreQRCodeViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageViewQRCode;
@property (weak, nonatomic) IBOutlet UIButton *buttonSave;

@end

@implementation StoreQRCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImage *qrcode = [UIImage createNonInterpolatedUIImageFormCIImage:[UIImage createQRForString:self.urlStr] withSize:250.0f];
    UIImage *customQrcode = [UIImage imageBlackToTransparent:qrcode withRed:60.0f andGreen:74.0f andBlue:89.0f];
    self.imageViewQRCode.image = customQrcode;
    // set shadow
    self.imageViewQRCode.layer.shadowOffset = CGSizeMake(0, 2);
    self.imageViewQRCode.layer.shadowRadius = 2;
    self.imageViewQRCode.layer.shadowColor = [UIColor blackColor].CGColor;
    self.imageViewQRCode.layer.shadowOpacity = 0.5;
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

#pragma mark =========== actions ===========

//保存到相册
- (IBAction)actionSafeToAlbum:(id)sender {
    self.buttonSave.userInteractionEnabled = NO; //防止多次点击
    UIImageWriteToSavedPhotosAlbum( self.imageViewQRCode.image, self, @selector(image:didFinishSavingWithError:contextInfo:) , nil ) ;
}

//分享
- (IBAction)actionShare:(id)sender {
    [[ShareInstance shareInstace] shareShopWithTitle:self.shopName shopDescription:self.shopDescription url:self.urlStr image:self.shopImage imageUrl:self.shopImageUrl];
}


- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
  contextInfo:(void *)contextInfo
{
    // Was there an error?
    if (error != NULL)
    {

        WS(weakSelf);
        [MBProgressHUD Javen_showError:@"保存失败！" onView:self.view delay:0.4 complete:^{
            weakSelf.buttonSave.userInteractionEnabled = YES;
        }];
        
    }
    else  // No errors
    {
        
        WS(weakSelf)
        [MBProgressHUD Javen_showSuccess:@"保存成功！" onView:self.view delay:0.4 complete:^{
            weakSelf.buttonSave.userInteractionEnabled = YES;
        }];
        // Show message image successfully saved
    }
}



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

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