#import "ReceiptViewController.h"

@interface ReceiptViewController()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIImageView *receiptResultImageView;
@property (weak, nonatomic) IBOutlet UILabel *receiptResultLabel;
@property (weak, nonatomic) IBOutlet UIButton *showDetailsButton;
@property (weak, nonatomic) IBOutlet UIView *signatureView;
@property (weak, nonatomic) IBOutlet SignatureCanvas *signatureCanvas;
@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet UIButton *updateButton;

@end


@implementation ReceiptViewController
{
    
    NSMutableArray *tableData;
    BOOL hideCells;
    NSString* amount;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    [self setTheme];
    hideCells = YES;
    amount = [_coreResponse.authorizedAmount stringByAppendingString:getCurrencyData(_coreResponse.currency)[2]];
    if([_coreResponse.code isEqualToString:@"A"]){
        _receiptResultImageView.image = [UIImage imageNamed:@"approval"];
        _receiptResultLabel.text = @"APPROVED";
        _receiptResultLabel.textColor = UIColorFromRGB(0x4CAE4E);
    } else {
        _receiptResultImageView.image = [UIImage imageNamed:@"decline"];
        _receiptResultLabel.text = @"DECLINED";
        _receiptResultLabel.textColor = UIColorFromRGB(0xC11D03);
    }
    if ([_coreResponse isKindOfClass:[CoreSaleResponse class]]){
        _updateButton.hidden = NO;
    } else {
        _updateButton.hidden = YES;
    }
    _tableView.estimatedRowHeight = 40.0;
    _tableView.rowHeight = UITableViewAutomaticDimension;
    [self receiptData];
}

- (void) receiptData{
    NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"en-US"];
    NSNumberFormatter *formatter = [NSNumberFormatter new];
    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [formatter setLocale:locale];

    tableData = [[NSMutableArray alloc] init];
    if(!hideCells){
        [tableData addObject:[NSString stringWithFormat:@"Amount ;%@" , amount]];
        [tableData addObject:[NSString stringWithFormat:@"Card Number ;%@" ,  _coreResponse.cardNumber]];
        [tableData addObject:[NSString stringWithFormat:@"CardHolderName ;%@" , _coreResponse.cardHolderName]];
        [tableData addObject:[NSString stringWithFormat:@"Card Type ;%@" , _coreResponse.cardType]];
        if(_coreResponse.surcharge != nil && _coreResponse.surcharge.amount != nil) {
            [tableData addObject:[NSString stringWithFormat:@"Surcharge ;%@" , [formatter stringFromNumber:_coreResponse.surcharge.amount]]];
        }
        if(_coreResponse.tip != nil && _coreResponse.tip.amount!= nil) {
            [tableData addObject:[NSString stringWithFormat:@"Tip ;%@" , [formatter stringFromNumber:_coreResponse.tip.amount]]];
        }
        [tableData addObject:[NSString stringWithFormat:@"Currency ;%@" ,  getCurrencyData(_coreResponse.currency)[2]]];
        [tableData addObject:[NSString stringWithFormat:@"UniqueRef ;%@" , _coreResponse.uniqueRef]];
        [tableData addObject:[NSString stringWithFormat:@"Expiry Date ;%@" , _coreResponse.expiryDate]];
        [tableData addObject:[NSString stringWithFormat:@"Description ;%@" ,  _coreResponse.descriptionCode]];
        if([_coreResponse.approvalCode length] != 0){
            [tableData addObject:[NSString stringWithFormat:@"Approval Code ;%@" ,  _coreResponse.approvalCode]];
        }
        if(_coreResponse.orderNumber != nil){
            [tableData addObject:[NSString stringWithFormat:@"Order Number ;%@" ,  _coreResponse.orderNumber]];
        }
        [tableData addObject:[NSString stringWithFormat:@"Date ;%@" , _coreResponse.dateTime]];
        [tableData addObject:[NSString stringWithFormat:@"Code ;%@" ,  _coreResponse.code]];
    } else {
        [tableData addObject:[NSString stringWithFormat:@"Amount ;%@" , amount]];
        [tableData addObject:[NSString stringWithFormat:@"Card Number ;%@" , _coreResponse.cardNumber]];
    }
    [_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}

- (void) setTheme{
    self.navigationController.navigationBar.barTintColor = UIColorFromRGB(0xC11D03);
    self.tabBarController.tabBar.tintColor = UIColorFromRGB(0xC11D03);
}

- (void) back{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)updateHandle:(id)sender {
    _mainView.hidden = YES;
    _signatureCanvas.signature = [CoreSignature new];
    CATransition *animation = [CATransition animation];
    animation.duration = 1.0;
    animation.type = kCATransitionFade;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [_signatureView.layer addAnimation:animation forKey:@"changeTextTransition"];
    
    _signatureView.hidden = NO;
    [UIView animateWithDuration:0.25 animations:^{_signatureView.alpha = 1.0;}];
}

- (IBAction)submitSignatureHandle:(id)sender {
    if ([_signatureCanvas checkSignature]) {
        CoreUpdate* update = [CoreUpdate new];
        update.uniqueRef = _coreResponse.uniqueRef;
        update.signature = _signatureCanvas.signature;
        [[WTPSTerminal singleton] updateTransaction:update];
        
        [_signatureCanvas clearCanvas:true];
        _signatureView.hidden = YES;
        
        CATransition *animation = [CATransition animation];
        animation.duration = 1.0;
        animation.type = kCATransitionFade;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        [_mainView.layer addAnimation:animation forKey:@"changeTextTransition"];
        _mainView.hidden = NO;
    }else{
        [[UiHelper singleton] showWarning:@"Signature cannot be empty"];
    }
}

- (IBAction)showDetailsHandle:(id)sender {
    hideCells = !hideCells;
    if (hideCells) {
        [_showDetailsButton setTitle:@"SHOW DETAILS" forState:UIControlStateNormal];
    } else {
        [_showDetailsButton setTitle:@"HIDE DETAILS" forState:UIControlStateNormal];
    }
    [self receiptData];
}

- (IBAction)processAnotherPaymentHandle:(id)sender {
    [self back];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CardCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cardCell"];
    
    if (cell == nil) {
        cell = [[CardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cardCell"];
    }
    
    NSString *summary = [tableData objectAtIndex:indexPath.row];
    NSArray *array = [summary componentsSeparatedByString:@";"];
    cell.labelKey.text = [array objectAtIndex:0];
    cell.labelValue.text = [array objectAtIndex:1];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

@end
