using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing.Printing;
using System.Globalization;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Xml.Linq;
using static DragonVideoPlayer.WindowsThumbnailProvider;
using static System.Collections.Specialized.BitVector32;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrayNotify;

namespace DragonVideoPlayer
{
  
  
    public class Radiko_Info
    {
        public string Name { get; set; }
        public string Title { get; set; }
        public string Info { get; set; }
        public string Url { get; set; }
        public string Img { get; set; }
        public string Desc { get; set; }
        public string Pfm { get; set; }
        public string StationName { get; set; }
    }
    public class epg
    {



        public static (string title, string desc, string image_path, string category) GetCurrentProgram(string xmlPath, string channelId , XDocument doc)
        {
           
            if (!File.Exists(xmlPath))
            {
                return ("", "", "", "");


            }
            try
            {
               

                DateTime now = DateTime.Now;

                var program = doc.Descendants("programme")
                .Where(p => (string)p.Attribute("channel") == channelId)
                .FirstOrDefault(p =>
                {
                    DateTime start = ParseXmlTvTime((string)p.Attribute("start"));
                    DateTime stop = ParseXmlTvTime((string)p.Attribute("stop"));

                    return start <= now && now < stop;
                });


                return (


                    program?.Element("title")?.Value ?? "",
                    program?.Element("desc")?.Value ?? "",

                      program?.Element("image")?.Value ?? "",
                      program?.Element("category")?.Value ?? ""

                );

            }
            catch (Exception ex)
            {

                Debug.WriteLine(ex.Message + ex.StackTrace);

            }
            return ("", "", "", "");
        }
        public string Create_single_RadikoHtml(string xmlPath, string stationId, XDocument doc)
        {
            if (!File.Exists(xmlPath))
                return "";

         

            var station = doc.Descendants("station")
                .FirstOrDefault(s => (string)s.Attribute("id") == stationId);

            if (station == null)
                return $"<html><body>station '{stationId}' が見つかりません。</body></html>";

            string stationName = station.Element("name")?.Value ?? stationId;

            var programs = station
                .Descendants("prog")
                .Select(p => new
                {
                    Start = (string)p.Attribute("ft") ?? "",
                    Stop = (string)p.Attribute("to") ?? "",
                    Title = p.Element("title")?.Value ?? "",
                    Desc = p.Element("desc")?.Value ?? "",
                    Info = p.Element("info")?.Value ?? "",
                    Pfm = p.Element("pfm")?.Value ?? "",
                    Img = p.Element("img")?.Value ?? "",
                    Url = p.Element("url")?.Value ?? ""
                });

            StringBuilder html = new();

            html.Append("""
<html>
<head>
<meta charset="UTF-8">
<style>
body {
    background:#202020;
    color:white;
    font-family:Meiryo;
}

.station {
    color:#ffcc00;
    font-size:24px;
    margin-bottom:20px;
}

.item {
    border-bottom:1px solid #555;
    padding:10px;
    margin-bottom:10px;
}

.time {
    color:#66ccff;
    font-size:16px;
}

.title {
    font-size:20px;
    font-weight:bold;
    margin:5px 0;
}

.pfm {
    color:#99ff99;
}

img {
    max-width:300px;
    margin-top:8px;
}

.desc {
    margin-top:8px;
}

.info {
    margin-top:8px;
    color:#cccccc;
}
</style>
</head>
<body>

<div class="station">Radiko:
""");

            html.Append(System.Net.WebUtility.HtmlEncode(stationName));
            html.Append(" (");
            html.Append(System.Net.WebUtility.HtmlEncode(stationId));
            html.Append(")</div>");

            foreach (var p in programs)
            {
                string start =
                    p.Start.Length >= 12
                    ? p.Start.Substring(8, 2) + ":" + p.Start.Substring(10, 2)
                    : "";

                string stop =
                    p.Stop.Length >= 12
                    ? p.Stop.Substring(8, 2) + ":" + p.Stop.Substring(10, 2)
                    : "";

                html.Append($"""
<div class="item">

<div class="time">
{start} ～ {stop}
</div>

<div class="title">
{System.Net.WebUtility.HtmlEncode(p.Title)}
</div>

""");

                if (!string.IsNullOrWhiteSpace(p.Pfm))
                {
                    html.Append($"""
<div class="pfm">
出演: {System.Net.WebUtility.HtmlEncode(p.Pfm)}
</div>
""");
                }

                if (!string.IsNullOrWhiteSpace(p.Img))
                {
                    html.Append($"""
<div>
<img src="{p.Img}">
</div>
""");
                }

                if (!string.IsNullOrWhiteSpace(p.Url))
                {
                    html.Append($"""
<div>
<a href="{p.Url}" target="_blank">{p.Url}</a>
</div>
""");
                }

                if (!string.IsNullOrWhiteSpace(p.Info))
                {
                    html.Append($"""
<div class="info">
{p.Info}
</div>
""");
                }

                if (!string.IsNullOrWhiteSpace(p.Desc))
                {
                    html.Append($"""
<div class="desc">
{p.Desc}
</div>
""");
                }

                html.Append("""
</div>
""");
            }

            html.Append("""
</body>
</html>
""");

            return html.ToString();
        }

