// Copyright (C) 2019 Marius Schellenberger

package server

import (
	"errors"
	"strconv"
	"time"
)

const (
	day = time.Hour * 24
	min = time.Second * 30
	max = day * 90
)

func duration(duration, mode string) (d time.Duration, err error) {
	switch mode {
	case "1":
		d, err = time.ParseDuration(duration)
		if err != nil {
			err = errors.New("could not parse duration")
			return
		}
	case "2":
		var t int
		t, err = strconv.Atoi(duration)
		if err != nil {
			err = errors.New("could not parse duration")
			return
		}
		d = time.Duration(int(day) * t)
	default:
		err = errors.New("invalid mode")
		return
	}

	if d < min {
		err = errors.New("minimum duration is 30 seconds")
	}
	if d > max {
		err = errors.New("maximum duration is 90 days")
	}
	return
}
