//
//  KWMShippingVC.m
//  iCemarose
//
//  Created by HouWeiBin on 2016/11/4.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMShippingVC.h"
#import "KWMShippingCell.h"

@implementation KWMShippingVC

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTitle];
    //獲取配送方式數據
    [self requestShippingData];
}

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

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

- (void)initTitle{
    self.title = @"配送方式";
}

-(void)requestShippingData{
    if(self.checkout==nil){
        return;
    }
    [self showLoading];
    [self.client getShippingRatesForCheckoutWithToken:self.checkout.token completion:^(NSArray *shippingRates, BUYStatus status, NSError *error) {
        [self hideLoading];
        if (shippingRates!=nil && [shippingRates count] > 0 && !error) {
            self.shippingRates = shippingRates;
            [self.tbvShipping reloadData];
        }
        else {
            [self showError:error];
            NSLog(@"Failed to retrieve shipping rates: %@", error);
        }
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    }];
}

//设置配送费到订单
-(void)setShippingToCheckout:(BUYShippingRate *)shippingRate{
    if(shippingRate == nil){
        return;
    }
    [self showLoading];
    [self.checkout setShippingRate:shippingRate];
    [self.client updateCheckout:self.checkout completion:^(NSDictionary *dictionary, BUYCheckout *checkout, NSError *error) {
        [self hideLoading];
        if (error == nil && checkout) {
            [self showToast:@"设置配送方式成功"];
            if(self.delegate!=nil){
                [self.delegate kwm_selectShippingRate:checkout];
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
        else {
            [self showError:error];
            [self showToast:@"设置配送方式失败"];
            NSLog(@"Error applying checkout: %@", error);
        }
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(self.shippingRates!=nil){
        return self.shippingRates.count;
    }else{
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indentifier = @"KWMShippingCell";
    KWMShippingCell *shippingCell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!shippingCell) {
        [tableView registerNib:[UINib nibWithNibName:indentifier bundle:nil] forCellReuseIdentifier:indentifier];
        shippingCell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    }
    if(self.shippingRates!=nil){
        BUYShippingRate *shippingRate =[self.shippingRates objectAtIndex:indexPath.row];
        [shippingCell setData:shippingRate];
        [shippingCell setSelected:NO];
        if(shippingRate!=nil && self.checkout!=nil && self.checkout.shippingRate!=nil){
            NSString *identity1 = shippingRate.shippingRateIdentifier;
            NSString *identity2 = self.checkout.shippingRate.shippingRateIdentifier;

            if(identity1!=nil && identity2!=nil && [identity1 isEqualToString:identity2]){
                [shippingCell setShipSelected:YES];
            }
        }
    }
    
    return shippingCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.shippingRates!=nil){
        BUYShippingRate *shippingRate =[self.shippingRates objectAtIndex:indexPath.row];
        if(shippingRate){
            [self setShippingToCheckout:shippingRate];
        }
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    
}



@end