#import "NetConfigurationViewController.h"

#ifdef MPOS_XCFRAMEWORK
@import frameworkMPosSDK;

#else
#import "MPosDeviceInfo.h"
#import "UDPSetting.h"
#endif

#import "AppDelegate.h"

@interface NetConfigurationViewController ()
@property (weak) IBOutlet NSButton *swDHCP;
@property (weak) IBOutlet NSTextField *tfIP;
@property (weak) IBOutlet NSTextField *tfSubnet;
@property (weak) IBOutlet NSTextField *tfGateway;
@property (weak) IBOutlet NSTextField *tfPortNumber;
@property (weak) IBOutlet NSTextField *tfInactivityTime;
@property (weak) IBOutlet NSTextField *tfSystemName;
@property (weak) IBOutlet NSView *vSystemName;

@property MPosDeviceInfo* targetDevice;
@end

@implementation NetConfigurationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    AppDelegate* ad = (AppDelegate*)[[NSApplication sharedApplication] delegate];
    if (ad.networkDevice != nil) {
        _targetDevice = ad.networkDevice;
        _swDHCP.state = _targetDevice.dhcp.integerValue == 1 ? NSControlStateValueOn : NSControlStateValueOff;
        _tfIP.stringValue = _targetDevice.address;
        _tfSubnet.stringValue = _targetDevice.subnetmask;
        _tfGateway.stringValue = _targetDevice.gateway;
        _tfPortNumber.stringValue = [NSString stringWithFormat:@"%ld", _targetDevice.portNumber.integerValue];
        _tfInactivityTime.stringValue = [NSString stringWithFormat:@"%ld", _targetDevice.inactivityTime.integerValue];
        
        [self setEnable: _swDHCP.state == NSControlStateValueOn];
        if (_targetDevice.name.length == 0) {
            [_vSystemName setHidden: YES];
        }else{
            _tfSystemName.stringValue = _targetDevice.name;
        }
    }else{
        [self dismissViewController:self];
    }
}

- (void)setEnable:(BOOL)isDhcp {
    if (isDhcp) {
        _tfIP.stringValue = _targetDevice.address;
        _tfSubnet.stringValue = _targetDevice.subnetmask;
        _tfGateway.stringValue = _targetDevice.gateway;
    }
    [_tfIP setEnabled:!isDhcp];
    [_tfSubnet setEnabled:!isDhcp];
    [_tfGateway setEnabled:!isDhcp];
}

- (IBAction)onDhcpSwitch:(id)sender {
    [self setEnable:_swDHCP.state == NSControlStateValueOn];
}

- (void) showMessageBox:(NSString*)message style:(NSAlertStyle)style {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSAlert *alert = [NSAlert new];
        alert.alertStyle = style;
        alert.messageText = message;
        [alert runModal];
    });
}

- (IBAction)onSetup:(id)sender {
    MPosDeviceInfo* targetDevice = _targetDevice;
    NSString* ipAddress = _tfIP.stringValue;
    NSString* subnetMask = _tfSubnet.stringValue;
    NSString* gateway = _tfGateway.stringValue;
    NSNumber* useDhcp = _swDHCP.state == NSControlStateValueOn ? @1 : @0;
    NSNumber* portNumber = [NSNumber numberWithInteger:_tfPortNumber.stringValue.integerValue];
    NSNumber* inactivityTime = [NSNumber numberWithInteger:_tfInactivityTime.stringValue.integerValue];
    NSString* systemName = _tfSystemName.stringValue;
    NSNumber* timeout = [NSNumber numberWithDouble:5.0];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        MPOS_RESULT result = [UDPSetting setNetworkInfo:targetDevice
                                              ipAddress:ipAddress
                                             subnetMask:subnetMask
                                                gateway:gateway
                                                useDhcp:useDhcp
                                             portNumber:portNumber
                                         inactivityTime:inactivityTime
                                             systemName:systemName
                                                timeout:timeout];
        
        if (result == MPOS_SUCCESS) {
            [self showMessageBox:@"setNetworkDevice success" style:NSAlertStyleInformational];
        }else{
            [self showMessageBox:@"setNetworkDevice failed" style:NSAlertStyleCritical];
        }
    });
}

- (IBAction)onClose:(id)sender {
    [self dismissViewController:self];
}

@end
