package ctbrec.sites.jasmin;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import javax.xml.bind.JAXBException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iheartradio.m3u8.ParseException;
import com.iheartradio.m3u8.PlaylistException;

import ctbrec.io.HttpClient;
import ctbrec.recorder.download.hls.HlsDownload;
import ctbrec.recorder.download.hls.SegmentPlaylist;

public class LiveJasminHlsDownload extends HlsDownload {

    private static final Logger LOG = LoggerFactory.getLogger(LiveJasminHlsDownload.class);
    private long lastMasterPlaylistUpdate = 0;
    private String segmentUrl;

    public LiveJasminHlsDownload(HttpClient client) {
        super(client);
    }

    @Override
    protected SegmentPlaylist getNextSegments(String segments) throws IOException, ParseException, PlaylistException  {
        if(this.segmentUrl == null) {
            this.segmentUrl = segments;
        }
        SegmentPlaylist playlist = super.getNextSegments(segmentUrl);
        long now = System.currentTimeMillis();
        if( (now - lastMasterPlaylistUpdate) > TimeUnit.SECONDS.toMillis(60)) {
            super.downloadExecutor.submit(this::updatePlaylistUrl);
            lastMasterPlaylistUpdate = now;
        }
        return playlist;
    }

    private void updatePlaylistUrl() {
        try {
            LOG.debug("Updating segment playlist URL for {}", getModel());
            segmentUrl = getSegmentPlaylistUrl(getModel());
        } catch (IOException | JAXBException | ExecutionException | ParseException | PlaylistException e) {
            LOG.error("Couldn't update segment playlist url. This might cause a premature download termination", e);
        }
    }
}
