#define INDEV	stdin
#define OUTDEV	stdout

#define DEFAULT_BUF_SIZE	1024
#define RESERVED		5

#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "SHCSDK.h"

/* Callback Funtion */
int CallbackProc(void *param, long long int result)
{
	// progress
	printf("File Transfer : %lld\n", result);

	// 1 : Stop
	// 0 : Continue
	return 1;
}

static int __totalsend = 0;
ssize_t Upload_Func(void *userval, char *buf, size_t len)
{
	int nbyte =*((int *)userval);

	// Check buffer (MUST BE)
	if(buf == 0x00)
		return 0; 

	// Check the capacity of transmission (MUST BE) 
	if(__totalsend >= nbyte)
		return 0; // 

	int nsend = 0;
	char data[11] = "1234567890";

	// copy data
	nsend = strlen(data);
	if ( nsend > (int)len )
	{
		nsend = len;
	}
	
	if( nbyte - __totalsend < nsend )
	{
		nsend = nbyte - __totalsend;
	}

	strncpy(buf, data, nsend);

	__totalsend += nsend;

	printf("uploading : %d\n", __totalsend);

	// Return capacity of transmission
	return nsend; 
}

int main(void)
{
	// Service Information : Again provided
	char *__service_host = "http://nctest.ktsh.co.kr/dav"; 
	char *__auth_string = "bmN0ZXN0QG5jdGVzdDpJci0ocTJeCl8XQ+7BR5mjQu+lIZB/YryG0skrskPC+NmftbbpDiB4C+iTuEErTRQQ+I3oOv5yDDg=";

	char path[DEFAULT_BUF_SIZE + RESERVED];
	char service_host[DEFAULT_BUF_SIZE + RESERVED];
	char auth_string[DEFAULT_BUF_SIZE + RESERVED];

	HSHSDK hsdk = NULL;
	int nBytes = 0;
	
	sprintf(service_host, "%s", __service_host);

	sprintf(auth_string, "%s", __auth_string);

	/* CSDK : init */
	fprintf(stderr, "service_host!!! %s\n", service_host);
	hsdk = sh_init(service_host, auth_string, CallbackProc, NULL);
	if (hsdk == NULL) {
		fprintf(OUTDEV, "Failed to initiate the SDK.\n");
		goto ERR_EXIT;
	}
	path[0] = '\0';


	/* TODO : listing base uri copy to path */
	strcpy(path, "/test");
	/* CSDK : Creating a directory */
	if (sh_make_directory(hsdk, path) == 0)
	{
		if (sh_get_error_number(hsdk) != SHCERRNO_ALREADYEXIST )
		{
			printf("sh_make_directory ERROR : %d, %s\n", sh_get_error_number(hsdk), sh_get_error_message(hsdk));
			goto ERR_EXIT;
		}
		else 
		{
			printf("sh_make_directory always EXIST\n");	
		}
	}
	else
	{
		printf("sh_make_directory SUCCESS\n");	
	}
		
	/* CSDK : Upload Test */
	/* sh_upload_buffer_r Test : Add at the end of the file */	
	nBytes = 1024;
	for(int n = 0; n < 3; ++n)
	{
		__totalsend = 0;
		if (!sh_upload_buffer_r(hsdk, "/test.txt", SHWRITEPOLICY_APPEND, Upload_Func, -1, nBytes, &nBytes)) 
		{
			printf("upload_buffer_r : %d, %s\n", sh_get_error_number(hsdk), sh_get_error_message(hsdk));
			goto ERR_EXIT;
		}
		else 
		{
			printf("upload_buffer_r SUCCESS\n");
		}
	}	
	/* sh_upload_buffer_r Test : Replace the middle part of the file */
	__totalsend = 0;
	if (!sh_upload_buffer_r(hsdk, "/test.txt", SHWRITEPOLICY_APPEND, Upload_Func, 10, nBytes, &nBytes)) 
	{
		printf("upload_buffer_r : %d, %s\n", sh_get_error_number(hsdk), sh_get_error_message(hsdk));
		goto ERR_EXIT;
	} 
	else 
	{
		printf("upload_buffer_r SUCCESS\n");
	}

	/* CSDK : delete Test */
	/* File delete test */
	if (sh_delete(hsdk, "/test.txt") == 0)
	{
		printf("sh_delete ERROR : %d, %s\n", sh_get_error_number(hsdk), sh_get_error_message(hsdk));
		goto ERR_EXIT;
	}
	else
	{
		printf("sh_delete SUCCESS\n");
	}
	/* Directory delete test */
	if (sh_delete(hsdk, "/test") == 0)
	{
		printf("sh_delete ERROR : %d, %s\n", sh_get_error_number(hsdk), sh_get_error_message(hsdk));
		goto ERR_EXIT;
	}
	else
	{
		printf("sh_delete SUCCESS\n");
	}


	// sh_open : Create the parent directory of the file
	strcpy(path, "/SVC1_TEST/test_send.txt");
	if(!sh_open(hsdk, path) )
	{
		printf("sh_open ERROR : %d, %s\n", sh_get_error_number(hsdk), sh_get_error_message(hsdk));
		goto ERR_EXIT;
	}
	else 
	{

		printf("sh_open SUCCESS.\n");

		nBytes = 1024;
		for(int u = 0; u < 3; ++u)
		{
			__totalsend = 0;
			// sh_send_append : Add data to the end of the file.
			if(!sh_send_append(hsdk, path, Upload_Func, nBytes, &nBytes))
			{
				printf("sh_send_append ERROR : %d, %s\n", sh_get_error_number(hsdk), sh_get_error_message(hsdk));
				goto ERR_EXIT;
			}
			else
			{
				printf("sh_send_append SUCCESS.\n");
			}
		}

		// sh_send_block : Changes in the contents of the specified location.
		nBytes = 10;
		__totalsend = 0;
		if( !sh_send_block(hsdk,path, Upload_Func, 3, nBytes, &nBytes) )
		{
			printf("sh_send_block ERROR : %d, %s\n", sh_get_error_number(hsdk), sh_get_error_message(hsdk));
			goto ERR_EXIT;
		}
		else
		{
			printf("sh_send_block SUCCESS.\n");
		}
	}
		

ERR_EXIT:
	if(hsdk) sh_free(hsdk);

	return 0;
}


