package leigh.m3;

import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;

import java.time.Instant;
import java.util.Objects;

/**
 * This class represents a chunk which is scheduled to play in a stream.
 *
 * @author Alex Leigh
 */
public class SegmentSchedule {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(SegmentSchedule.class);
    private final MediaSegment chunk;
    private final Instant expire;
    private final int sequenceNumber;
    private final boolean isDiscontinuous;

    public SegmentSchedule(MediaSegment chunk, Instant expire, int sequenceNumber, boolean isDiscontinuous) {
        this.chunk = chunk;
        this.expire = expire;
        this.sequenceNumber = sequenceNumber;
        this.isDiscontinuous = isDiscontinuous;

        logger.info("Creating new chunk: {}", sequenceNumber);

    }

    /**
     * Returns <code>true</code> if this scheduled segment is discontinuous with the previously scheduled segment.
     */
    public boolean isDiscontinuous() {
        return isDiscontinuous;
    }

    @Override
    public String toString() {
        return "SegmentSchedule{" +
                "chunk=" + chunk +
                ", expire=" + expire +
                ", sequenceNumber=" + sequenceNumber +
                ", isDiscontinuous=" + isDiscontinuous +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SegmentSchedule that = (SegmentSchedule) o;
        return sequenceNumber == that.sequenceNumber && isDiscontinuous == that.isDiscontinuous && Objects.equals(chunk, that.chunk) && Objects.equals(expire, that.expire);
    }

    @Override
    public int hashCode() {
        return Objects.hash(chunk, expire, sequenceNumber, isDiscontinuous);
    }

    public MediaSegment getChunk() {
        return chunk;
    }

    public int getSequenceNumber() {
        return sequenceNumber;
    }

    public Instant getExpire() {
        return expire;
    }

}
