package de.christinecoenen.code.zapp.app.player;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Objects;

import de.christinecoenen.code.zapp.app.mediathek.model.MediathekShow;
import de.christinecoenen.code.zapp.app.settings.repository.StreamQualityBucket;
import de.christinecoenen.code.zapp.model.ChannelModel;

public class VideoInfo {

	public static VideoInfo fromShow(MediathekShow show) {
		VideoInfo videoInfo = new VideoInfo();
		videoInfo.url = show.getVideoUrl();
		videoInfo.urlHighestQuality = show.getVideoUrlHd();
		videoInfo.urlLowestQuality = show.getVideoUrlLow();
		videoInfo.title = show.getTitle();
		videoInfo.subtitle = show.getTopic();
		videoInfo.subtitleUrl = show.getSubtitleUrl();
		videoInfo.hasDuration = true;
		return videoInfo;
	}

	public static VideoInfo fromChannel(ChannelModel channel) {
		VideoInfo videoInfo = new VideoInfo();
		videoInfo.url = channel.getStreamUrl();
		videoInfo.title = channel.getName();
		videoInfo.subtitle = channel.getSubtitle();
		videoInfo.hasDuration = false;
		return videoInfo;
	}

	@NonNull
	private String url = "";

	private String urlLowestQuality = null;

	private String urlHighestQuality = null;

	@NonNull
	private String title = "";

	@NonNull
	private String subtitle = "";

	@Nullable
	private String subtitleUrl;

	private boolean hasDuration = false;

	@NonNull
	String getUrl() {
		return url;
	}

	@NonNull
	String getUrl(StreamQualityBucket quality) {
		switch (quality) {
			case MEDIUM:
				return url;
			case HIGHEST:
				return urlHighestQuality == null ? url : urlHighestQuality;
			default:
				return urlLowestQuality == null ? url : urlLowestQuality;
		}
	}

	@NonNull
	public String getTitle() {
		return title;
	}

	@NonNull
	public String getSubtitle() {
		return subtitle;
	}

	@Nullable
	String getSubtitleUrl() {
		return subtitleUrl;
	}

	public boolean hasSubtitles() {
		return subtitleUrl != null;
	}

	boolean hasDuration() {
		return hasDuration;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		VideoInfo videoInfo = (VideoInfo) o;
		return hasDuration == videoInfo.hasDuration &&
			url.equals(videoInfo.url) &&
			Objects.equals(urlLowestQuality, videoInfo.urlLowestQuality) &&
			Objects.equals(urlHighestQuality, videoInfo.urlHighestQuality) &&
			title.equals(videoInfo.title) &&
			subtitle.equals(videoInfo.subtitle) &&
			Objects.equals(subtitleUrl, videoInfo.subtitleUrl);
	}

	@NonNull
	@Override
	public String toString() {
		return "VideoInfo{" +
			"url='" + url + '\'' +
			", urlLowestQuality='" + urlLowestQuality + '\'' +
			", urlHighestQuality='" + urlHighestQuality + '\'' +
			", title='" + title + '\'' +
			", subtitle='" + subtitle + '\'' +
			", subtitleUrl='" + subtitleUrl + '\'' +
			", hasDuration=" + hasDuration +
			'}';
	}
}

@@ -1251,26 +1251,16 @@
  = %22%22;%0A%0A
-%09@NonNull%0A
 %09private
@@ -1271,29 +1271,24 @@
 ing subtitle
- = %22%22
 ;%0A%0A%09@Nullabl
@@ -2417,24 +2417,33 @@
 %0A%09%09%09
-subtitle.equals(
+Objects.equals(subtitle, 
 vide
@@ -2516,24 +2516,172 @@
 leUrl);%0A%09%7D%0A%0A
+%09@Override%0A%09public int hashCode() %7B%0A%09%09return Objects.hash(url, urlLowestQuality, urlHighestQuality, title, subtitle, subtitleUrl, hasDuration);%0A%09%7D%0A%0A
 %09@NonNull%0A%09@

Fix crash when returning some live streams from background playback