package model

import (
	"fmt"
	"github.com/jftuga/geodist"
	"time"
)

//go:generate goplantuml -show-aggregations -show-compositions -aggregate-private-members -output ships.puml .

type Position struct {
	Lat float64
	Lon float64
}

type Ship struct {
	name                 string
	position             Position
	traveledDistanceInKm int
}

type Movement struct {
	ShipName   string
	TimeStamp  time.Time
	ToPosition Position
}

func NewShip(shipName string, startPosition Position) *Ship {
	return &Ship{name: shipName, position: startPosition}
}

func (s *Ship) UpdatePosition(pos Position) {
	if s.position.Lat == 0 {
		s.position = pos
		return
	}
	s.traveledDistanceInKm += calculateDistance(s.position, pos)
	s.position = pos
}

func (s *Ship) String() string {
	return fmt.Sprintf("Ship '%s' traveled %d km", s.name, s.traveledDistanceInKm)
}

func calculateDistance(startPos, endPos Position) int {
	start := geodist.Coord{Lat: startPos.Lat, Lon: startPos.Lon}
	target := geodist.Coord{Lat: endPos.Lat, Lon: endPos.Lon}

	_, km, _ := geodist.VincentyDistance(start, target)
	return int(km)
}
