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