001package net.filebot.web;
002
003import static java.util.Collections.*;
004
005import java.util.AbstractList;
006import java.util.List;
007import java.util.Locale;
008
009public class Movie extends SearchResult {
010
011        protected int year;
012        protected int imdbId;
013        protected int tmdbId;
014
015        // optional movie name language hint
016        protected String language;
017
018        public Movie() {
019                // used by deserializer
020        }
021
022        public Movie(String name, String[] aliasNames, int year, int imdbId, int tmdbId, Locale locale) {
023                super(tmdbId > 0 ? tmdbId : imdbId > 0 ? imdbId : 0, name, aliasNames);
024                this.year = year;
025                this.imdbId = imdbId;
026                this.tmdbId = tmdbId;
027                this.language = locale == null ? null : locale.getLanguage();
028        }
029
030        public Movie(Movie movie) {
031                super(movie.id, movie.name, movie.aliasNames);
032                this.year = movie.year;
033                this.imdbId = movie.imdbId;
034                this.tmdbId = movie.tmdbId;
035                this.language = movie.language;
036        }
037
038        public int getYear() {
039                return year;
040        }
041
042        public int getImdbId() {
043                return imdbId;
044        }
045
046        public int getTmdbId() {
047                return tmdbId;
048        }
049
050        public Locale getLanguage() {
051                return language == null ? null : new Locale(language);
052        }
053
054        public String getNameWithYear() {
055                return toString(name, year);
056        }
057
058        @Override
059        public List<String> getEffectiveNames() {
060                if (name == null || name.length() == 0) {
061                        return emptyList();
062                }
063                if (aliasNames == null || aliasNames.length == 0) {
064                        return singletonList(toString(name, year));
065                }
066                return new AbstractList<String>() {
067
068                        @Override
069                        public String get(int index) {
070                                return index == 0 ? Movie.toString(name, year) : Movie.toString(aliasNames[index - 1], year);
071                        }
072
073                        @Override
074                        public int size() {
075                                return 1 + aliasNames.length;
076                        }
077                };
078        }
079
080        public List<String> getEffectiveNamesWithoutYear() {
081                return super.getEffectiveNames();
082        }
083
084        @Override
085        public boolean equals(Object object) {
086                if (object instanceof Movie) {
087                        Movie other = (Movie) object;
088
089                        if (tmdbId > 0 && other.tmdbId > 0) {
090                                return tmdbId == other.tmdbId;
091                        }
092                        if (imdbId > 0 && other.imdbId > 0) {
093                                return imdbId == other.imdbId;
094                        }
095
096                        return year == other.year && name.equals(other.name);
097                }
098
099                return false;
100        }
101
102        @Override
103        public Movie clone() {
104                return new Movie(name, aliasNames, year, imdbId, tmdbId, getLanguage());
105        }
106
107        @Override
108        public String toString() {
109                if (name != null && year > 0) {
110                        return toString(name, year);
111                }
112
113                if (tmdbId > 0) {
114                        return String.format("TheMovieDB::%s", tmdbId);
115                }
116
117                if (imdbId > 0) {
118                        return String.format("IMDB::%s", imdbId);
119                }
120
121                return super.toString();
122        }
123
124        private static String toString(String name, int year) {
125                return String.format("%s (%s)", name, year < 0 ? 0 : year);
126        }
127
128        public static Movie IMDB(int imdbId) {
129                return new Movie(null, null, 0, imdbId, 0, null);
130        }
131
132        public static Movie IMDB(String name, int year, int imdbId) {
133                return new Movie(name, null, year, imdbId, 0, null);
134        }
135
136        public static Movie TMDB(int tmdbId) {
137                return new Movie(null, null, 0, 0, tmdbId, null);
138        }
139
140        public static Movie TMDB(String name, int year, int tmdbId) {
141                return new Movie(name, null, year, 0, tmdbId, null);
142        }
143
144        public static Movie NameYear(String name, int year) {
145                return new Movie(name, null, year, 0, 0, null);
146        }
147
148}