//
//  KWMDiscountVC.m
//  iCemarose
//
//  Created by HouWeiBin on 16/9/2.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMDiscountVC.h"
#import "KWMStringUtil.h"

@interface KWMDiscountVC ()

@end

@implementation KWMDiscountVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"输入优惠码";
    [self initDiscountTF];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickBackgroundView:)];
    [self.view addGestureRecognizer:tapGesture];
}

- (void)onClickBackgroundView:(UITapGestureRecognizer *)tapGesture{
    [self.tfDiscount resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

+(NSString *)kwmTag{
    return @"KWMDiscountVC";
}

//初始化優惠碼textfiled
- (void)initDiscountTF{
    UILabel *leftPaddingView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 13, 25)];
    UILabel *rightPaddingView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 25)];
    self.tfDiscount.leftView = leftPaddingView;
    self.tfDiscount.rightView = rightPaddingView;
    self.tfDiscount.leftViewMode = UITextFieldViewModeAlways;
    self.tfDiscount.rightViewMode = UITextFieldViewModeAlways;
    
    [self.tfDiscount addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

    self.tfDiscount.delegate = self;
    
}



-(void)textFieldDidChange :(UITextField *)theTextField{
    if([KWMStringUtil isEmpty:theTextField.text]){
        self.btnClear.hidden = YES;
    }else{
        self.btnClear.hidden = NO;
    }
    NSLog( @"text changed: %@", theTextField.text);
}

-(void)onClickClear:(id)sender{
    self.tfDiscount.text = @"";
    self.btnClear.hidden = YES;
}

-(void)onClickAdd:(id)sender{
    NSString *code = self.tfDiscount.text;
    if([KWMStringUtil isEmpty:code]){
        [self showToast:@"请输入优惠码~"];
    }else{
        [self addDiscountVC:code];
    }
}

-(void)addDiscountVC:(NSString *)code{
    if([KWMStringUtil isEmpty:code]){
        return;
    }
    [self showLoading];

    BUYDiscount *discount = [self.client.modelManager discountWithCode:code];
    self.checkout.discount = discount;
    self.checkout.attributes = nil;
    [self.client updateCheckout:self.checkout completion:^(NSDictionary *dictionary, BUYCheckout *checkout, NSError *error) {
        [self hideLoading];
        if (error == nil && checkout) {
            [self showToast:@"添加优惠码成功"];
            NSLog(@"Successfully added discount");
            self.checkout = checkout;
            if(self.delegate != nil){
                [self.delegate kwm_addedDiscount:checkout];
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
        else {
            self.checkout.discount = nil;
            [self clearDiscount];
            if(self.delegate != nil){
                [self.delegate kwm_addedDiscount:self.checkout];
            }
            // 八成是因为checkout是web生成的所以如果discount如果传的有问题返回的是500如果没问题的话是正常的
            if ([@"Internal Server Error" isEqualToString:error.userInfo[@"errors"]]) {
                [self showToast:@"您输入的优惠码有误"];
            }else {
                [self showError:error];
            }
//            [self showToast:@"添加优惠码失败"];
            NSLog(@"Error applying checkout: %@", error);
        }
    }];
}

-(void)clearDiscount{
    [self showLoading];
    [self.client updateCheckout:self.checkout completion:^(NSDictionary *dictionary, BUYCheckout *checkout, NSError *error) {
        [self hideLoading];
     }];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHANUM] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    return [string isEqualToString:filtered];
}

@end