//
//  IntroViewController.m
//  kneet2
//
//  Created by Jason Lee on 9/16/15.
//  Copyright (c) 2015 Jason Lee. All rights reserved.
//
#import "RequestHandler.h"
#import "IntroViewController.h"


@interface IntroViewController () {
    
}
@end

#pragma mark - Class Definition

@implementation IntroViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initUI];
    
    
    NSLog(@"%s\n applicationState = %zd", __PRETTY_FUNCTION__, [UIApplication sharedApplication].applicationState);
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive) {
        [self prepareViewDidLoad];
    }
}


- (void)initUI {
    NSString *test = @"test";
    
    NSLog(@"%s\n %@", __PRETTY_FUNCTION__, test);
    DDLogError(@"test colors");
}

- (void)prepareViewDidLoad {
    
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear: animated];
    
    [self performSelector:@selector(requestVersionInfo) withObject:nil afterDelay:0.0f];
//    
//#ifndef PRODUCT_MODE
//    
//    ServerPopTableView *spopup = [[ServerPopTableView alloc] initFromNib];
//    [spopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
//        if (buttonIndex == 0) {//OK
//            [self performSelector:@selector(requestVersionInfo) withObject:nil afterDelay:0.0f];
//        }
//    }];
//    
//#else
//    
//    [self performSelector:@selector(requestVersionInfo) withObject:nil afterDelay:0.0f];
//#endif
}

#pragma mark - Main Logic
- (void)requestVersionInfo {
    
    //parameters
    NSDictionary *parameter = @{@"os_type": MOBILE_DEVICE_TYPE,
                                @"store_code": APPSTORE_TYPE,
                                @"app_version": [CommonUtil applicationShortVersion]};
    
    NSString *path = [NSString stringWithFormat:API_GET_APP_VERSION];
    
    [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
        if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
            return;
        }
        
        JDJSONModel *result = (JDJSONModel *) responseObject;
        
        if (result) {//API 성공 ,
            if ([result.resultCode isEqualToString:@"99"]) {//사용불가
                //앱 업그레이드 유도
                [[JDFacade facade] alert:MSG_ALERT_APP_UPDATE completionHander:^{
                    NSString *appURL = URL_APP_UPDATE;
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appURL]];
                }];
            } else {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"LoginViewController" storyboardName:@"Main"];
                    [JDFacade facade].appDelegate.window.rootViewController = vc;
                });
            }
        }
    } failure:^(id errorObject) {
        JDErrorModel *error = (JDErrorModel *)errorObject;
        [[JDFacade facade] alert:error.errorMessage];
    }];
}



#pragma mark - UI Events



#pragma mark - MemoryWarning
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end