/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

#import "AppDelegate.h"

#import "RCTRootView.h"
#import <UIKit/UIKit.h>
#import <Bugly/Bugly.h>


@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{


  NSURL *jsCodeLocation;

  /**
   * Loading JavaScript code - uncomment the one you want.
   *
   * OPTION 1
   * Load from development server. Start the server from the repository root:
   *
   * $ npm start
   *
   * To run on device, change `localhost` to the IP address of your computer
   * (you can get this by typing `ifconfig` into the terminal and selecting the
   * `inet` value under `en0:`) and make sure your computer and iOS device are
   * on the same Wi-Fi network.
   */
  jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];

  /**
   * OPTION 2
   * Load from pre-bundled file on disk. The static bundle is automatically
   * generated by the "Bundle React Native code and images" build step when
   * running the project on an actual device or running the project on the
   * simulator in the "Release" build configuration.
   */
# ifndef DEBUG
   jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
# endif

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"Yingping"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];

//  [self showBuildsNumber];

  [self path];

  return YES;
}

- (NSArray *)allURLSchemes{
  NSArray *urlTypes = [[NSBundle mainBundle] infoDictionary][@"CFBundleURLTypes"];
  NSMutableArray *urlSchemes = [NSMutableArray array];
  for (NSDictionary *type in urlTypes) {
    NSArray *schemes = type[@"CFBundleURLSchemes"];
    if (schemes.count > 0) {
      [urlSchemes addObjectsFromArray:schemes];
    }
  }
  return urlSchemes;
}


- (NSString *)path{
  NSArray *myPathList = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  NSString *mainPath    = [myPathList  objectAtIndex:0];

  NSString *bunddleID = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];

  NSLog(@"bunddle ID:%@",bunddleID);
  mainPath = [mainPath stringByAppendingPathComponent:bunddleID];
  mainPath = [mainPath stringByAppendingPathComponent:@"fsCachedData"];

  NSFileManager *fileManager = [NSFileManager defaultManager];
  NSError *error;
  BOOL fileExists = [fileManager fileExistsAtPath:mainPath];

  if (fileExists)
  {
    return mainPath;
//    BOOL success = [fileManager removeItemAtPath:mainPath error:&error];
//    if (!success) NSLog(@"Error: %@", [error localizedDescription]);

  }

  return nil;
}

- (void)showBuildsNumber{

    NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, 20)];
    label.text = [NSString stringWithFormat:@"Builds %@",info[@"CFBundleVersion"]];
    label.font = [UIFont systemFontOfSize:9];
    label.textColor = RGB(255, 255, 255);
    label.textAlignment = NSTextAlignmentCenter;

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
      [self.window addSubview:label];
    });

}

@end
