package model;

import workers.FilesWorker;

import javax.swing.*;
import java.io.File;
import java.nio.file.Files;

public class SOMImage {

    private static String name; // the filename
    private final SOMConfig config = SOMConfig.getInstance();
    private final File tmb; // thumbnail
    private final File prv; // preview
    private final File src; // source
    private final File fnl; // final
    private final File tmp; // temp

    private boolean tmbthere; // thumbnail status
    private boolean prvthere; // preview status
    private boolean srcthere; // source status
    private boolean fnlthere; // final status

    SOMImage(String name) {

        File src = new File(FilesWorker.getSourcePath().toString() + "/" + name);

        File prv = new File(FilesWorker.getPreviewPath().toString() + "/"
                + name);

        File tmb = new File(FilesWorker.getThumbPath().toString() + "/" + name);

        File fnl = new File(FilesWorker.getFinalPath().toString() + "/" + name);

        File tmp = new File(FilesWorker.getTempPath().toString() + "/" + name);

        this.tmb = tmb;
        this.prv = prv;
        this.src = src;
        this.fnl = fnl;
        this.tmp = tmp;

        this.updateImageState();
    }

    public SOMImage() {
        super();

        int numberOfEventPictures = SOMEvent.getInstance().getImages().size();
        String fill = "00000";
        int nmb = (numberOfEventPictures + 1);
        String number = String.valueOf(nmb);
        String filledNumber = fill.substring(number.length()) + number;
        name = filledNumber + ".jpg";

        File src = new File(FilesWorker.getSourcePath().toString() + "/" + name);
        File prv = new File(FilesWorker.getPreviewPath().toString() + "/" + name);
        File tmb = new File(FilesWorker.getThumbPath().toString() + "/" + name);
        File fnl = new File(FilesWorker.getFinalPath().toString() + "/" + name);
        File tmp = new File(FilesWorker.getTempPath().toString() + "/" + name);

        this.tmb = tmb;
        this.prv = prv;
        this.src = src;
        this.fnl = fnl;
        this.tmp = tmp;

        this.updateImageState();
    }

    public void updateImageState() {
        // tmb
        tmbthere = Files.exists(tmb.toPath());
        // prv
        prvthere = Files.exists(prv.toPath());
        // src
        srcthere = Files.exists(src.toPath());
        // fnl
        fnlthere = Files.exists(fnl.toPath());
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the tmb
     */
    public File getTmb() {
        return this.tmb;
    }

    /**
     * @return the prv
     */
    public File getPrv() {
        return prv;
    }

    /**
     * @return the src
     */
    public File getSrc() {
        return src;
    }

    /**
     * @return the fnl
     */
    public File getFnl() {
        return fnl;
    }

    /**
     * @return the tmp
     */
    public File getTmp() {
        return tmp;
    }

    /**
     * @return the tmbthere
     */
    public boolean isTmbthere() {
        return tmbthere;
    }

    /**
     * @return the prvthere
     */
    public boolean isPrvthere() {
        return prvthere;
    }

    /**
     * @return the srcthere
     */
    public boolean isSrcthere() {
        return srcthere;
    }

    /**
     * @return the fnlthere
     */
    public boolean isFnlthere() {
        return fnlthere;
    }

    /**
     * @param type 0 = thumb, 1 = preview, 2 = full Size
     * @return
     */
    public ImageIcon toImageIcon(int type) {
        File imagepath;
        switch (type) {
            case 0:
                imagepath = getTmb();
                break;
            case 1:
                imagepath = getPrv();
                break;
            case 2:
                imagepath = getSrc();
                break;
            default:
                imagepath = null;
                break;
        }

        return new ImageIcon(imagepath.getPath());
    }
}
