#include <stdio.h>
#include <stdlib.h>
//container structure
struct container
{
  int k;
  char contents[100];
};
//ship structure
struct ship
{
  int id;
  struct container containers[1000];
};
//function prototypes
void join_convoy(int kContainers);

void leave_convoy(int shipId);

// global variables.
struct ship convoy[10000];
int noOfShips=0;
int kContainers=0;
int main(){
  int x;// Event number
  int sid;//ship id
  int position;//Ship position in convoy
  const int shippos;//Ship position 
  const int contpos;//Container position
  while (1){
    printf("\nChoose an Option.\n0. to remove ship from convoy.\n1. to add a ship to convoy\n2. Trader request ship id at this position.\n3. Get contents of container at a certain ship.\n4. -1 to exit the program.\n");
    scanf("%i",&x);
    switch(x){
    case 0:
      //remove ship from convoy
      printf("\nEnter the id of the ship to remove from convoy:  ");
      scanf("%d",&sid);//read the ship id from the user
      leave_convoy(sid);//leave the convoy
      main();//reload the main program ,start all over again
    case 1:
      //join convoy
      printf("\nEnter the number of containers in the ship: ");
      scanf("%d",&kContainers);//read the n containers
      join_convoy(kContainers);// join the convoy
      main();// reload the program
    case 2:
      //get position of ship in convoy and return its id
      printf("\nEnter the position of the ship: ");
      scanf("%d",&position);//read ship position
      printf("\nShip id at position %d is %d \n",position,convoy[position-1].id);//print the id of the ship
      main();
    case 3:
      //get position of ship and position of container in ship and return its contents
      printf("\nEnter position of the ship in convoy: ");
      scanf("%d",&shippos);
      printf("\nEnter the container position: ");
      scanf("%d",&contpos);
      // if the number of ships is less than the given ship position in the convoy
      if (noOfShips<shippos){
	printf("\nThe ship container request is invalid !\n");
      }
      else{
	printf("\nContents of ship position %i container %i is %s\n",(shippos,contpos,convoy[shippos-1].containers[contpos-1].contents));
      }
      main();
    case -1:
      exit(0);
    default:
      exit(0);
    }
  }
  return 0;
}

void join_convoy(int kContainers){
  struct ship newShip;
  newShip.id=noOfShips+1;
  for(int i=0;i<kContainers;i++){
    int k;
    char contents[100];
    printf("Enter the number of alphanumeric characters (k)  in the contents of the container:\n");
    scanf("%d",&k);
    printf("Enter the contents(k characters): ");
    scanf("%s",&contents);
    printf("\n");
    newShip.containers[i].k=k;
    newShip.containers[i].contents;
  }
  convoy[noOfShips]=newShip;
  noOfShips+=1;
  //newShip.id
  //get number of containers
  //get number of chars of contents
  //get the characters contents
}

void leave_convoy(int shipId){
  //search for ship id in convoy
  for(int i=0;i<noOfShips;i++){
    if (convoy[i].id==shipId && i!=noOfShips-1){
      struct ship tempship=convoy[i];
      convoy[i]=convoy[noOfShips-1];
      noOfShips-=1;
    }
    else if (i==noOfShips-1){
      noOfShips-=1;
    }
    else{
      printf("\nThe Ship Is not in the convoy!\n");
      exit(0);
    }
  }
}
