package model;

import workers.FilesWorker;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author simon
 */
public class SOMEvent {
    /**
     * The instance for the Singleton
     */
    private static SOMEvent event = null;
    /**
     * The Event Date
     */
    private final Date date = new Date();
    /**
     * All images form the Event
     */
    private final List<SOMImage> images = new ArrayList<SOMImage>();
    /**
     * The currently selected image
     */
    private SOMImage currentimage;
    /**
     * Makes the getEventImages function unusable after first use
     */
    private boolean imagesread = false;

    /**
     * Event Constructor
     */
    private SOMEvent() {
    }

    /**
     * Returns an Instance of the Event (Singleton)
     *
     * @return
     */
    public static SOMEvent getInstance() {
        // Singleton to prevent Instantiation
        if (event == null) {
            event = new SOMEvent();
            // event.currentimage = SOMImage.getFallbackImage();
        }

        return event;
    }

    /**
     * Returns the List of Images already shot at the event if SOM is turned off
     * in between. Gets unfunctional after one time use caused by imagesread
     * boolean.
     */
    public void getEventImages() {
        // Behandelt
        if (!imagesread) {

            File[] files = FilesWorker.getSourcePath().listFiles();

            assert files != null;
            Arrays.sort(files);

            for (File file : files) {
                SOMImage img = new SOMImage(file.getName());
                img.updateImageState();
                this.addImage(img);
            }

            imagesread = true;
        }
//        return;
    }

    /**
     * Adds the Given Image to the Events' Image List
     *
     * @param now
     * @return
     */
    public void addImage(SOMImage now) {
        images.add(now);
    }

    /**
     * Returns the Events' Date
     *
     * @return the date
     */
    public Date getDate() {
        return date;
    }

    /**
     * Returns a List of all of the Events' images
     *
     * @return the images
     */
    public List<SOMImage> getImages() {
        // If there are no Event Images Collected
        if (images.size() == 0)
            // get all the events' images
            getEventImages();
        //...and return them
        return images;
    }

    /**
     * returns the last image shot
     *
     * @return
     */
    public SOMImage getLastImage() {
        SOMImage[] imagesarr = this.images.toArray(new SOMImage[this.images
                .size()]);

        return imagesarr[imagesarr.length - 1];
    }

    /**
     * Returns the Icon of the last shot image and sets the "current image" to
     * this one
     *
     * @return
     */
    public void createAndSetLastImage() {
        this.currentimage = getLastImage();
        // this.currenticon =
//        makeIcon(this.currentimage);
        // return currenticon;
    }

    /**
     * Sets the given Image as the Current Image, creates a Preview and sets it
     * to UI
     *
     * @param
     */
    public void createAndSetImage(SOMImage image) {
        this.currentimage = image;
        // this.currenticon =
//        makeIcon(this.currentimage);
        // return currenticon;
    }


    /**
     * Sets the given ImageIcon as the current Icon
     * <p>
     * //     * @param ImageIcon
     * small
     */
    public void setCurrentIcon() {
        /*
      The ImageIcon of the currently shown Image
     */
    }

    /**
     * Returns the Current Image for the Print Panel to pass to the System
     * Printing Queue
     *
     * @return
     */
    public SOMImage getCurrentimage() {
        return currentimage;
    }
}