//
//  InquireCarViewController.m
//  goodpk
//
//  Created by 歐特儀 on 2020/2/11.
//  Copyright © 2020 Altob. All rights reserved.
//

#import "InquireCarViewController.h"
#import "InquireCarModel.h"
#import "CocoaSecurity.h"
#import "Util.h"
#import <CommonCrypto/CommonDigest.h>

#define APIURL @"https://cloudservice.altob.com.tw/LockCarSerivce/api/JumpApi"
#define CARWHERE @"/CARWHERE?carNo="
#define ALPHANUM @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

@interface InquireCarViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton *okBtn;
@property (weak, nonatomic) IBOutlet UITextField *inputText;
@property (weak, nonatomic) IBOutlet UIImageView *ImgView;
@property (weak, nonatomic) IBOutlet UILabel *stationName;
@property (weak, nonatomic) IBOutlet UILabel *inTime;
@property (weak, nonatomic) IBOutlet UIView *FootView;

@end

@implementation InquireCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"車輛位置查詢";
    
    [self.okBtn addTarget:self
            action:@selector(okButtonClicked:)
            forControlEvents:UIControlEventTouchUpInside
    ];
    
    //注册截圖通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(userDidTakeScreenshot:)
                                                 name:UIApplicationUserDidTakeScreenshotNotification object:nil];
    
    self.inputText.delegate = self;
    self.inputText.keyboardType = UIKeyboardTypeASCIICapable;
}

- (BOOL)textField:(UITextField* )textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString* )string
{
    if (textField == self.inputText)
    {
       NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHANUM] invertedSet];
       NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        if([string isEqualToString:filtered]){
            NSUInteger newLength = [textField.text length] + [string length] - range.length;
            return (newLength > 10 ) ? NO : YES;
        }else{
            return NO;
        }
    }
    else
    {
      return true;
    }

}

- (void) okButtonClicked:(id)sender {
    if([self.inputText.text length] > 0){
        [self posCarNo:[self.inputText.text uppercaseStringWithLocale:[NSLocale currentLocale]]];
        [[[UIApplication sharedApplication] keyWindow] endEditing: YES];
    }else{
        [self showAlert:@"錯誤" :@"請輸入車牌" :nil];
    }
}

-(void) posCarNo:(NSString*)carNo{
    
    CocoaSecurityResult *NcarNo = [CocoaSecurity aesEncrypt:carNo key:[Util sha256HashFor:@"Altob"]];
    NSString *carUrl = [NSString stringWithFormat:@"%@%@%@", APIURL,CARWHERE, NcarNo.base64];
    
    //第一步，创建URL
    NSURL *url = [NSURL URLWithString:carUrl];
    //NSDictionary *jsonBodyDict = @{};
    
    //第二步，创建请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    //设置请求方式为POST，默认为GET
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"Bearer 9a406a82bd551e6ef8e845f42f788af4" forHTTPHeaderField:@"Authorization"];
    [request setHTTPMethod:@"GET"];
    
    //设置参数
    //NSData *jsonBodyData = [NSJSONSerialization dataWithJSONObject:jsonBodyDict options:kNilOptions error:nil];
    //[request setHTTPBody:jsonBodyData];
    
    //新作法
    NSURLSession *sesson = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
    
    // 2.创建 NSURLSessionDataTask
    NSURLSessionDataTask *dataTask = [sesson dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
         if (error) {
             // error
             dispatch_async(dispatch_get_main_queue(), ^{
                 [self showAlert: @"注意" : @"您所使用的連線加密(SSL)異常，基於安全考量不提供相關功能。請確認您的連線環境及App下載來源安全。" : 0];
             });
         }else {
             // 获得数据后，返回到主线程更新 UI
             dispatch_async(dispatch_get_main_queue(), ^{
                 NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                 InquireCarModel *ICM = [[InquireCarModel alloc] initWithString:responseString error:nil];
                 if([ICM.result_code isEqual:@"OK"]){
                     
                     //NSLog(@"參數查詢：%@", ICM.station_name);
                     //NSLog(@"參數查詢：%@", ICM.in_time);
                     //NSLog(@"參數查詢：%@", ICM.image_url);

                     //顯示UI
                     [self.FootView setHidden:NO];
                     //放資料
                     [self.stationName setText:ICM.station_name];
                     [self.inTime setText:ICM.in_time];
                     
                     if(ICM.image_url2 != nil && ![ICM.image_url2 isEqualToString:@""]){
                         //下載圖片_非同步
                         dispatch_async(dispatch_get_global_queue(0,0), ^{
                             NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:ICM.image_url2]];
                             //NSLog(@"參數查詢：%@", data);
                             if ( data == nil )
                                 return;
                             dispatch_async(dispatch_get_main_queue(), ^{
                                 // WARNING: is the cell still using the same data by this point??
                                 self.ImgView.image = [UIImage imageWithData: data];
                             });
                           
                         });
                     }else{
                      
                         //下載圖片_非同步
                         dispatch_async(dispatch_get_global_queue(0,0), ^{
                             NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:ICM.image_url]];
                             //NSLog(@"參數查詢：%@", data);
                             if ( data == nil )
                                 return;
                             dispatch_async(dispatch_get_main_queue(), ^{
                                 // WARNING: is the cell still using the same data by this point??
                                 self.ImgView.image = [UIImage imageWithData: data];
                             });
                           
                         });
                     }
                     
                 }else {
                     [self showAlert:@"錯誤" :@"沒有資料,請確認車牌輸入正確或網路連接正常" :nil];
                 }
             });
         }
    }];
    // 3.执行 Task
    [dataTask resume];
    
    /*
    //第三步，连接服务器
    NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    NSString *jsonString = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
    //NSLog(@"ticket:%@", jsonString);
    
    InquireCarModel *ICM = [[InquireCarModel alloc] initWithString:jsonString error:nil];
    if([ICM.result_code isEqual:@"OK"]){
        
        //NSLog(@"參數查詢：%@", ICM.station_name);
        //NSLog(@"參數查詢：%@", ICM.in_time);
        //NSLog(@"參數查詢：%@", ICM.image_url);

        //顯示UI
        [self.FootView setHidden:NO];
        //放資料
        [self.stationName setText:ICM.station_name];
        [self.inTime setText:ICM.in_time];
        
        if(ICM.image_url2 != nil && ![ICM.image_url2 isEqualToString:@""]){
            //下載圖片_非同步
            dispatch_async(dispatch_get_global_queue(0,0), ^{
                NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:ICM.image_url2]];
                //NSLog(@"參數查詢：%@", data);
                if ( data == nil )
                    return;
                dispatch_async(dispatch_get_main_queue(), ^{
                    // WARNING: is the cell still using the same data by this point??
                    self.ImgView.image = [UIImage imageWithData: data];
                });
              
            });
        }else{
         
            //下載圖片_非同步
            dispatch_async(dispatch_get_global_queue(0,0), ^{
                NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:ICM.image_url]];
                //NSLog(@"參數查詢：%@", data);
                if ( data == nil )
                    return;
                dispatch_async(dispatch_get_main_queue(), ^{
                    // WARNING: is the cell still using the same data by this point??
                    self.ImgView.image = [UIImage imageWithData: data];
                });
              
            });
        }
        
    }else {
        [self showAlert:@"錯誤" :@"沒有資料,請確認車牌輸入正確或網路連接正常" :nil];
    }
    */
}

