001package net.filebot.format;
002
003import java.util.Optional;
004
005import net.filebot.media.NamingStandard;
006import net.filebot.media.NamingStandard.Structure;
007import net.filebot.util.ReadOnlyFile;
008import net.filebot.web.Episode;
009import net.filebot.web.Movie;
010
011public class StructuredFile extends ReadOnlyFile {
012
013        private final Structure components;
014
015        private final NamingStandard naming;
016        private final Object object;
017
018        private StructuredFile(Structure components, NamingStandard naming, Object object) {
019                super(components.getPath(), false);
020                this.components = components;
021                this.naming = naming;
022                this.object = object;
023        }
024
025        private StructuredFile apply(Structure components) {
026                return new StructuredFile(components, naming, object);
027        }
028
029        public StructuredFile category(String s) {
030                return apply(components.category(s));
031        }
032
033        public StructuredFile library(String s) {
034                return apply(components.library(s));
035        }
036
037        public StructuredFile collection(String s) {
038                return apply(components.collection(s));
039        }
040
041        public StructuredFile group(String s) {
042                return apply(components.group(s));
043        }
044
045        public StructuredFile name(String s) {
046                return apply(components.name(s));
047        }
048
049        public StructuredFile number(String s) {
050                return apply(components.number(s));
051        }
052
053        public StructuredFile title(String s) {
054                return apply(components.title(s));
055        }
056
057        public StructuredFile tag(String s) {
058                return apply(components.tag(s));
059        }
060
061        public StructuredFile part(String s) {
062                return apply(components.part(s));
063        }
064
065        public StructuredFile suffix(String s) {
066                return apply(components.suffix(s));
067        }
068
069        public StructuredFile getId() {
070                if (object instanceof Episode) {
071                        Episode e = (Episode) object;
072                        return apply(components.collection(naming.getID(e.getSeriesInfo())));
073                }
074
075                if (object instanceof Movie) {
076                        Movie m = (Movie) object;
077                        return apply(components.collection(naming.getID(m)));
078                }
079
080                return this;
081        }
082
083        public StructuredFile getYear() {
084                if (object instanceof Episode) {
085                        String n = NamingStandard.Default.getSeriesNameYear((Episode) object);
086                        return apply(components.collection(null).collection(n).name(null).name(n));
087                }
088
089                return this;
090        }
091
092        public StructuredFile getUnix() {
093                return apply(components.validate(false));
094        }
095
096        public static StructuredFile of(Object object, NamingStandard naming) {
097                return Optional.ofNullable(object).map(naming::getPath).map(p -> new StructuredFile(p, naming, object)).orElse(null);
098        }
099
100}