#include "Ship.h"

#include <iomanip>

namespace Battleship{

std::string Ship::getShipString() const{
    switch(size){
    case 1:
        return "submarine";
        break;
    case 2:
        return "destroyer";
        break;
    case 3:
        return "cruiser";
        break;
    case 4:
        return "battleship";
        break;
    case 5:
        return "carrier";
        break;
    case 6:
        return "cargo";
        break;
    case 7:
        return "tanker";
        break;
    default:
        return "unknown";
        break;
    }
}

void Ship::printShip(std::ostream& out) const{
    if (x < 0 || y < 0){
        out << getShipString() << std::endl;
        return;
    }
    out << std::setw(10) << std::left << getShipString()
        << " " << y << " " << x;
    if (size > 1){
        out << ((horizontal) ? " horizontal" : " vertical");
    }
    out << std::endl;
}

void Ship::setShip(std::string ship){
    size = convertToShip(ship);
}

uint8_t convertToShip(std::string ship){
    if (ship == "submarine") return 1;
    if (ship == "destroyer") return 2;
    if (ship == "cruiser") return 3;
    if (ship == "battleship") return 4;
    if (ship == "carrier") return 5;
    if (ship == "cargo") return 6;
    if (ship == "tanker") return 7;
    return 0;
}

void deleteLinkedShips(Ship*& ship){
    if (ship == NULL) return;
    Ship* nextShip = ship->getNextShip();
    delete ship;
    deleteLinkedShips(ship = nextShip);
}

};
