#import "NetConfigurationViewController.h"

#ifdef MPOS_XCFRAMEWORK
@import frameworkMPosSDK;

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

#import "AppDelegate.h"

@interface NetConfigurationViewController ()
@property (weak, nonatomic) IBOutlet UISwitch *swDHCP;
@property (weak, nonatomic) IBOutlet UITextField *tfIP;
@property (weak, nonatomic) IBOutlet UITextField *tfSubnet;
@property (weak, nonatomic) IBOutlet UITextField *tfGateway;
@property (weak, nonatomic) IBOutlet UITextField *tfPortNumber;
@property (weak, nonatomic) IBOutlet UITextField *tfInactivityTime;
@property (weak, nonatomic) IBOutlet UITextField *tfSystemName;
@property (weak, nonatomic) IBOutlet UIView *vSystemName;

@property MPosDeviceInfo* targetDevice;
@end

@implementation NetConfigurationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    AppDelegate* ad = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    if (ad.networkDevice != nil) {
        _targetDevice = ad.networkDevice;
        [_swDHCP setOn:_targetDevice.dhcp.integerValue != 0 animated: true];
        [_tfIP setText: _targetDevice.address];
        [_tfSubnet setText: _targetDevice.subnetmask];
        [_tfGateway setText: _targetDevice.gateway];
        [_tfPortNumber setText:[NSString stringWithFormat:@"%ld", _targetDevice.portNumber.integerValue]];
        [_tfInactivityTime setText:[NSString stringWithFormat:@"%ld", _targetDevice.inactivityTime.integerValue]];
        [self setEnable: _swDHCP.isOn];
        if (_targetDevice.interfaceType == MPOS_INTERFACE_WIFI) {
            [_tfSystemName setText: _targetDevice.name];
        }else{
            [_vSystemName setHidden: YES];
        }
    }else{
        [self.navigationController popViewControllerAnimated: YES];
    }
}

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

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

-(void)showMessageBox:(NSString*)message title:(NSString*)title {
    __weak NetConfigurationViewController* weakSelf  = self;
    dispatch_async(dispatch_get_main_queue(),^{
        __strong NetConfigurationViewController* strongSelf = weakSelf;
        UIAlertController* alert = [UIAlertController alertControllerWithTitle: title message:message preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler: nil];
        [alert addAction: defaultAction];
        [strongSelf presentViewController:alert animated:YES completion:nil];
    });
}

- (IBAction)onSetup:(id)sender {
    MPosDeviceInfo* targetDevice = _targetDevice;
    NSString* ipAddress = _tfIP.text;
    NSString* subnetMask = _tfSubnet.text;
    NSString* gateway = _tfGateway.text;
    NSNumber* useDhcp = _swDHCP.isOn ? @1 : @0;
    NSNumber* portNumber = [NSNumber numberWithInteger:_tfPortNumber.text.integerValue];
    NSNumber* inactivityTime = [NSNumber numberWithInteger:_tfInactivityTime.text.integerValue];
    NSString* systemName = _tfSystemName.text;
    NSNumber* timeout = [NSNumber numberWithDouble:5.0];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 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" title:@"INFORMATION"];
        }else{
            [self showMessageBox:@"setNetworkDevice failed" title:@"ERROR"];
        }
    });
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

@end
