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

#import "ViewController.h"
#import "HelpInfoViewController.h"
#import "InquireCarViewController.h"
#import "CarSecurityViewController.h"
#import "PaymentViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *b1;
@property (weak, nonatomic) IBOutlet UIButton *b2;
@property (weak, nonatomic) IBOutlet UIButton *b3;
@property (weak, nonatomic) IBOutlet UIButton *b4;
@property (weak, nonatomic) IBOutlet UIButton *stationBtn;
@property (weak, nonatomic) IBOutlet UILabel *stationName;
@property (weak, nonatomic) IBOutlet UIImageView *EndBg;
@property NSUserDefaults* defaults;
@property NSMutableArray *StationData;
@property NSString* station;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitle:@"CAR IN! 智慧停車"];
    
    //注册截圖通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(userDidTakeScreenshot:)
                                                 name:UIApplicationUserDidTakeScreenshotNotification object:nil];
    
    self.StationData = @[
    @{@"name":@"市民廣場", @"code":@"40808"},
    @{@"name":@"新體", @"code":@"12143"},
    @{@"name":@"二重公園", @"code":@"40806"},
    @{@"name":@"金城", @"code":@"12117"},
    @{@"name":@"澳底", @"code":@"12145"}
    ];
    
    [self setBtnListener];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    self.defaults = [NSUserDefaults standardUserDefaults];
    
    if (! [self.defaults boolForKey:@"notFirstRun"]) {
        //App初次啟動
        [self showAlert];
    }
}

-(void) setBtnListener{
    
    [self.b1 setTag:0];
    [self.b2 setTag:1];
    [self.b3 setTag:2];
    [self.b4 setTag:3];
    
    [self.b1 addTarget:self
            action:@selector(handleButtonClicked:)
            forControlEvents:UIControlEventTouchUpInside
    ];
    
    [self.b2 addTarget:self
            action:@selector(handleButtonClicked:)
            forControlEvents:UIControlEventTouchUpInside
    ];
    
    [self.b3 addTarget:self
            action:@selector(handleButtonClicked:)
            forControlEvents:UIControlEventTouchUpInside
    ];
    
    [self.b4 addTarget:self
            action:@selector(handleButtonClicked:)
            forControlEvents:UIControlEventTouchUpInside
    ];
    
    [self.stationBtn addTarget:self
            action:@selector(stationButtonClicked:)
            forControlEvents:UIControlEventTouchUpInside
    ];
}

- (void) stationButtonClicked:(id)sender {
    [self showStationSheetAlert];
}

- (void) handleButtonClicked:(id)sender {
    
    if(self.station != nil && self.station.length > 0){
        //NSLog(@"test: %@", self.station);
        switch ([sender tag]) {
                    case 0:
                        //NSLog(@"button1 have been clicked.");
                        {
                            //查車
                            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"InquireCar" bundle:[NSBundle mainBundle]];
                            InquireCarViewController *InquireCarController = [storyboard instantiateViewControllerWithIdentifier:@"InquireCar"];
                            [self.navigationController popViewControllerAnimated: YES];
                            [self.navigationController pushViewController:InquireCarController animated:YES];
                        }
                        break;
                        
                    case 1:
                        //NSLog(@"button2 have been clicked.");
                        {
                            //鎖車
                            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"CarSecurity" bundle:[NSBundle mainBundle]];
                            CarSecurityViewController *CarSecurityController = [storyboard instantiateViewControllerWithIdentifier:@"CarSecurity"];
                            [self.navigationController popViewControllerAnimated: YES];
                            CarSecurityController.station = self.station;
                            [self.navigationController pushViewController:CarSecurityController animated:YES];
                        }
                        break;
                        
                    case 2:
                        //NSLog(@"button3 have been clicked.");
                        {
                            //求助
                            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Help" bundle:[NSBundle mainBundle]];
                            HelpInfoViewController *HelpController = [storyboard instantiateViewControllerWithIdentifier:@"Help"];
                            [self.navigationController popViewControllerAnimated: YES];
                            [self.navigationController pushViewController:HelpController animated:YES];
                        }
                        break;
                        
                    case 3:
                        //NSLog(@"button4 have been clicked.");
                        {
                            /*
                            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Payment" bundle:[NSBundle mainBundle]];
                            PaymentViewController *PaymentController = [storyboard instantiateViewControllerWithIdentifier:@"Payment"];
                            [self.navigationController popViewControllerAnimated: YES];
                            [self.navigationController pushViewController:PaymentController animated:YES];
                             */
                            [[UIApplication sharedApplication] openURL: [NSURL URLWithString:@"https://parks.altob.com.tw/opay.html"] options:nil completionHandler:nil];
                        }
                        break;
            default:
                break;
        }
    }else{
        [self showAlert2: @"注意" : @"請先選擇場站" : 0];
    }
}

-(void)showAlert{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"重要"
                                        message:@"本APP將會使用您的網路連線功能和電話權限於緊急協助中撥打電話給場站。在任何傳輸的過程中也會對您的資料進行加密。如不同意的話則無法使用完整的App功能"
                                        preferredStyle:UIAlertControllerStyleAlert ];
    
    //添加确定到UIAlertController中
    UIAlertAction *sinupAction = [UIAlertAction actionWithTitle:@"同意" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        [self.defaults setBool:YES forKey:@"notFirstRun"];
    }];
    
    [alertController addAction:sinupAction];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"不同意" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        [self.defaults setBool:NO forKey:@"notFirstRun"];
        exit(0);
    }];
    
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

-(void)showAlert2:(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) showStationSheetAlert{
    // 創建一個UIAlertController, 命名為alertThree, preferredStyle 設置為 UIAlertControllerStyleActionSheet
    UIAlertController *alertThree = [UIAlertController alertControllerWithTitle:@"場站" message:@"請選擇所在場站：" preferredStyle:UIAlertControllerStyleActionSheet];
    // 創建一個“取消”按鈕
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    // 將按鈕添加到alertThree上
    [alertThree addAction:cancelAction];
    
    // 根據陣列資料創建選項按鈕
    for(int i = 0 ; i < self.StationData.count; i++){
        UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:[[self.StationData objectAtIndex:i] valueForKey:@"name"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
            [self.stationName setText:[[self.StationData objectAtIndex:i] valueForKey:@"name"]];
            self.station = [[self.StationData objectAtIndex:i] valueForKey:@"code"];
        }];
        // 將按鈕添加到alertThree上
        [alertThree addAction:confirmAction];
    }
    
    alertThree.popoverPresentationController.sourceView = self.view;
    alertThree.popoverPresentationController.sourceRect = CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/2, 1.0, 1.0);
    
    // 彈出alertThree
    [self presentViewController:alertThree animated:YES completion:nil];
}

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

@end
