package ctbrec.recorder;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import ctbrec.Model;
import ctbrec.ModelGroup;
import ctbrec.Recording;
import ctbrec.io.HttpClient;

public interface Recorder {
    public void addModel(Model model) throws IOException, InvalidKeyException, NoSuchAlgorithmException;

    public void stopRecording(Model model) throws IOException, InvalidKeyException, NoSuchAlgorithmException;
    public void stopRecordingAt(Model model) throws IOException, InvalidKeyException, NoSuchAlgorithmException;

    public void switchStreamSource(Model model) throws IOException, InvalidKeyException, NoSuchAlgorithmException;

    /**
     * Returns true, if a model is in the list of models to record. This does not reflect, if there currently is a recording running. The model might be offline
     * aswell.
     */
    public boolean isTracked(Model model);

    /**
     * Get the list of all models, which are tracked by ctbrec
     *
     * @return a List of Model objects, which might be empty
     */
    public List<Model> getModels();

    public List<Recording> getRecordings() throws IOException, InvalidKeyException, NoSuchAlgorithmException;

    public void delete(Recording recording) throws IOException, InvalidKeyException, NoSuchAlgorithmException;

    /**
     * Pins a recording. A pinned recording cannot be deleted.
     * @param recording
     * @throws IOException
     * @throws InvalidKeyException
     * @throws NoSuchAlgorithmException
     */
    public void pin(Recording recording) throws IOException, InvalidKeyException, NoSuchAlgorithmException;
    /**
     * Unpins a previously pinned recording. A pinned recording cannot be deleted.
     * @param recording
     * @throws IOException
     * @throws InvalidKeyException
     * @throws NoSuchAlgorithmException
     */
    public void unpin(Recording recording) throws IOException, InvalidKeyException, NoSuchAlgorithmException;

    public void shutdown(boolean immediately);

    public void suspendRecording(Model model) throws IOException, InvalidKeyException, NoSuchAlgorithmException;
    public void resumeRecording(Model model) throws IOException, InvalidKeyException, NoSuchAlgorithmException;

    public boolean isSuspended(Model model);

    public boolean isMarkedForLaterRecording(Model model);
    public void markForLaterRecording(Model model, boolean mark) throws InvalidKeyException, NoSuchAlgorithmException, IOException;

    /**
     * Returns only the models from getModels(), which are online
     * @return
     */
    public List<Model> getOnlineModels();

    /**
     * Returns only the models from getModels(), which are actually recorded right now (a recording process is currently running).
     * @return
     * @throws IOException
     * @throws IllegalStateException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     */
    public default List<Model> getCurrentlyRecording() throws InvalidKeyException, NoSuchAlgorithmException, IOException {
        List<Recording> recordings = getRecordings();
        return getModels().stream().filter(m -> {
            for (Recording recording : recordings) {
                if (recording.getStatus() == Recording.State.RECORDING && recording.getModel().equals(m)) {
                    return true;
                }
            }
            return false;
        }).collect(Collectors.toList());
    }

    public HttpClient getHttpClient();

    /**
     * Get the total size of the filesystem we are recording to
     * @return the total size in bytes
     * @throws IOException
     */
    public long getTotalSpaceBytes() throws IOException;

    /**
     * Get the free space left on the filesystem we are recording to
     * @return the free space in bytes
     * @throws IOException
     */
    public long getFreeSpaceBytes() throws IOException;

    /**
     * Regenerate the playlist for a recording. This is helpful, if the
     * playlist is corrupt or hasn't been generated for whatever reason
     * @param recording
     * @throws IllegalStateException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws IOException
     */
    public void rerunPostProcessing(Recording recording) throws IOException, InvalidKeyException, NoSuchAlgorithmException;

    /**
     * Tells the recorder, that the recording priority for the given model has changed
     *
     * @param model the model with changed priority. The new value can be retrieved with {@link Model#getPriority()}
     * @throws IOException
     * @throws InvalidKeyException
     * @throws NoSuchAlgorithmException
     */
    public void priorityChanged(Model model) throws IOException, InvalidKeyException, NoSuchAlgorithmException;

    public void setNote(Recording rec, String note) throws IOException, InvalidKeyException, NoSuchAlgorithmException;

    /**
     * Pauses the recording of models entirely. The state of which models should be recorded and which are paused
     * is kept.
     * @throws IOException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     */
    public void pause() throws InvalidKeyException, NoSuchAlgorithmException, IOException;

    /**
     * Resumes recording
     * @throws IOException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     */
    public void resume() throws InvalidKeyException, NoSuchAlgorithmException, IOException;

    /**
     * Returns the number of models, which are on the recording list and not marked for later recording
     * @return
     */
    public int getModelCount();

    public Set<ModelGroup> getModelGroups();

    /**
     * Saves a model group. If the group already exists, it will be overwritten. Otherwise it will
     * be saved as a new group.
     * @param group
     * @throws IOException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     */
    public void saveModelGroup(ModelGroup group) throws IOException, InvalidKeyException, NoSuchAlgorithmException;

    public void deleteModelGroup(ModelGroup group) throws IOException, InvalidKeyException, NoSuchAlgorithmException;

    default Optional<ModelGroup> getModelGroup(Model model) {
        return getModelGroups().stream()
                .filter(mg -> mg.getModelUrls().contains(model.getUrl()))
                .findFirst();
    }
}
