//
//  PayView.m
//  ALand
//
//  Created by Javen on 16/8/19.
//  Copyright © 2016年 Z. All rights reserved.
//

#import "PayView.h"
#import "CoverShadowView.h"
#import <ifaddrs.h>
#import <arpa/inet.h>

/**
 *  目前仅支持微信和支付宝 = ['weixin', 'alipay', 'unionpay', 'applePay', 'pingplus', 'deposit'],
 */
static CGFloat const H = 100;
static NSString *const chanelWX = @"weixin";
static NSString *const chanelAlipay = @"alipay";
typedef NS_ENUM(NSInteger, payType) {
    payTypeAlipay = 0,
    payTypeWepay,
};
@interface PayView ()
@property (nonatomic, strong) CoverShadowView *cover;
@end
@implementation PayView

- (void)awakeFromNib {
    
}

- (void)hide
{
    
    [self.cover hide];
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(0, kHeight, kWidth, H);
    }];
    
}

- (void)show {
    [self.cover show];
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(0, kHeight - H, kWidth, H);
    }];
    
    WS(weakSelf)
    self.cover.tapAction = ^{
        weakSelf.completion(nil, nil);
        [weakSelf hide];
    };
}

- (IBAction)payWithWX:(id)sender {
    [self payWithChannel:chanelWX];
    
}

- (IBAction)payWithAlipay:(id)sender {
    [self payWithChannel:chanelAlipay];
}


- (void)payWithChannel:(NSString *)chanel {
    NSDictionary *params = @{@"operCtx" : [[UserInfo shareInstance] getOperCtx],
                             @"pingppReq" : @{@"billUuid": self.billUuid ? self.billUuid : [NSNull null] ,
                                              @"ownerBillNumber" : self.ownerBillNumber ? self.ownerBillNumber : [NSNull null],
                                              @"payMethod" : chanel,
                                              @"clientIp" : [self getIPAddress],
                                              @"alipayExtra" : [NSNull null],
                                              @"wxpayExtra" : [NSNull null]}};
    [[HTTPCilent shareCilent] POST:@"order/prepay" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        NSString *charge = responseObject[@"pingppCharge"];
        [Pingpp createPayment:charge appURLScheme:@"wxb7c76f3ffc6b6e5e" withCompletion:self.completion];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}


- (NSString *)getIPAddress {
    NSString *address = @"error";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0) {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while(temp_addr != NULL) {
            if(temp_addr->ifa_addr->sa_family == AF_INET) {
                // Check if interface is en0 which is the wifi connection on the iPhone
                if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            }
            temp_addr = temp_addr->ifa_next;
        }
    }
    // Free memory
    freeifaddrs(interfaces);
    return address;
}


- (CoverShadowView *)cover
{
    if (!_cover) {
        _cover = [[CoverShadowView alloc] initOnTopWithHeight:kHeight];
        [kWindow addSubview:_cover];
        
        self.top = kHeight;
        [kWindow addSubview:self];
    }
    return _cover;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
