package rtsp

import (
	"log"
	"strings"
	"time"

	"go.uber.org/zap"
)

// LastPartURI returns the last part of the URI after "/".
func LastPartURI(URI string) (CutURI string) {
	split := strings.Split(URI, "/")
	return split[len(split)-1]
}

// waitPeriod waits for the next period.
func waitPeriod(period int, cam *zap.Logger) {
	periodTD := time.Duration(period) * time.Second

	now := time.Now()
	nextSegment := now.Truncate(periodTD).Add(periodTD)
	waitDuration := nextSegment.Sub(now)
	cam.Info("waiting for start recording:", zap.Duration("time", waitDuration))
	log.Println("waiting for start recording: ", waitDuration)
	time.Sleep(waitDuration)
}

// findResolution finds resolution in SDP.
func findResolution(Body []byte) string {
	split := strings.Split(string(Body), "\r\n")
	for _, line := range split {
		if strings.Contains(line, "a=x-dimensions:") {
			s, _ := strings.CutPrefix(line, "a=x-dimensions:")
			resolution := strings.Join(strings.Split(s, ","), "-")
			return resolution
		}
	}
	return "resolution"
}
