package model

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

type Position struct {
	Lat float64
	Lon float64
}

type Movement struct {
	from Position
	to   Position
}

func NewMovement(fromPosition Position, toPosition Position) *Movement {
	return &Movement{from: fromPosition, to: toPosition}
}
func (m *Movement) calculateDistanceinKm() int {
	start := geodist.Coord{Lat: m.from.Lat, Lon: m.from.Lon}
	target := geodist.Coord{Lat: m.to.Lat, Lon: m.to.Lon}

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

type Ship struct {
	name                 string
	position             Position
	traveledDistanceInKm int
}

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

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)
}

func calculateDistanceOld(startLat, startLon, endLat, endLon float64) int {
	start := geodist.Coord{Lat: startLat, Lon: startLon}
	target := geodist.Coord{Lat: endLat, Lon: endLon}

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