<!DOCTYPE html>
<html lang="<%= lang %>">

<head>
    <%- include('common-meta') %>

        <link rel="stylesheet" href="/stylesheets/styles.css">
        <link rel="stylesheet" href="/stylesheets/header.css">
        <link rel="stylesheet" href="/stylesheets/detail.css">

        <script type="text/javascript"
            src="https://platform-api.sharethis.com/js/sharethis.js#property=67e0d66a54a3d000192a4615&product=inline-share-buttons&source=platform"
            async="async"></script>

        <script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "CreativeWork",
            "name": "<%= translate.printableColoringPage[lang] %>: <%= detail.title %>",
            "description": "<%= detail.desc %>",
            "url": "https://art.pcoloring.com<%= uri %>",
            "image": "<%= detail.thumb %>",
            "category": "<%= detail.tags[0] %>",
            "keywords": "coloring page, <%= detail.title %>, color by number, paint by number, free, printable,  <%= detail.tags.join() %>",
            "contentRating": "General Audience",
            "mainEntityOfPage": "https://art.pcoloring.com<%= uri %>",
            "publisher": {
                "@type": "Organization",
                "name": "JCCY",
                "logo": {
                    "@type": "ImageObject",
                    "url": "https://art.pcoloring.com/assets/icon/icon.webp"
                }
            },
            "offers": {
                "@type": "Offer",
                "priceCurrency": "USD",
                "price": "0.00",
                "eligibleRegion": {
                "@type": "Place",
                "name": "Worldwide"
                },
                "url": "https://art.pcoloring.com<%= uri %>"
            },
            "author": {
                "@type": "Person",
                "name": "<%= detail.user.username %>"
            },
            "datePublished": "<%= detail.publishTime %>"
        }
    </script>
</head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-JBGGVGLHTP"></script>
<script>
    window.dataLayer = window.dataLayer || [];
    function gtag() { dataLayer.push(arguments); }
    gtag('js', new Date());

    gtag('config', 'G-JBGGVGLHTP');
</script>

<body>
    <%- include('header') %>
        <div class="details">
            <div class="poster"><img src="<%= detail.thumb %>" alt="<%= detail.title %>"></div>
            <div class="description">
                <div style="display: flex; justify-content: space-between;">
                    <h1>
                        <%= detail.title %>
                    </h1>
                    <div class="sharethis-inline-share-buttons"></div>
                </div>
                <% if (detail.totalStartCount> 0) { %>
                    <div style="color:gray;">
                        <%=detail.totalStartCount%>
                            <%= translate.interested[lang] %>
                    </div>
                    <% } %>
                        <p>
                            <%= translate.artist[lang] %>: <a href="/<%= lang %>/artist/<%= detail.user._id %>"
                                    class="tag-button">
                                    <%= detail.user.username %>
                                </a>
                        </p>
                        <p>
                            <%= translate.publishTime[lang] %>: <%= detail.publishTime %>
                        </p>
                        <p>
                            <%= translate.tag[lang] %>:
                                <% detail.tags.forEach(tag=> { %>
                                    <a href="/<%= lang %>/tag/<%= tag %>" class="tag-button">
                                        <%= tag %>
                                    </a>
                                    <% }); %>
                        </p>
                        <p>
                            <%= detail.copy ?? detail.desc %>
                        </p>
                        <div class="button-wrapper">
                            <a href="/play/<%= detail._id %>" class="play-button">
                                <%= translate.play[lang] %>
                            </a>
                            <a id="appBtn" class="play-button" style="background-color: darkolivegreen;">
                                <%= translate.playOnApp[lang] %>
                            </a>
                            <a href="/download/pdf/<%= detail._id %>" class="play-button"
                                style="background-color: lightseagreen;">
                                <%= translate.download[lang] %>
                            </a>
                            <a id="printBtn" onclick="printImage('<%= detail._id %>')" class="play-button"
                                style="background-color: black;">
                                <%= translate.print[lang] %>
                            </a>
                        </div>
            </div>
        </div>

        <p style=" display: flex; justify-content: center; color: #777; font-size: 18px; font-weight: 500;">
            <%= translate.mayYouLike[lang] %>:
        </p>

        <div class="content" style="margin-bottom: 40px;">
            <div class="image-grid">
                <% relates.forEach(item=> { %>
                    <div class="image-card">
                        <a href="<%= item.uri %>"><img src="<%= item.thumb %>" alt="<%= item.title %>"></a>
                        <div class="card-title">
                            <%= item.title %>
                        </div>
                    </div>
                    <% }); %>
            </div>
        </div>

        <!-- <%- include('pagination') %> -->


        <script>
            function isMobileDevice() {
                return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
            }

            window.onload = function () {
                if (isMobileDevice()) {
                    const printBtn = document.getElementById('printBtn');
                    printBtn.style.display = 'none'; // 移动端隐藏打印按钮
                }
            };

            document.getElementById('appBtn').addEventListener('click', function () {
                const userAgent = navigator.userAgent || navigator.vendor || window.opera;

                // Android 检测
                if (/android/i.test(userAgent)) {
                    window.open('https://play.google.com/store/apps/details?id=com.pcoloring.art.puzzle.color.by.number', '_blank');
                }
                // iOS 检测
                else if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
                    window.open('https://apps.apple.com/gb/app/art-number-coloring-book/id1575480118', '_blank');
                }
                // 其他操作系统（例如桌面）
                else {
                    // 可以显示一个提示，或者跳转到通用的下载页面
                    console.log('无法确定操作系统，或者为桌面操作系统');
                    window.open('https://pcoloring.com/anc/', '_blank');
                }
            });

            async function printImage(id) {
                try {
                    const response = await fetch(`/download/pdf/${id}`);

                    if (!response.ok) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }

                    const pdfBlob = await response.blob();
                    const pdfUrl = URL.createObjectURL(pdfBlob);

                    const printWindow = window.open(pdfUrl, '_blank');
                    printWindow.onload = () => {
                        printWindow.print();
                    };

                    URL.revokeObjectURL(pdfUrl); // 释放 URL 对象
                } catch (error) {
                    console.error('Error printing image:', error);
                }
            }
        </script>
</body>

</html>