//
//  ViewController.m
//  AV_VSaaS_Client
//
//  Created by tutk on 2019/5/8.
//  Copyright © 2019 tutk. All rights reserved.
//

#import "ViewController.h"
#import <sys/time.h>
#import <pthread.h>
#import "IOTCAPIs.h"
#import "AVAPIs.h"
#import "P2PCam/AVIOCTRLDEFs.h"
#import "P2PCam/AVFRAMEINFO.h"
#include "NebulaAPIs.h"
#include "NebulaJsonAPIs.h"

static char gPincode[MAX_PIN_CODE_LENGTH+1] = "0000";
static char *gStoken = NULL;

@interface ViewController ()

@end

@implementation ViewController
@synthesize text_udid, text_stoken, text_show_msg;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

//########################################################
//# Get VSaaS information from app
//########################################################
void GetVsaasInfoFromApp(char *vsaas_info)
{
    //not implement in this sample
    sprintf(vsaas_info, "{\"func\":\"setVsaasInfo\",\"args\":{\"url\":\"idc-stg-gcp-vsaas.kalay.us\",\"stoken\":\"%s\"}}", gStoken);
}

//########################################################
//# Main function
//########################################################
- (IBAction)button_start:(id)sender {
    int ret = 0;
    char request[1024] = {0};
    const char *profile_str = NULL;
    NebulaJsonObject* json_response;
    NebulaClientCtx *client_ctx;
    
    /*Get udid from text_udid*/
    char *udid = (char *)[text_udid.text UTF8String];
    
    /*Get stoken from text_stoken*/
    gStoken = (char *)[text_stoken.text UTF8String];
    
    NSString *str =[[NSString alloc] init];
    str = [NSString stringWithFormat:@"UID is %s\n%@", udid, str];
    text_show_msg.text=str;
    
    //Nebula Initialize
    ret = Nebula_Initialize();
    
    if (ret < 0) {
        str = [NSString stringWithFormat:@"Nebula_Initialize fail ret [%d]\n%@", ret , str];
        text_show_msg.text=str;
        return;
    } else {
        str = [NSString stringWithFormat:@"Nebula_Initialize success\n%@", str];
        text_show_msg.text=str;
    }
    
    ret = Nebula_Client_New(udid, &client_ctx);
    
    if (ret < 0) {
        str = [NSString stringWithFormat:@"Nebula_Client_New fail ret [%d]\n%@", ret, str];
        text_show_msg.text=str;
        return;
    } else {
        str = [NSString stringWithFormat:@"Nebula_Client_New success\n%@", str];
        text_show_msg.text=str;
    }
    
    //Wait until device bind success
    sleep(3);
    NebulaJsonObject *json_args = NULL;
    unsigned int flag = 0;
    ret = Nebula_Client_Bind(client_ctx, gPincode, &json_args, 30000,&flag);
    
    if (ret < 0) {
        str = [NSString stringWithFormat:@"Nebula_Client_Bind fail ret [%d]\n%@", ret, str];
        text_show_msg.text=str;
        return;
    } else {
        str = [NSString stringWithFormat:@"Nebula_Client_Bind success\n%@", str];
        text_show_msg.text=str;
    }
    
    ret = Nebula_Client_Free_Bind_Response(json_args);
    
    if (ret < 0) {
        str = [NSString stringWithFormat:@"Nebula_Client_Free_Bind_Response fail ret [%d]\n%@", ret, str];
        text_show_msg.text=str;
    } else {
        str = [NSString stringWithFormat:@"Nebula_Client_Free_Bind_Response success\n%@", str];
        text_show_msg.text=str;
    }
    
    GetVsaasInfoFromApp(request);

    str = [NSString stringWithFormat:@"get vsaas info: %s\n%@",request, str];
    text_show_msg.text=str;
    
    ret = Nebula_Client_Send_Command(client_ctx, request, &json_response, 10000,&flag);
    
    if (ret < 0) {
        str = [NSString stringWithFormat:@"Nebula_Client_Send_Command fail ret [%d]\n%@", ret, str];
        text_show_msg.text=str;
    } else {
        str = [NSString stringWithFormat:@"Nebula_Client_Send_Command success\n%@", str];
        text_show_msg.text=str;
    }
    
    profile_str = Nebula_Json_Obj_To_String(json_response);
    str = [NSString stringWithFormat:@"[setVsaasInfo] %s\n%@", profile_str, str];
    text_show_msg.text=str;
    
    ret = Nebula_Client_Free_Send_Command_Response(json_response);
    ret = Nebula_Client_Delete(client_ctx);
    Nebula_DeInitialize();
    avDeInitialize();
    IOTC_DeInitialize();
}
@end