        public string CreateNowRadikoHtml(string xmlPath, XDocument doc)
        {
            if (!File.Exists(xmlPath))
                return "";

           

            DateTime now = DateTime.Now;

            StringBuilder html = new();

            html.Append("""
<html>
<head>
<meta charset="UTF-8">
<style>
body {
    background:#202020;
    color:white;
    font-family:Meiryo;
}

.station {
    color:#ffcc00;
    font-size:22px;
    margin-top:15px;
}

.time {
    color:#66ccff;
}

.title {
    font-size:18px;
    font-weight:bold;
    margin-top:5px;
}

.pfm {
    color:#99ff99;
}

.item {
    border-bottom:1px solid #555;
    padding:10px;
}

img {
    max-width:200px;
    margin-top:5px;
}
</style>
</head>
<body>
<h1>Radiko：現在放送中の番組</h1>
""");

            foreach (var station in doc.Descendants("station"))
            {
                string stationName =
                    station.Element("name")?.Value ?? "";

                var prog = station.Descendants("prog")
                    .FirstOrDefault(p =>
                    {
                        DateTime start = DateTime.ParseExact(
                            (string)p.Attribute("ft"),
                            "yyyyMMddHHmmss",
                            CultureInfo.InvariantCulture);

                        DateTime stop = DateTime.ParseExact(
                            (string)p.Attribute("to"),
                            "yyyyMMddHHmmss",
                            CultureInfo.InvariantCulture);

                        return start <= now && now < stop;
                    });

                if (prog == null)
                    continue;

                string title = prog.Element("title")?.Value ?? "";
                string desc = prog.Element("desc")?.Value ?? "";
                string info = prog.Element("info")?.Value ?? "";
                string pfm = prog.Element("pfm")?.Value ?? "";
                string img = prog.Element("img")?.Value ?? "";
                string url = prog.Element("url")?.Value ?? "";

                string startTime =
                    ((string)prog.Attribute("ft")).Substring(8, 2) + ":" +
                    ((string)prog.Attribute("ft")).Substring(10, 2);

                string stopTime =
                    ((string)prog.Attribute("to")).Substring(8, 2) + ":" +
                    ((string)prog.Attribute("to")).Substring(10, 2);

                html.Append("<div class=\"item\">");

                html.Append("<div class=\"station\">");
                html.Append(System.Net.WebUtility.HtmlEncode(stationName));
                html.Append("</div>");

                html.Append("<div class=\"time\">");
                html.Append(startTime);
                html.Append(" ～ ");
                html.Append(stopTime);
                html.Append("</div>");

                html.Append("<div class=\"title\">");
                html.Append(System.Net.WebUtility.HtmlEncode(title));
                html.Append("</div>");

                if (!string.IsNullOrWhiteSpace(pfm))
                {
                    html.Append("<div class=\"pfm\">");
                    html.Append(System.Net.WebUtility.HtmlEncode(pfm));
                    html.Append("</div>");
                }

                if (!string.IsNullOrWhiteSpace(img))
                {
                    html.Append("<div><img src=\"");
                    html.Append(img);
                    html.Append("\"></div>");
                }

                if (!string.IsNullOrWhiteSpace(url))
                {
                    html.Append("<div><a href=\"");
                    html.Append(url);
                    html.Append("\" target=\"_blank\">");
                    html.Append(url);
                    html.Append("</a></div>");
                }

                if (!string.IsNullOrWhiteSpace(info))
                {
                    html.Append("<div>");
                    html.Append(info);
                    html.Append("</div>");
                }

                if (!string.IsNullOrWhiteSpace(desc))
                {
                    html.Append("<div>");
                    html.Append(System.Net.WebUtility.HtmlDecode(desc));
                    html.Append("</div>");
                }

                html.Append("</div>");
            }

            html.Append("""
</body>
</html>
""");

            return html.ToString();
        }

