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

import com.fasterxml.jackson.annotation.JsonProperty;

import org.openapitools.jackson.nullable.JsonNullable;

import java.time.LocalDate;

import io.delphiplatform.api.v3.model.spotify.ChartTrackMetricsDimension;
import io.delphiplatform.api.v3.rdb.entity.apple.music.AppleMusicChartTrackEntity;
import io.delphiplatform.api.v3.rdb.entity.spotify.SpotifyChartTrackEntity;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@EqualsAndHashCode
public class ChartTrackStateMetrics {

    @JsonProperty("date")
    private LocalDate date;

    @JsonProperty("previous_date")
    private LocalDate previousDate;

    @JsonProperty("is_entry")
    private Boolean isEntry;

    @JsonProperty("position")
    private Integer position;

    @JsonProperty("isrc_position")
    private Integer isrcPosition;

    @JsonProperty("previous_position")
    private Integer previousPosition;

    @JsonProperty("trend")
    private Integer trend;

    @JsonProperty("date_streams")
    private JsonNullable<Integer> dateStreams;

    @JsonProperty("isrc_date_streams")
    private JsonNullable<Integer> isrcDateStreams;


    public static ChartTrackStateMetrics of(SpotifyChartTrackEntity entity,
        ChartTrackMetricsDimension metricsDimension) {
        if (entity == null) {
            return null;
        }
        if (metricsDimension == null || metricsDimension == ChartTrackMetricsDimension.TRACK_ID) {
            ChartTrackStateMetrics metrics = new ChartTrackStateMetrics();
            metrics.setDate(entity.getId().getChartDate());
            metrics.setPreviousDate(entity.getPrevChartDate());
            metrics.setIsEntry(entity.isEntry() || entity.isReentry());
            metrics.setPosition(entity.getCurrentPosition());
            metrics.setPreviousPosition(entity.getPrevPosition());
            metrics.setTrend(entity.getTrend());
            metrics.setDateStreams(JsonNullable.of(entity.getPeriodStreams()));
            metrics.setIsrcDateStreams(JsonNullable.of(entity.getIsrcPeriodStreams()));
            return metrics;
        } else {
            ChartTrackStateMetrics metrics = new ChartTrackStateMetrics();
            metrics.setDate(entity.getId().getChartDate());
            metrics.setPreviousDate(entity.getIsrcPrevChartDate());
            Boolean isEntry = (entity.getIsrcIsEntry() != null || entity.getIsrcIsReentry() != null) ?
                Boolean.TRUE.equals(entity.getIsrcIsEntry()) || Boolean.TRUE.equals(entity.getIsrcIsReentry()) :
                null;
            metrics.setIsEntry(isEntry);
            metrics.setPosition(entity.getCurrentPosition());
            metrics.setIsrcPosition(entity.getIsrcCurrentPosition());
            metrics.setPreviousPosition(entity.getIsrcPrevPosition());
            metrics.setTrend(entity.getIsrcTrend());
            metrics.setDateStreams(JsonNullable.of(entity.getPeriodStreams()));
            metrics.setIsrcDateStreams(JsonNullable.of(entity.getIsrcPeriodStreams()));
            return metrics;
        }
    }

    public static ChartTrackStateMetrics of(AppleMusicChartTrackEntity entity,
        ChartTrackMetricsDimension metricsDimension) {
        if (entity == null) {
            return null;
        }
        if (metricsDimension == null || metricsDimension == ChartTrackMetricsDimension.TRACK_ID) {
            ChartTrackStateMetrics metrics = new ChartTrackStateMetrics();
            metrics.setDate(entity.getId().getChartDate());
            metrics.setPreviousDate(entity.getPrevChartDate());
            metrics.setIsEntry(entity.isEntry() || entity.isReentry());
            metrics.setPosition(entity.getCurrentPosition());
            metrics.setPreviousPosition(entity.getPrevPosition());
            metrics.setTrend(entity.getTrend());
            metrics.setDateStreams(JsonNullable.undefined());
            metrics.setIsrcDateStreams(JsonNullable.undefined());
            return metrics;
        }
        ChartTrackStateMetrics metrics = new ChartTrackStateMetrics();
        metrics.setDate(entity.getId().getChartDate());
        metrics.setPreviousDate(entity.getIsrcPrevChartDate());
        Boolean isEntry = (entity.getIsrcIsEntry() != null || entity.getIsrcIsReentry() != null) ?
            Boolean.TRUE.equals(entity.getIsrcIsEntry()) || Boolean.TRUE.equals(entity.getIsrcIsReentry()) :
            null;
        metrics.setIsEntry(isEntry);
        metrics.setPosition(entity.getCurrentPosition());
        metrics.setIsrcPosition(entity.getIsrcCurrentPosition());
        metrics.setPreviousPosition(entity.getIsrcPrevPosition());
        metrics.setTrend(entity.getIsrcTrend());
        metrics.setDateStreams(JsonNullable.undefined());
        metrics.setIsrcDateStreams(JsonNullable.undefined());
        return metrics;
    }
}

