package tim.prune.cmd;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import tim.prune.DataSubscriber;
import tim.prune.data.AudioClip;
import tim.prune.data.MediaObject;
import tim.prune.data.Photo;
import tim.prune.data.TrackInfo;

/**
 * Command to remove one or more media items (photos and/or audios)
 */
public class RemoveMediaCmd extends Command
{
	protected final ArrayList<MediaObject> _media = new ArrayList<>();


	public RemoveMediaCmd(List<MediaObject> inMedia) {
		this(null, inMedia);
	}

	RemoveMediaCmd(Command inParent, List<MediaObject> inMedia)
	{
		super(inParent);
		if (inMedia != null) {
			_media.addAll(inMedia);
		}
	}

	public int getUpdateFlags() {
		return DataSubscriber.MEDIA_MODIFIED;
	}

	protected boolean executeCommand(TrackInfo inInfo)
	{
		for (MediaObject media : _media)
		{
			if (media instanceof Photo) {
				inInfo.getPhotoList().delete((Photo) media);
			}
			else if (media instanceof AudioClip) {
				inInfo.getAudioList().delete((AudioClip) media);
			}
			else {
				return false;
			}
		}
		return !_media.isEmpty();
	}

	protected Command makeInverse(TrackInfo inInfo)
	{
		final ArrayList<MediaInsertion> inserts = new ArrayList<>();
		for (MediaObject mediaObj : _media)
		{
			if (mediaObj instanceof Photo photo) {
				inserts.add(new MediaInsertion(photo, inInfo.getPhotoList().getIndexOf(photo)));
			}
			else if (mediaObj instanceof AudioClip audio) {
				inserts.add(new MediaInsertion(audio, inInfo.getAudioList().getIndexOf(audio)));
			}
		}
		Collections.sort(inserts);
		return new InsertMediaCmd(this, inserts);
	}
}
