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

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;

import io.delphiplatform.api.v3.model.video.AbstractChart;
import io.delphiplatform.api.v3.rdb.entity.RegionEntity;
import io.delphiplatform.api.v3.rdb.entity.shazam.FactShazamChartEntity;
import io.delphiplatform.api.v3.rdb.entity.shazam.ShazamChartEntity;
import io.delphiplatform.api.v3.rdb.entity.video.ShazamChartTrackSummaryReportEntity;
import io.delphiplatform.api.v3.rdb.entity.video.ShazamCityEntity;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

/**
 * ShazamChart
 */
@Setter
@Getter
@SuperBuilder
@EqualsAndHashCode(callSuper = true)
public class ShazamChart extends AbstractChart {

    @JsonProperty("city_id")
    private String cityId;

    @JsonProperty("city_name")
    private String cityName;

    public static ShazamChart of(ShazamChartTrackSummaryReportEntity entity) {
        if (entity == null) {
            return null;
        }
        ShazamCityEntity shazamCity = entity.getChart().getShazamCity();
        String cityName = shazamCity == null ? null : shazamCity.getCityName();

        return
            ShazamChart.builder().cityId(entity.getId().getShazamCityId())
                .cityName(cityName)
                .name(entity.getChart().getName())
                .countryCode(entity.getRegion().getCountryCode())
                .totalPositions(entity.getTotalPositions())
                .chartId(entity.getChart().getChartId())
                .build();
    }

    public static ShazamChart of(FactShazamChartEntity factShazamChartEntity) {
        ShazamChartEntity entity = factShazamChartEntity.getChart();
        if (entity == null) {
            return null;
        }
        RegionEntity region = entity.getRegion();

        String countryCode = null;
        if (region != null) {
            countryCode = entity.getRegion().getCountryCode();
        }

        ShazamCityEntity shazamCity = entity.getShazamCity();
        String cityName = null;
        String cityId = null;
        if (shazamCity != null) {
            cityName = shazamCity.getCityName();
            cityId = shazamCity.getShazamCityId();
            countryCode = shazamCity.getCountryCode();
        }

        return
            ShazamChart.builder().cityId(cityId)
                .cityName(cityName)
                .name(entity.getName())
                .countryCode(countryCode)
                .totalPositions(factShazamChartEntity.getTotalPositions())
                .chartId(entity.getChartId())
                .build();
    }

    public static ShazamChart of(ShazamChartEntity entity) {
        if (entity == null) {
            return null;
        }
        RegionEntity region = entity.getRegion();

        String countryCode = null;
        if (region != null) {
            countryCode = entity.getRegion().getCountryCode();
        }

        ShazamCityEntity shazamCity = entity.getShazamCity();
        String cityName = null;
        String cityId = null;
        if (shazamCity != null) {
            cityName = shazamCity.getCityName();
            cityId = shazamCity.getShazamCityId();
            countryCode = shazamCity.getCountryCode();
        }

        return
            ShazamChart.builder().cityId(cityId)
                .cityName(cityName)
                .name(entity.getName())
                .countryCode(countryCode)
                .chartId(entity.getChartId())
                .build();
    }

    @JsonInclude(Include.NON_NULL)
    @Override
    public Integer getTotalPositions() {
        return super.getTotalPositions();
    }
}

