#include <stdio.h>
#include <winsock2.h>
#include <windows.h>
#include <string.h>
struct fileinfo
{
    char filepath[1024];
    char filename[512];
    char filecontent[8112];
    char *Pointer;
};

int read_file(char *filename, char *variable);

int main(int argc, char *argv[])
{
    if(argc < 2)
    {
        printf("please, provide IP and Port\n");
        return 0;
    }


    WSADATA wsa;
    SOCKET s;
    struct sockaddr_in server;

    printf("Initialising Winsock2...\n");
    if(WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d", WSAGetLastError());
        return 1;
    }
    printf("Initialised.\n");

    if((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d", WSAGetLastError());
        return 1;
    }
    printf("Socket created.\n");

    server.sin_addr.s_addr = inet_addr(argv[1]);
    server.sin_family = AF_INET;
    server.sin_port = htons(atoi(argv[2]));

    // Connection to server and sending/receiving data
    if(connect(s, (struct sockaddr *)&server, sizeof(server)) != SOCKET_ERROR)
    {
        printf("Connected\n");
    }else
    {
        printf("Could not connect to server: %d\n", WSAGetLastError());
        return 1;
    }

    // Processing file infos
    struct fileinfo SFileInformation;
    strcpy(SFileInformation.filepath, "C:\\Users\\i7Matisse\\Documents\\1 informatique\\DEV\\C\\UTILES\\TCP-IP connection winsock2\\FTP\\Client\\main.c");
    SFileInformation.Pointer = SFileInformation.filepath;
    
    // name
    while(SFileInformation.Pointer[0] != '.')
    {
        SFileInformation.Pointer++;
    };
    while(SFileInformation.Pointer[0] != '\\')
    {
        SFileInformation.Pointer--;
    }
    SFileInformation.Pointer++;
    strcpy(SFileInformation.filename, SFileInformation.Pointer);

    // Read the file
    read_file(SFileInformation.filepath, SFileInformation.filecontent);
    


    // process packet size
    char Cpacketsize[5];
    int packetsize;


    // networking

    // name of the file
    if(send(s, Cpacketsize, 5, 0) == SOCKET_ERROR) // size
    {
        printf("Error sending file name size: %d\n", WSAGetLastError());
        return 1;
    }else
    {
        printf("File name size sent !\n");
    }// name
    if(send(s, SFileInformation.filename, Ipacketsize, 0) == SOCKET_ERROR)
    {
        printf("Error sending file name : %d\n", WSAGetLastError());
        return 1;
    }else
    {
        printf("File name \"%s\" sent !\n", SFileInformation.filename);
    }


    // file content
    Ipacketsize = strlen(SFileInformation.filecontent) + 1;
    if(Ipacketsize < 10)
    {
        Cpacketsize[0] = '0';
        Cpacketsize[1] = '0';
        Cpacketsize[2] = '0';
        Cpacketsize[3] = '\0';
    }else if(Ipacketsize < 100)
    {
        Cpacketsize[0] = '0';
        Cpacketsize[1] = '0';
        Cpacketsize[2] = '\0';
    }else if(Ipacketsize < 1000)
    {
        Cpacketsize[0] = '0';
        Cpacketsize[1] = '\0';
    }else if(Ipacketsize < 10000)
    {
        Cpacketsize[0] = '\0';
    }
    strcat(Cpacketsize, itao(Ipacketsize));



    if(send(s, Cpacketsize, Ipacketsize, 0) == SOCKET_ERROR)
    {
        printf("Error sending file content size: %d\n", WSAGetLastError());
        return 1;
    }else
    {
        printf("File content size sent !\n");
    }
    if(send(s, strlen(SFileInformation.filecontent) + 1, strlen(SFileInformation.filecontent) + 1, 0) == SOCKET_ERROR)
    {
        printf("Error sending file content size: %d\n", WSAGetLastError());
        return 1;
    }else
    {
        printf("File content sent !\n");
    }
    closesocket(s);
    WSACleanup();
    return 0;
}
int read_file(char *filename, char *variable)
{
    FILE *file;
    file = fopen(filename, "rb");
    if (file == NULL) return 1;

    fseek(file, 0, SEEK_END); 
    int length = ftell(file);
    fseek(file, 0, SEEK_SET);

    char *string = malloc(sizeof(char) * (length+1));
  
    // c will store each char we read from the string
    char c;
    // i will be an index into the char array string as we read each char
    int i = 0;

    // keep reading each char from the file until we reach the end of the file
    while ( (c = fgetc(file)) != EOF)
    {
        // store char into the char array string
        string[i] = c;

        // increment i so we store the next char in the next index in the char array
        i++;
    }
    // terminate the string
    string[i] = '\0';
    strcpy(variable, string);
    fclose(file);
    return 0;
}