package be.msyben.vinylManager.tools.discogs.entities;

import java.io.Serializable;
import java.util.Date;

public final class Release implements Serializable {
    private static final long serialVersionUID = -1253508518689896008L;
    private String title;
    private long id;
    private Artist[] artists;
    private String data_quality;
    private String thumb;
    private Community community;
    private Company[] companies;
    private String country;
    private Date date_added;
    private Date date_changed;
    private Double estimated_weight;
    private Artist[] extraartists;
    private int format_quantity;
    private Format[] formats;
    private String[] genres;
    private Identifier[] identifiers;
    private Image[] images;
    private Label[] labels;
    private Double lowest_price;
    private long master_id;
    private String master_url;
    private String notes;
    private int num_for_sale;
    private String released;
    private String released_formatted;
    private String resource_url;
    //private String[] series;
    private Serie[] series;
    private String[] styles;
    private Track[] tracklist;
    private String uri;
    private Video[] videos;
    private int year;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Artist[] getArtists() {
        return artists;
    }

    public void setArtists(Artist[] artists) {
        this.artists = artists;
    }

    public String getData_quality() {
        return data_quality;
    }

    public void setData_quality(String data_quality) {
        this.data_quality = data_quality;
    }

    public String getThumb() {
        return thumb;
    }

    public void setThumb(String thumb) {
        this.thumb = thumb;
    }

    public Community getCommunity() {
        return community;
    }

    public void setCommunity(Community community) {
        this.community = community;
    }

    public Company[] getCompanies() {
        return companies;
    }

