package cif

import (
	"errors"
	"time"

	"git.fjla.uk/owlboard/timetable-mgr/log"
	"go.uber.org/zap"
)

const updateHour = 7
const dailyUpdateUrl = "https://publicdatafeeds.networkrail.co.uk/ntrod/CifFileAuthenticate?type=CIF_ALL_UPDATE_DAILY&day=toc-update-"
const fullUpdateUrl = "https://publicdatafeeds.networkrail.co.uk/ntrod/CifFileAuthenticate?type=CIF_ALL_FULL_DAILY&day=toc-full"

func getDayString() string {
	london, err := time.LoadLocation("Europe/London")
	if err != nil {
		log.Msg.Error("Unable to load time zone info", zap.Error(err))
	}

	timeNow := time.Now().In(london)
	day := timeNow.Weekday()

	dayStrings := [...]string{"sun", "mon", "tue", "wed", "thu", "fri", "sat"}

	return dayStrings[day]
}

func getUpdateUrl(updateType string) (string, error) {
	if updateType == "daily" {
		return dailyUpdateUrl + getDayString(), nil
	} else if updateType == "full" {
		return fullUpdateUrl, nil
	}
	err := errors.New("Invalid update type provided, must be one of 'daily' or 'full'")
	return "", err
}
