@using Zap.BI.Portal.Helpers
@using Zap.Text
@model Zap.BI.Portal.Pages.Printing.PrintModel
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title></title>
    <script>
        // Slightly better error info in case of parsing errors in PhantomJS
        window.onerror = function(message, source, lineno, colno, error) {
            console.error('Global JavaScript error: ' + message + ' at ' + source + ', line ' + lineno + ', col ' + colno, error);
            return true; // Prevent default browser error handling
        };
    </script>
    <script>
        function FakeDeferred() {
            this._doneCallbacks = [];
        }

        FakeDeferred.prototype = {
            done: function (func) {
                if (this._resolved) func();
                this._doneCallbacks.push(func);
                return this;
            },

            catch: function () { },

            resolve: function () {
                this._resolved = true;

                this._doneCallbacks.forEach(function (func) {
                    func.apply(window);
                });
                return this;
            },
            
            state: function () {
                return this._resolved ? "resolved" : "pending" ;
            }
        };

        Zap = { portalReady: new FakeDeferred() };
        window.pendingDeferreds = [Zap.portalReady];

        Object.assign = Object.assign || function (obj) {
            var otherObjs = Array.prototype.slice.call(arguments, 1);
            otherObjs.forEach(function (other) {
                Object.keys(other).forEach(function (key) {
                    obj[key] = other[key];
                });
            });
            return obj;
        };
        window.EnumDefinitions = [];
        window.TextEntries = [];
        window.LegendPlacementText = {};
        window.FormattedNumberText = {};
        window.ChartText = {};
        window.ChartPrintInfo = [];
    </script>
    <script>
        @Html.RawSanitizedScript(@Model.Script)
    </script>
    <style>
        @Html.Raw(@Model.Style)
    </style>
</head>
<body class="print-document @Model.BodyClasses.Join(" ")">
    @foreach (string page in Model.Pages)
    {
        <div class="print-page">
            <div class="print-content-container">
                @Html.RawSanitizedHtml(page)
            </div>
        </div>
    }
<script>
        window.onload = function () {
            Zap.portalReady.resolve();
            
            waitForPendingPromises()
                .then(waitForImageLoad)
                .finally(signalPhantom);
        };
        
        function waitForImageLoad() {
            return new Promise(function (resolve) {
                checkImages();
                
                function checkImages() {
                    if (areImagesLoaded()) 
                        setTimeout(resolve, 0); // images are 'complete' before actually being rendered, setTimeout waits for a re-paint
                    else
                        setTimeout(checkImages, 10);
                }
            });
        }

        function areImagesLoaded() {
            var images = document.images;
            for (var i = 0; i < images.length; i++) {
                if (!images[i].complete)
                    return false;
            }

            return true;
        }

        function waitForPendingPromises() {
            return Promise.all(window.pendingDeferreds.map(function (p) { return p.catch(function () { }); }));
        }

        function signalPhantom() {
            if (window.callPhantom)
                window.callPhantom();
        }
    </script>
</body>
</html>
