#import "CDVPluginSntp.h"

@implementation CDVPluginSntp

- (void)setServer:(CDVInvokedUrlCommand*)command
{
    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    [self.commandDelegate sendPluginResult:pluginResult
                                callbackId:command.callbackId];
}

- (void)getTime:(CDVInvokedUrlCommand*)command
{
     NTPServer *server = [[NTPServer alloc] initWithHostname:@"pool.ntp.org" port:123];
    NSDate *date = [server dateWithError:nil];
    NSString *millis = [NSString stringWithFormat:@"%lld", [@(floor([date timeIntervalSince1970] * 1000)) longLongValue]] ;
    NSMutableDictionary *result = [NSMutableDictionary new];
    NSDateFormatter* formator = [[NSDateFormatter alloc]init];
    [formator setDateFormat:@"MM/dd/yyyy"];
    [result setObject:millis forKey:@"time"];
    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result];
    [self.commandDelegate sendPluginResult:pluginResult
                                callbackId:command.callbackId];
}

- (void)getTimeOffset:(CDVInvokedUrlCommand*)command
{
    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
                                                         messageAsInt:0];
    [self.commandDelegate sendPluginResult:pluginResult
                                callbackId:command.callbackId];
}

@end
