// See LICENSE file

#import <Foundation/Foundation.h>
#import "JXcore.h"
#import "JXMobile.h"
#import "CDVJXcore.h"
#import "Reachability.h"

@implementation JXMobile
{}

Reachability* reachability;

+ (void)initialize {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    reachability = [Reachability reachabilityForInternetConnection];
    [reachability startNotifier];
}

+ (void) reachabilityChanged:(NSNotification *)notice
{
    NetworkStatus status =  [reachability currentReachabilityStatus];
    NSString *strStatus = [JXMobile reachabilityStatusString:(status)];
    [JXcore Evaluate:[NSString stringWithFormat:@"process.emit('connectionStatusChanged','%@');",strStatus]];
}

+ (NSString *) reachabilityStatusString:(NetworkStatus)_status {

    NSString *strStatus;
    if(_status == NotReachable)
    {
        //No internet
        strStatus = @"NotConnected";
    }
    else if (_status == ReachableViaWiFi)
    {
        //WiFi
        strStatus = @"WiFi";
    }
    else if (_status == ReachableViaWWAN)
    {
        //3G
        strStatus = @"WWAN";
    }

    return strStatus;
}

+ (void) defineMethods {
  // Listen to Errors on the JS land
  [JXcore addNativeBlock:^(NSArray *params, NSString *callbackId) {
    NSString *errorMessage = (NSString*)[params objectAtIndex:0];
    NSString *errorStack = (NSString*)[params objectAtIndex:1];

    NSLog(@"Error!: %@\nStack:%@\n", errorMessage, errorStack);
  } withName:@"OnError"];

  // User documents location
  [JXcore addNativeBlock:^(NSArray *params, NSString *callbackId) {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    [JXcore callEventCallback:callbackId withJSON:[NSString stringWithFormat:@"\"%@\"",documentsDirectory]];
  } withName:@"GetDocumentsPath"];

  [JXcore addNativeBlock:^(NSArray *params, NSString *callbackId) {
      NetworkStatus status = [reachability currentReachabilityStatus];
      NSString *strStatus = [JXMobile reachabilityStatusString:(status)];
      [JXcore callEventCallback:callbackId withJSON:[NSString stringWithFormat:@"{\"%@\":1}",strStatus]];
  } withName:@"GetConnectionStatus"];

  // Manufacturer - Device Name
  [JXcore addNativeBlock:^(NSArray *params, NSString *callbackId) {
    NSString *deviceName = [[UIDevice currentDevice] name];
    [JXcore callEventCallback:callbackId withJSON:[NSString stringWithFormat:@"\"Apple-%@\"",deviceName]];
  } withName:@"GetDeviceName"];

  [JXcore addNativeBlock:^(NSArray *params, NSString *callbackId) {
          NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
                             stringByAppendingPathComponent:@"database"];
          NSLog(@"%@",path);
          NSFileManager *fileManager = [NSFileManager defaultManager];
          BOOL isDir = FALSE;
          BOOL isDirExist = [fileManager fileExistsAtPath:path isDirectory:&isDir];
          if(!(isDirExist && isDir)) {
                BOOL bCreateDir = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
                if(!bCreateDir){
                      NSLog(@"创建文件夹失败！");
                }
                NSLog(@"创建文件夹成功，文件路径%@", path);
          }

          [JXcore callEventCallback:callbackId withJSON:[NSString stringWithFormat:@"\"%@\"", path]];

  } withName:@"GetDocumentPath"];


  [JXcore addNativeBlock:^(NSArray *params, NSString *callbackId) {
        NSURL *url = [NSURL URLWithString:@"App-Prefs:root=WIFI"];
        if ([[UIApplication sharedApplication] canOpenURL:url])  {
              [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
                   if (success) {
                       [JXcore callEventCallback:callbackId withJSON:[NSString stringWithFormat:@"\"%@\"", @"YES"]];
                   } else {
                       [JXcore callEventCallback:callbackId withJSON:[NSString stringWithFormat:@"\"%@\"", @"NO"]];
                   }
              }];
        }

  } withName:@"ShowWIFIConfig"];
}
@end