        public string CreateRadikoAiiEpgHtml(string xmlPath, XDocument doc)
        {
            if (!File.Exists(xmlPath))
                return "";

           

            StringBuilder html = new();

            html.Append("""
<html>
<head>
<meta charset="UTF-8">
<style>
body {
    background:#202020;
    color:white;
    font-family:Meiryo;
}

.station {
    background:#333;
    color:#ffcc00;
    font-size:24px;
    padding:8px;
    margin-top:20px;
}

.item {
    border-bottom:1px solid #555;
    padding:8px;
}

.time {
    color:#66ccff;
    font-weight:bold;
}

.title {
    font-size:18px;
    font-weight:bold;
    margin-top:4px;
}

.pfm {
    color:#99ff99;
    margin-top:4px;
}

.info {
    color:#cccccc;
    margin-top:4px;
}

.desc {
    margin-top:4px;
}

img {
    max-width:250px;
    margin-top:6px;
}

a {
    color:#66ccff;
}
</style>
</head>
<body>
<h1>Radiko全て番組表</h1>
""");

            foreach (var station in doc.Descendants("station"))
            {
                string stationName =
                    station.Element("name")?.Value ?? "";

                string stationId =
                    (string)station.Attribute("id") ?? "";

                html.Append("""
<div class="station">
""");

                html.Append(System.Net.WebUtility.HtmlEncode(stationName));
                html.Append(" (");
                html.Append(System.Net.WebUtility.HtmlEncode(stationId));
                html.Append(")");

                html.Append("""
</div>
""");

                var programs = station
                    .Descendants("prog")
                    .OrderBy(p => (string)p.Attribute("ft"));

                foreach (var prog in programs)
                {
                    string start =
                        ((string)prog.Attribute("ft") ?? "").Length >= 12
                        ? ((string)prog.Attribute("ft")).Substring(8, 2) + ":" +
                          ((string)prog.Attribute("ft")).Substring(10, 2)
                        : "";

                    string stop =
                        ((string)prog.Attribute("to") ?? "").Length >= 12
                        ? ((string)prog.Attribute("to")).Substring(8, 2) + ":" +
                          ((string)prog.Attribute("to")).Substring(10, 2)
                        : "";

                    string title = prog.Element("title")?.Value ?? "";
                    string desc = prog.Element("desc")?.Value ?? "";
                    string info = prog.Element("info")?.Value ?? "";
                    string pfm = prog.Element("pfm")?.Value ?? "";
                    string img = prog.Element("img")?.Value ?? "";
                    string url = prog.Element("url")?.Value ?? "";

                    html.Append("""
<div class="item">
""");

                    html.Append("<div class=\"time\">");
                    html.Append(start);
                    html.Append(" ～ ");
                    html.Append(stop);
                    html.Append("</div>");

                    html.Append("<div class=\"title\">");
                    html.Append(System.Net.WebUtility.HtmlEncode(title));
                    html.Append("</div>");

                    if (!string.IsNullOrWhiteSpace(pfm))
                    {
                        html.Append("<div class=\"pfm\">");
                        html.Append(System.Net.WebUtility.HtmlEncode(pfm));
                        html.Append("</div>");
                    }

                    if (!string.IsNullOrWhiteSpace(info))
                    {
                        html.Append("<div class=\"info\">");
                        html.Append(info);
                        html.Append("</div>");
                    }

                    if (!string.IsNullOrWhiteSpace(desc))
                    {
                        html.Append("<div class=\"desc\">");
                        html.Append(System.Net.WebUtility.HtmlDecode(desc));
                        html.Append("</div>");
                    }

                    if (!string.IsNullOrWhiteSpace(img))
                    {
                        html.Append("<div><img src=\"");
                        html.Append(img);
                        html.Append("\"></div>");
                    }

                    if (!string.IsNullOrWhiteSpace(url))
                    {
                        html.Append("<div><a href=\"");
                        html.Append(url);
                        html.Append("\" target=\"_blank\">");
                        html.Append(url);
                        html.Append("</a></div>");
                    }

                    html.Append("""
</div>
""");
                }
            }

            html.Append("""
</body>
</html>
""");

            return html.ToString();
        }
        public string CreateAllEpgHtml_whitelist(string xmlPath, HashSet<string> allowChannels,String name, XDocument doc)
        {
            if (!File.Exists(xmlPath))
            {
                return "";


            }
            
            StringBuilder html = new();
            try
            {
              
                int epg_limit = 3;
                String epg_limit_st = config_class.config_text_read("epgの時間制限");
                if (string.IsNullOrEmpty(epg_limit_st) == false)
                {

                    epg_limit = int.Parse(epg_limit_st);
                }

                DateTime now = DateTime.Now;// XMLは +0000 なのでUTCで比較
                DateTime limit = now.AddHours(epg_limit);
                var programs = doc.Descendants("programme")
      .Select(p => new
      {
          ChannelId = (string)p.Attribute("channel") ?? "",
          Title = p.Element("title")?.Value ?? "",
          Desc = p.Element("desc")?.Value ?? "",
          StartTime = ParseXmlTvTime((string)p.Attribute("start")!),
          StopTime = ParseXmlTvTime((string)p.Attribute("stop")!)
      })
   .Where(p =>
    p.StopTime > now &&
    p.StartTime <= limit &&
    allowChannels.Contains(p.ChannelId)
)
      .OrderBy(p => p.StartTime)
      .Select(p => new
      {
          p.ChannelId,
          p.Title,
          p.Desc,
          Start = p.StartTime.ToString("HH:mm"),
          Stop = p.StopTime.ToString("HH:mm")
      });




                html.Append("""
<html>
<head>
<meta charset="UTF-8">
<style>
body {background:#202020;
    color:white;
    font-family:Meiryo;
}
.item {margin:10px;
    padding:10px;
    border-bottom:1px solid #555;
}
.channel {color:#ffcc00;
    font-size:18px;
}
.time {color:#66ccff;
}
</style>
</head>
<body>
<h1>
""");
                html.Append(name);
                html.Append(" "+xmlPath + " " + epg_limit_st + "時間");
             
                html.Append("""
</h1>
""");

                foreach (var p in programs)
                {
                    string channelName =
                        doc.Descendants("channel")
                           .FirstOrDefault(c => (string)c.Attribute("id") == p.ChannelId)?
                           .Element("display-name")?.Value ?? p.ChannelId;

                    html.Append($"""
        <div class="item">
            <div class="channel">{channelName}</div>
        <div class="time">
            {p.Start} ～ {p.Stop}
        </div>
            <div>{p.Title}</div>
            <div>{p.Desc}</div>
        </div>
        """);
                }

                html.Append("</body></html>");

            }
            catch (Exception ex)
            {

                Debug.WriteLine(ex.Message + ex.StackTrace);

            }

            return html.ToString();
        }
        public string CreateAllEpgHtml(string xmlPath, XDocument doc)
        {
            if (!File.Exists(xmlPath))
            {
                return "";


            }
            HashSet<string> removeChannels = new()
{
    "テレビ神奈川_jp",
    "テレ玉_jp",
    "ParadiseTV",
    "Splash.jp",
    "RedCherry.jp",
    "PhoenixTelevision.jp"
};
            StringBuilder html = new();
            try
            {
              
                int epg_limit = 3;
              String epg_limit_st = config_class.config_text_read("epgの時間制限");
                if (string.IsNullOrEmpty(epg_limit_st)==false)
                {

                     epg_limit = int.Parse(epg_limit_st);
                }
              
                    DateTime now = DateTime.Now;// XMLは +0000 なのでUTCで比較
                DateTime limit = now.AddHours(epg_limit);
                var programs = doc.Descendants("programme")
      .Select(p => new
      {
          ChannelId = (string)p.Attribute("channel") ?? "",
          Title = p.Element("title")?.Value ?? "",
          Desc = p.Element("desc")?.Value ?? "",
          StartTime = ParseXmlTvTime((string)p.Attribute("start")!),
          StopTime = ParseXmlTvTime((string)p.Attribute("stop")!)
      })
      .Where(p =>
          p.StopTime > now &&
          p.StartTime <= now.AddHours(3) &&
          !removeChannels.Contains(p.ChannelId)
      )
      .OrderBy(p => p.StartTime)
      .Select(p => new
      {
          p.ChannelId,
          p.Title,
          p.Desc,
          Start = p.StartTime.ToString("HH:mm"),
          Stop = p.StopTime.ToString("HH:mm")
      });




                html.Append("""
<html>
<head>
<meta charset="UTF-8">
<style>
body {background:#202020;
    color:white;
    font-family:Meiryo;
}
.item {margin:10px;
    padding:10px;
    border-bottom:1px solid #555;
}
.channel {color:#ffcc00;
    font-size:18px;
}
.time {color:#66ccff;
}
</style>
</head>
<body>
<h1>全てのTV欄
""");
                html.Append(" "+xmlPath+" "+ epg_limit_st+"時間");

                html.Append("""
</h1>
""");

                foreach (var p in programs)
                {
                    string channelName =
                        doc.Descendants("channel")
                           .FirstOrDefault(c => (string)c.Attribute("id") == p.ChannelId)?
                           .Element("display-name")?.Value ?? p.ChannelId;

                    html.Append($"""
        <div class="item">
            <div class="channel">{channelName}</div>
        <div class="time">
            {p.Start} ～ {p.Stop}
        </div>
            <div>{p.Title}</div>
            <div>{p.Desc}</div>
        </div>
        """);
                }

                html.Append("</body></html>");

            }
            catch (Exception ex)
            {

                Debug.WriteLine(ex.Message + ex.StackTrace);

            }

            return html.ToString();
        }
        public string CreateNowEpgHtml(string xmlPath, XDocument doc)
        {
            if (!File.Exists(xmlPath))
            {
                return "";


            }
            HashSet<string> removeChannels = new()
{
    "テレビ神奈川_jp",
    "テレ玉_jp",
    "ParadiseTV",
    "Splash.jp",
    "RedCherry.jp",
    "PhoenixTelevision.jp"
};
            StringBuilder html = new();
            try { 
            

            DateTime now = DateTime.Now;// XMLは +0000 なのでUTCで比較

                var programs = doc.Descendants("programme")
        .Where(p =>
        {
            DateTime start = ParseXmlTvTime(
                (string)p.Attribute("start"));

            DateTime stop = ParseXmlTvTime(
                (string)p.Attribute("stop"));

            string channelId = (string)p.Attribute("channel") ?? "";

            return start <= now &&
                   now < stop &&
                   !removeChannels.Contains(channelId);
        })
        .Select(p => new
        {
            ChannelId = (string)p.Attribute("channel") ?? "",
            Title = p.Element("title")?.Value ?? "",
            Desc = p.Element("desc")?.Value ?? "",
            Start = ParseXmlTvTime(
                (string)p.Attribute("start")).ToString("HH:mm"),

            Stop = ParseXmlTvTime(
                (string)p.Attribute("stop")).ToString("HH:mm")
        });


                html.Append("""
    <html>
    <head>
    <meta charset="UTF-8">
    <style>
    body {
        background:#202020;
        color:white;
        font-family:Meiryo;
    }
    .item {
        margin:10px;
        padding:10px;
        border-bottom:1px solid #555;
    }
    .channel {
        color:#ffcc00;
        font-size:18px;
    }
    .time {
        color:#66ccff;
    }
    </style>
    </head>
    <body>
    <h1>現在放送中
    """);
             
                html.Append(" " + xmlPath);

                html.Append("""
</h1>
""");


                foreach (var p in programs)
            {
                string channelName =
                    doc.Descendants("channel")
                       .FirstOrDefault(c => (string)c.Attribute("id") == p.ChannelId)?
                       .Element("display-name")?.Value ?? p.ChannelId;

                html.Append($"""
        <div class="item">
            <div class="channel">{channelName}</div>
        <div class="time">
            {p.Start} ～ {p.Stop}
        </div>
            <div>{p.Title}</div>
            <div>{p.Desc}</div>
        </div>
        """);
            }

            html.Append("</body></html>");

        }
            catch (Exception ex)
            {

                Debug.WriteLine(ex.Message + ex.StackTrace);

            }

            return html.ToString();
        }
        private static DateTime ParseXmlTvTime(string time)
        {
            // "20260622173200 +0900"
            //                 ↓
            // "20260622173200 +09:00"
            string fixedTime = time.Insert(time.Length - 2, ":");

            return DateTimeOffset.ParseExact(
                fixedTime,
                "yyyyMMddHHmmss zzz",
                CultureInfo.InvariantCulture
            ).LocalDateTime; // 日本PCなら日本時間になる
        }
        public static Radiko_Info Radik_GetCurrentProgram(string xmlPath, string stationId, XDocument doc)
        {

            if (!File.Exists(xmlPath))
            {
                return null;


            }

            try
            {

              

                DateTime now = DateTime.Now;

                var station_id = doc
     .Descendants("station")
     .FirstOrDefault(x => (string)x.Attribute("id") == stationId)?
     .Element("name")?
     .Value ?? "";
                var station = doc
                .Descendants("station")
                .FirstOrDefault(x => (string)x.Attribute("id") == stationId);
                
                if (station == null)
                    return null;

                var prog = station
                    .Descendants("prog")
                    .FirstOrDefault(x =>
                    {
                        string ft = (string)x.Attribute("ft");
                        string to = (string)x.Attribute("to");

                        if (string.IsNullOrEmpty(ft) || string.IsNullOrEmpty(to))
                            return false;

                        DateTime start = DateTime.ParseExact(
                            ft,
                            "yyyyMMddHHmmss",
                            CultureInfo.InvariantCulture);

                        DateTime end = DateTime.ParseExact(
                            to,
                            "yyyyMMddHHmmss",
                            CultureInfo.InvariantCulture);

                        return now >= start && now < end;
                    });
               
                if (prog == null)
                    return null;


                return new Radiko_Info
                {
                    StationName = station_id ?? "",
                    Title = (string)prog.Element("title") ?? "",
                    Info = (string)prog.Element("info") ?? "",
                    Url = (string)prog.Element("url") ?? "",
                    Img = (string)prog.Element("img") ?? "",
                    Desc = (string)prog.Element("desc") ?? "",
                    Pfm = (string)prog.Element("pfm") ?? ""
                };

            }
            catch (Exception ex)
            {

                Debug.WriteLine(ex.Message + ex.StackTrace);

            }
            return null;

        }
        public string Create_single_EpgHtml(string xmlPath, string channelId, XDocument doc)
        {
            if (!File.Exists(xmlPath))
            {
                return "";


            }
         
           

            StringBuilder html = new StringBuilder();
            try { 
         
         
                    // チャンネル名取得
                string channelName =
                doc.Descendants("channel")
                   .FirstOrDefault(x => (string)x.Attribute("id") == channelId)?
                   .Element("display-name")?.Value ?? channelId;


            // 番組取得
            //var programs = doc.Descendants("programme")
            //    .Where(x => (string)x.Attribute("channel") == channelId)
            //    .OrderBy(x => (string)x.Attribute("start"));
            var programs = doc.Descendants("programme")
    .Where(x => (string)x.Attribute("channel") == channelId)
    .OrderBy(x => (string)x.Attribute("start"))
    .Select(x => new
    {
        Start = (string)x.Attribute("start"),
        Stop = (string)x.Attribute("stop"),
        Title = x.Element("title")?.Value ?? "",
        Desc = x.Element("desc")?.Value ?? "",
        Image = x.Element("image")?.Value ?? ""
    });

        

            html.Append("""
<html>
<head>
<meta charset="UTF-8">
<style>
body{
    font-family: "Meiryo";
    background:#202020;
    color:white;
}
h1{
    color:#ffcc00;
}
.program{
    border-bottom:1px solid #555;
    padding:8px;
}
.time{
    color:#66ccff;
    font-weight:bold;
}
.title{
    font-size:16px;
}
.desc{
    color:#cccccc;
    margin-top:5px;
}
.thumb {
    width: 240px;
    height: auto;
    margin: 8px 0;
    border-radius: 6px;
}
</style>
</head>
<body>
""");
                if (string.IsNullOrEmpty(channelId))
                {
                    html.Append($"<h1>チャンネルを選択してません</h1>");

                }
                else
                {
                    html.Append($"<h1>{channelName} {xmlPath}</h1>");


                }


                foreach (var p in programs)
            {
                string st = p.Start.Substring(8, 2) + ":" + p.Start.Substring(10, 2);
                string ed = p.Stop.Substring(8, 2) + ":" + p.Stop.Substring(10, 2);

                html.Append($"""
    <div class="program">
        <div class="time">{st} ～ {ed}</div>
        <div class="title">{p.Title}</div>
    """);

                // 画像がある場合のみ表示
                if (!string.IsNullOrEmpty(p.Image))
                {
                    html.Append($"""
        <div>
            <img src="{p.Image}" class="thumb">
        </div>
        """);
                }

                html.Append($"""
        <div class="desc">{p.Desc}</div>
    </div>
    """);


                 


                    html.Append("""
</body>
</html>
""");
            }

        }
            catch (Exception ex)
            {

                Debug.WriteLine(ex.Message + ex.StackTrace);

            }

            return html.ToString();
           

        }
    }
}
