/****
 *
 * Implementation of solar-system.h.  See that file for function descriptions.
 *
 */
#include "solar-system.h"
#include <stdio.h>
#include <string.h>

void init_system(SolarSystem* ss, double diameter, char* galaxy) {
    int i;
    (*ss).diameter = diameter;
    strcpy((*ss).galaxy, galaxy);
    for (i = 0; i < MAX_PLANETS; i++) {
        (*ss).planets[i] = EMPTY_PLANET;
    }
}

void add_planet(Planet p, SolarSystem* ss) {
    int i;
    for (i = 0; i < MAX_PLANETS && !is_empty((*ss).planets[i]); i++) ;
    if (i < MAX_PLANETS) {
        (*ss).planets[i] = p;
    }
}

void put_planet(Planet p, int pos, SolarSystem* ss) {
    if (pos < 0 || pos >= MAX_PLANETS) {
        return;
    }
    (*ss).planets[pos] = p;
}

Planet find_planet(char* name, SolarSystem ss) {
    int pos = find_planet_position(name, ss);
    if (pos >= 0) {
        return ss.planets[pos];
    }
    return EMPTY_PLANET;
}

int find_planet_position(char* name, SolarSystem ss) {
    int i;
    for (i = 0; i < MAX_PLANETS; i++) {
        if (strcmp(ss.planets[i].name, name) == 0) {
            return i;
        }
    }
    return -1;
}

void remove_planet(char* name, SolarSystem* ss) { 
    int pos = find_planet_position(name, *ss);
    if (pos >= 0) {
        (*ss).planets[pos] = EMPTY_PLANET;
    }
}

void print_system(SolarSystem ss) {
    int i;
    for (i = 0; i < MAX_PLANETS; i++) {
        if (!is_empty(ss.planets[i])) {
            printf("planets[%d]:\n", i);
            print_planet(ss.planets[i]);
            printf("\n");
        }
    }
}