using Shared.Engine; using Shared.Engine.Pools; using Shared.Models.Base; using System.Text; using System.Text.Json.Serialization; namespace Shared.Models.Templates { public class EpisodeTpl : ITplResult { public List data { get; private set; } public VoiceTpl? vtpl { get; private set; } public EpisodeTpl() : this(null, 20) { } public EpisodeTpl(int capacity) : this(null, capacity) { } public EpisodeTpl(in VoiceTpl vtpl) : this(vtpl, 20) { } public EpisodeTpl(in VoiceTpl? vtpl, int capacity) { this.vtpl = vtpl; data = new List(capacity); } public void Append(string name, string title, string s, string e, string link, string method = "play", in StreamQualityTpl? streamquality = null, in SubtitleTpl? subtitles = null, string streamlink = null, string voice_name = null, VastConf vast = null, List headers = null, int? hls_manifest_timeout = null, in SegmentTpl? segments = null, string subtitles_call = null) { if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(link)) { data.Add(new EpisodeDto( method, link, streamlink, Http.NormalizeHeaders(headers), streamquality?.ToObject(emptyToNull: true), subtitles?.ToObject(emptyToNull: true), subtitles_call, short.TryParse(s, out short _s) ? _s : (short)0, short.TryParse(e, out short _e) ? _e : (short)0, voice_name, name, $"{title} ({e} серия)", hls_manifest_timeout, vast, segments?.ToObject() )); } } public void Append(in VoiceTpl vtpl) { this.vtpl = vtpl; } public bool IsEmpty => data == null || data.Count == 0; public int Length => data?.Count ?? 0; public string ToHtml() { if (IsEmpty) return string.Empty; var sb = ToBuilderHtml(); string result = sb.ToString(); StringBuilderPool.Return(sb); return result; } public StringBuilder ToBuilderHtml() { if (IsEmpty) return StringBuilderPool.EmptyHtml; var html = StringBuilderPool.Rent(); bool firstjson = true; if (vtpl.HasValue) vtpl.Value.WriteTo(html); html.Append("
"); foreach (var i in data) { var vast = i.vast ?? AppInit.conf.vast; html.Append("
"); html.Append("
"); UtilsTpl.HtmlEncode(i.name, html); html.Append("
"); firstjson = false; } html.Append("
"); return html; } public string ToJson(in VoiceTpl vtpl) { this.vtpl = vtpl; return ToJson(); } public string ToJson() { if (IsEmpty) return string.Empty; var sb = ToBuilderJson(); string result = sb.ToString(); StringBuilderPool.Return(sb); return result; } public StringBuilder ToBuilderJson() { if (IsEmpty) return StringBuilderPool.EmptyJsonObject; var json = StringBuilderPool.Rent(); UtilsTpl.WriteJson(json, new EpisodeResponseDto( vtpl?.ToObject(emptyToNull: true), data ), EpisodeJsonContext.Default.EpisodeResponseDto); return json; } } [JsonSourceGenerationOptions( DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault )] [JsonSerializable(typeof(EpisodeDto))] [JsonSerializable(typeof(Dictionary))] [JsonSerializable(typeof(SubtitleDto))] [JsonSerializable(typeof(List))] [JsonSerializable(typeof(VastConf))] [JsonSerializable(typeof(SegmentDto))] [JsonSerializable(typeof(List))] [JsonSerializable(typeof(Dictionary>))] [JsonSerializable(typeof(EpisodeResponseDto))] [JsonSerializable(typeof(VoiceDto))] [JsonSerializable(typeof(EpisodeDto))] [JsonSerializable(typeof(List))] [JsonSerializable(typeof(List))] public partial class EpisodeJsonContext : JsonSerializerContext { } public readonly struct EpisodeDto { public string method { get; } public string url { get; } public string stream { get; } public Dictionary headers { get; } public Dictionary quality { get; } public IReadOnlyList subtitles { get; } public string subtitles_call { get; } public short? s { get; } public short? e { get; } public string details { get; } public string name { get; } public string title { get; } public int? hls_manifest_timeout { get; } public VastConf vast { get; } public Dictionary> segments { get; } [JsonConstructor] public EpisodeDto( string method, string url, string stream, Dictionary headers, Dictionary quality, IReadOnlyList subtitles, string subtitles_call, short? s, short? e, string details, string name, string title, int? hls_manifest_timeout, VastConf vast, Dictionary> segments) { this.method = method; this.url = url; this.stream = stream; this.headers = headers; this.quality = quality; this.subtitles = subtitles; this.subtitles_call = subtitles_call; this.s = s; this.e = e; this.details = details; this.name = name; this.title = title; this.hls_manifest_timeout = hls_manifest_timeout; this.vast = vast; this.segments = segments; } } public readonly struct EpisodeResponseDto { public string type { get; } public IReadOnlyList voice { get; } public IReadOnlyList data { get; } [JsonConstructor] public EpisodeResponseDto(IReadOnlyList voice, IReadOnlyList data) { type = "episode"; this.voice = voice; this.data = data; } } }