from django.db import models

from pycruisemapper.classes import CruiseMapper, Ship


class Vessel(models.Model):
    name = models.CharField(max_length=64)
    imo = models.IntegerField(null=True, blank=True)
    mmsi = models.IntegerField(null=True, blank=True)

    def query_cruisemapper(self) -> Ship:
        cm = CruiseMapper()
        all_ships = cm.get_ships()

        try:
            if self.imo:
                filtered = filter(lambda x: x.imo == self.imo, all_ships)
            elif self.mmsi:
                filtered = filter(lambda x: x.mmsi == self.mmsi, all_ships)
            else:
                return False

            ship = list(filtered)[0]

        except:
            return False

        try:
            ship = cm.fill_ship(ship)
        except:
            pass

        return ship