    public void setCompanies(Company[] companies) {
        this.companies = companies;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public Date getDate_added() {
        return date_added;
    }

    public void setDate_added(Date date_added) {
        this.date_added = date_added;
    }

    public Date getDate_changed() {
        return date_changed;
    }

    public void setDate_changed(Date date_changed) {
        this.date_changed = date_changed;
    }

    public Double getEstimated_weight() {
        return estimated_weight;
    }

    public void setEstimated_weight(Double estimated_weight) {
        this.estimated_weight = estimated_weight;
    }

    public Artist[] getExtraartists() {
        return extraartists;
    }

    public void setExtraartists(Artist[] extraartists) {
        this.extraartists = extraartists;
    }

    public int getFormat_quantity() {
        return format_quantity;
    }

    public void setFormat_quantity(int format_quantity) {
        this.format_quantity = format_quantity;
    }

    public Format[] getFormats() {
        return formats;
    }

    public void setFormats(Format[] formats) {
        this.formats = formats;
    }

    public String[] getGenres() {
        return genres;
    }

    public void setGenres(String[] genres) {
        this.genres = genres;
    }

    public Identifier[] getIdentifiers() {
        return identifiers;
    }

    public void setIdentifiers(Identifier[] identifiers) {
        this.identifiers = identifiers;
    }

    public Image[] getImages() {
        return images;
    }

    public void setImages(Image[] images) {
        this.images = images;
    }

    public Label[] getLabels() {
        return labels;
    }

    public void setLabels(Label[] labels) {
        this.labels = labels;
    }

    public Double getLowest_price() {
        return lowest_price;
    }

    public void setLowest_price(Double lowest_price) {
        this.lowest_price = lowest_price;
    }

    public long getMaster_id() {
        return master_id;
    }

    public void setMaster_id(long master_id) {
        this.master_id = master_id;
    }

    public String getMaster_url() {
        return master_url;
    }

    public void setMaster_url(String master_url) {
        this.master_url = master_url;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public int getNum_for_sale() {
        return num_for_sale;
    }

    public void setNum_for_sale(int num_for_sale) {
        this.num_for_sale = num_for_sale;
    }

    public String getReleased() {
        return released;
    }

    public void setReleased(String released) {
        this.released = released;
    }

    public String getReleased_formatted() {
        return released_formatted;
    }

    public void setReleased_formatted(String released_formatted) {
        this.released_formatted = released_formatted;
    }

    public String getResource_url() {
        return resource_url;
    }

    public void setResource_url(String resource_url) {
        this.resource_url = resource_url;
    }

    /*public String[] getSeries() {
        return series;
    }

    public void setSeries(String[] series) {
        this.series = series;
    }*/
    public Serie[] getSeries() {
        return series;
    }

    public void setSeries(Serie[] series) {
        this.series = series;
    }

    public String[] getStyles() {
        return styles;
    }

    public void setStyles(String[] styles) {
        this.styles = styles;
    }

    public Track[] getTracklist() {
        return tracklist;
    }

    public void setTracklist(Track[] tracklist) {
        this.tracklist = tracklist;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public Video[] getVideos() {
        return videos;
    }

    public void setVideos(Video[] videos) {
        this.videos = videos;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    @Override
    public String toString() {
        return "Release{" + "title=" + title + ", id=" + id + ", artists=" + artists + ", data_quality=" + data_quality + ", thumb=" + thumb + ", community=" + community + ", companies=" + companies + ", country=" + country + ", date_added=" + date_added + ", date_changed=" + date_changed + ", estimated_weight=" + estimated_weight + ", extraartists=" + extraartists + ", format_quantity=" + format_quantity + ", formats=" + formats + ", genres=" + genres + ", identifiers=" + identifiers + ", images=" + images + ", labels=" + labels + ", lowest_price=" + lowest_price + ", master_id=" + master_id + ", master_url=" + master_url + ", notes=" + notes + ", num_for_sale=" + num_for_sale + ", released=" + released + ", released_formatted=" + released_formatted + ", resource_url=" + resource_url + ", series=" + series + ", styles=" + styles + ", tracklist=" + tracklist + ", uri=" + uri + ", videos=" + videos + ", year=" + year + '}';
    }
    
    
    
    /** Helpers **/
    public String getArtistsString() {
        if (artists == null || artists.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < artists.length; i++) {
            if (i > 0) {
                sb.append(", ");
            }
            sb.append(artists[i].getName().replaceAll("\\([0-9]*\\)$", ""));
        }
        return sb.toString();
    }
    public String getGenresString() {
        if (genres == null || genres.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < genres.length; i++) {
            if (i > 0) {
                sb.append(", ");
            }
            sb.append(genres[i]);
        }
        return sb.toString();
    }
    public String getStylesString() {
        if (styles == null || styles.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < styles.length; i++) {
            if (i > 0) {
                sb.append(", ");
            }
            sb.append(styles[i]);
        }
        return sb.toString();
    }
    
    public String getBarCode() {
        if (identifiers == null || identifiers.length == 0) {
            return "";
        }
        for (Identifier identifier : identifiers) {
            if (identifier.getType() != null && identifier.getType().equals("Barcode")) {
                return identifier.getValue().replace(" ", "");
            }
        }
        return "";
    }
    
    public String getLabelString() {
        if (labels == null || labels.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < labels.length; i++) {
            if (i > 0) {
                sb.append(", ");
            }
            sb.append(labels[i].getName()==null?"-":labels[i].getName());
        }
        return sb.toString();
    }
    
    
    public String getCatNo() {
        if (labels != null && labels.length > 0) {
            return labels[0].getCatno();
        }
        return "";
    }
    
    public String getFirstImageUrl() {
        if (images == null || images.length == 0) {
            return "";
        }
        
        return images[0].getUri()==null?"":images[0].getUri();
    }
    
    public String getFormatString(){
        if (formats == null || formats.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < formats.length; i++) {
            if (i > 0) {
                sb.append(", ");
            }
            sb.append(formats[i].getName());
        }
        return sb.toString();
    }
    
    public String getCompaniesString() {
        if (companies == null || companies.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (Company company : companies) {
            if (!first) {
                sb.append(", ");
            }
            first = false;
            if (company.getName() != null && !company.getName().isEmpty()) {
                sb.append(company.getName());
            }
        }
        return sb.toString();
    }
    
    public String getFormatDescriptionString() {
        if (formats == null || formats.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (Format format : formats) {
            if (!first) {
                sb.append("\r\n");
            }
            first = false;
            if (format.getQty() != null && !format.getQty().isEmpty()) {
                try {
                    int nbr = Integer.parseInt(format.getQty());
                    if (nbr > 1) {
                        sb.append(nbr).append("X ");
                    }
                } catch (NumberFormatException ex) {
                    sb.append(format.getQty()).append("X ");
                }
            }
            
            if (format.getName() != null && !format.getName().isEmpty()) {
                sb.append(format.getName());
            }
            if (format.getDescriptions() != null) {                
                for (String desc : format.getDescriptions()) {
                    sb.append(", ").append(desc);
                }
            }
            if (format.getText() !=null && !format.getText().isEmpty()) {
                sb.append(", ").append(format.getText());
            }
        }
        return sb.toString();
    }
    /*public String getFormatDescriptionString() {
        if (formats == null || formats.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < formats.length; i++) {
            if (formats[i].getDescriptions() != null) {
                for (int j = 0; j < formats[i].getDescriptions().length; j++) {
                    if (sb.length() > 0) {
                        sb.append(", ");
                    }
                    sb.append(formats[i].getDescriptions()[j]);
                }
            }
        }
        return sb.toString();
    }*/
    
    public boolean isBootleg() {
        if (formats != null) {
            for (Format f:formats) {
                if (f.getDescriptions() != null) {
                    for (String desc : f.getDescriptions()) {
                        if (desc.equals("Unofficial Release")) {
                            return true;
                        }
                    }
                }
            }
        }
        
        if (labels != null) {
            for (Label l : labels) {
                if (l.getName().equals("Not On Label")) {
                    return true;
                }
            }
        }
        
        return false;
    }
}
