package ctbrec;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.EnumSet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ctbrec.event.EventBusHolder;
import ctbrec.event.RecordingStateChangedEvent;
import ctbrec.recorder.download.Download;

public class Recording implements Serializable {
    private static final transient Logger LOG = LoggerFactory.getLogger(Recording.class);

    private Model model;
    private transient Download download;
    private Instant startDate;
    private String path;
    private State status = State.UNKNOWN;
    private int progress = -1;
    private long sizeInByte = -1;
    private String metaDataFile;
    private boolean singleFile = false;
    private boolean pinned = false;

    public enum State {
        RECORDING("recording"),
        GENERATING_PLAYLIST("generating playlist"),
        POST_PROCESSING("post-processing"),
        FINISHED("finished"),
        DOWNLOADING("downloading"),
        DELETING("deleting"),
        DELETED("deleted"),
        UNKNOWN("unknown"),
        WAITING("waiting"),
        FAILED("failed");

        private String desc;

        State(String desc) {
            this.desc = desc;
        }

        @Override
        public String toString() {
            return desc;
        }
    }

    public Instant getStartDate() {
        return startDate;
    }

    public void setStartDate(Instant startDate) {
        this.startDate = startDate;
    }

    public State getStatus() {
        return status;
    }

    public void setStatus(State status) {
        this.status = status;
    }

    public void setStatusWithEvent(State status) {
        setStatus(status);
        fireStatusEvent(status);
    }

    public int getProgress() {
        return this.progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public File getAbsoluteFile() {
        String recordingsDir = Config.getInstance().getSettings().recordingsDir;
        File recordingsFile = new File(recordingsDir, getPath());
        return recordingsFile;
    }

    public long getSizeInByte() {
        return sizeInByte;
    }

    public void setSizeInByte(long sizeInByte) {
        this.sizeInByte = sizeInByte;
    }

    public void postprocess() {
        getDownload().postprocess(this);
    }

    private void fireStatusEvent(State status) {
        RecordingStateChangedEvent evt = new RecordingStateChangedEvent(getDownload().getTarget(), status, getModel(), getStartDate());
        EventBusHolder.BUS.post(evt);
    }

    public Model getModel() {
        return model;
    }

    public void setModel(Model model) {
        this.model = model;
    }

    public Download getDownload() {
        return download;
    }

    public void setDownload(Download download) {
        this.download = download;
    }

    public boolean isSingleFile() {
        return singleFile;
    }

    public void setSingleFile(boolean singleFile) {
        this.singleFile = singleFile;
    }

    public String getMetaDataFile() {
        return metaDataFile;
    }

    public void setMetaDataFile(String metaDataFile) {
        this.metaDataFile = metaDataFile;
    }

    public boolean isPinned() {
        return pinned;
    }

    public void setPinned(boolean pinned) {
        this.pinned = pinned;
    }

    public Duration getLength() {
        if (getDownload() != null) {
            return getDownload().getLength();
        } else {
            return Duration.ofSeconds(0);
        }
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getStartDate() == null) ? 0 : (int) (getStartDate().toEpochMilli() ^ (getStartDate().toEpochMilli() >>> 32)));
        result = prime * result + ((model == null) ? 0 : model.hashCode());
        result = prime * result + ((path == null) ? 0 : path.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if(!(obj instanceof Recording)) {
            return false;
        }
        Recording other = (Recording) obj;
        if (getModel() == null) {
            if (other.getModel() != null) {
                return false;
            }
        } else if (!getModel().equals(other.getModel())) {
            return false;
        }
        if (getPath() == null) {
            if (other.getPath() != null) {
                return false;
            }
        } else if (!getPath().equals(other.getPath())) {
            return false;
        }
        if (getStartDate() == null) {
            if (other.getStartDate() != null) {
                return false;
            }
        } else if (getStartDate().toEpochMilli() != other.getStartDate().toEpochMilli()) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(Config.RECORDING_DATE_FORMAT);
        LocalDateTime localStartDate = LocalDateTime.ofInstant(getStartDate(), ZoneId.systemDefault());
        return getModel().getSanitizedNamed() + '_' + formatter.format(localStartDate);
    }

    private long getSize() {
        File rec = new File(Config.getInstance().getSettings().recordingsDir, getPath());
        if (rec.isDirectory()) {
            return getDirectorySize(rec);
        } else {
            if (!rec.exists()) {
                if (rec.getName().endsWith(".m3u8")) {
                    return getDirectorySize(rec.getParentFile());
                } else {
                    return -1;
                }
            } else {
                return rec.length();
            }
        }
    }

    private long getDirectorySize(File dir) {
        final long[] size = { 0 };
        int maxDepth = 1; // Don't expect subdirs, so don't even try
        try {
            Files.walkFileTree(dir.toPath(), EnumSet.noneOf(FileVisitOption.class), maxDepth, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                    size[0] += attrs.size();
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFileFailed(Path file, IOException exc) {
                    // Ignore file access issues
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException e) {
            LOG.error("Couldn't determine size of recording {}", this, e);
        }
        return size[0];
    }

    public void refresh() {
        sizeInByte = getSize();
    }
}
