package io.delphiplatform.api.v3.model.amazon;

import com.fasterxml.jackson.annotation.JsonProperty;

import io.delphiplatform.api.v3.rdb.entity.amazon.AmazonChartEntity;
import io.delphiplatform.api.v3.rdb.entity.amazon.AmazonChartTrackEntity;
import io.delphiplatform.api.v3.rdb.entity.amazon.AmazonChartTrackLifetimeEntity;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;

@Getter
@EqualsAndHashCode
@Builder
public class AmazonChart {

    @JsonProperty("chart_id")
    private String chartId;

    @JsonProperty("name")
    private String name;

    @JsonProperty("total_positions")
    private Integer totalPositions;

    @JsonProperty("country_code")
    private String countryCode;

    @JsonProperty("genre")
    private String genre;

    public static AmazonChart of(AmazonChartEntity entity) {
        if (entity == null) {
            return null;
        }

        String genreNameStr = entity.getGenre() != null ? entity.getGenre().getName() : "_";
        String countryCodeStr = entity.getCountryCode() != null ? entity.getCountryCode().toUpperCase() : "_";
        String chartName = String.format("Amazon Music Top Tracks %s %s", genreNameStr, countryCodeStr);

        return AmazonChart.builder()
            .chartId(entity.getChartId())
            .name(chartName)
            .totalPositions(entity.getTotalPositions())
            .countryCode(entity.getRegion().getCountryCode())
            .genre(entity.getGenre().getName())
            .build();
    }

    public static AmazonChart of(AmazonChartTrackEntity amazonChartTrackEntity) {
        AmazonChartEntity entity = amazonChartTrackEntity.getChart();
        if (entity == null) {
            return null;
        }

        return of(entity);
    }

    public static AmazonChart of(AmazonChartTrackLifetimeEntity amazonChartTrackEntity) {
        AmazonChartEntity entity = amazonChartTrackEntity.getChart();
        if (entity == null) {
            return null;
        }

        return of(entity);
    }

}