-(void)showAlert:(NSString *)title :(NSString *)text: (NSInteger *)type{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
                                        message:text
                                        preferredStyle:UIAlertControllerStyleAlert ];
    
    UIAlertAction *sinupAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
       
    }];
    [alertController addAction:sinupAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

//截屏响应
- (void)userDidTakeScreenshot:(NSNotification *)notification
{
    [self showAlert: @"注意" : @"偵測到截圖,請妥善保管截圖,避免重要資訊外流" : 0];
}

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler//通过调用block，来告诉NSURLSession要不要收到这个证书
{
   // Get remote certificate
   SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
   SecCertificateRef certificate = SecTrustGetCertificateAtIndex(serverTrust, 0);
    SecKeyRef serverPublicKey = SecCertificateCopyPublicKey(certificate);
    
    
   // Set SSL policies for domain name check
   NSMutableArray *policies = [NSMutableArray array];
   [policies addObject:(__bridge_transfer id)SecPolicyCreateSSL(true, (__bridge CFStringRef)challenge.protectionSpace.host)];
   SecTrustSetPolicies(serverTrust, (__bridge CFArrayRef)policies);

   // Evaluate server certificate
   SecTrustResultType result;
   SecTrustEvaluate(serverTrust, &result);
    
   BOOL certificateIsValid = (result == kSecTrustResultUnspecified || result == kSecTrustResultProceed);
    
   // Get local and remote cert data
   NSData *remoteCertificateData = CFBridgingRelease(SecCertificateCopyData(certificate));
    SecCertificateRef cer = SecCertificateCreateWithData ( NULL, (__bridge CFDataRef) remoteCertificateData);
    NSLog(@"%@", cer);
    
    NSString *certPath = [[NSBundle mainBundle] pathForResource:@"server" ofType:@"cer"];
    NSData *certData = [NSData dataWithContentsOfFile:certPath];
    SecCertificateRef localCertificate = SecCertificateCreateWithData(nil, (CFDataRef)certData);
    NSLog(@"localCertificate: %@", localCertificate);
    
    NSString *hash = [[self doSha256:remoteCertificateData] base64EncodedStringWithOptions:0];
    
   // The pinnning check
   if ([hash isEqualToString:@"FHloZIw4i6+30lmxrUujLieHlIDpxHySL1niMxvgmpU="] && certificateIsValid) {
       NSURLCredential *credential = [NSURLCredential credentialForTrust:serverTrust];
       completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
   } else {
       completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, NULL);
   }
}

- (NSData *)doSha256:(NSData *)dataIn {
    NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH];
    CC_SHA256(dataIn.bytes, dataIn.length, macOut.mutableBytes);
    return macOut;
}

@end
