//
// SoldStockBarcodeViewController.m
// MyProject
//
// Created by haibara on 6/19/15.
//
//

#import "SoldStockBarcodeViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "SDK.h"
#import "ImageUtils.h"
#import "Vars.h"

@interface SoldStockBarcodeViewController ()
@property (nonatomic, weak) IBOutlet UILabel *tooltipLabel;
@property (nonatomic, weak) IBOutlet UILabel *infoLabel;
@property (nonatomic, weak) IBOutlet UIImageView *imageView;
@end

@implementation SoldStockBarcodeViewController

- (instancetype)init {
	if (self = [super init]) {
		// Custom initialization
		[[Vars appDelegate] applyDefaultConfigurationForViewController:self];
	}
	return self;
}

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view from its nib.
	if ([self.stock.entry.product.vendor_obj.availableMetadataObjectType isEqualToString:AVMetadataObjectTypeQRCode]) {
		self.title = @"QR Code";
	} else {
		self.title = @"Barcode";
	}

	self.tooltipLabel.text = [NSString stringWithFormat:@"Scan the %@ inside the viewfinder rectangle to check-in it", [self.stock.entry.product.vendor_obj.availableMetadataObjectType isEqualToString:AVMetadataObjectTypeQRCode] ? @"QR code" : @"barcode"];

	CIFilter *filter = [CIFilter filterWithName:self.stock.entry.product.vendor_obj.availableCodeGenerator];
	[filter setValue:[self.stock.barcode dataUsingEncoding:NSUTF8StringEncoding] forKey:@"inputMessage"];
	[filter setDefaults];
	self.imageView.image = [ImageUtils scaleCIIImage:filter.outputImage toSize:self.imageView.frame.size];

	NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
	[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:self.stock.sold_to_name attributes:@{ NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleBody] }]];
	[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\n"]];
	[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:self.stock.otherShortDescription attributes:@{ NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote] }]];
	self.infoLabel.attributedText = [[NSAttributedString alloc] initWithAttributedString:attributedString];
}

@end
