export class NoSleep {
    static oldIOS: boolean = typeof navigator !== 'undefined' && parseFloat(('' + (/CPU.*OS ([0-9_]{3,4})[0-9_]{0,1}|(CPU like).*AppleWebKit.*Mobile/i.exec(navigator.userAgent) || [0, ''])[1]).replace('undefined', '3_2').replace('_', '.').replace('_', '')) < 10 && !window["MSStream"];
    static webm: string = 'data:video/webm;base64,GkXfo0AgQoaBAUL3gQFC8oEEQvOBCEKCQAR3ZWJtQoeBAkKFgQIYU4BnQI0VSalmQCgq17FAAw9CQE2AQAZ3aGFtbXlXQUAGd2hhbW15RIlACECPQAAAAAAAFlSua0AxrkAu14EBY8WBAZyBACK1nEADdW5khkAFVl9WUDglhohAA1ZQOIOBAeBABrCBCLqBCB9DtnVAIueBAKNAHIEAAIAwAQCdASoIAAgAAUAmJaQAA3AA/vz0AAA=';
    static mp4: string = 'data:video/mp4;base64,[encoded data, 6456 characters]';

    static hasInit: boolean = false;
    static noSleepTimer: number;
    static noSleepVideo: HTMLVideoElement;
    static isStart: boolean;

    public static init() {
        if (this.hasInit) return;

        if (!this.oldIOS) {
            let noSleepVideo = document.createElement('video');

            noSleepVideo.setAttribute('muted', '');
            noSleepVideo.setAttribute('title', 'Intro');
            noSleepVideo.setAttribute('playsinline', '');

            this.addSourceToVideo(noSleepVideo, 'webm', this.webm);
            this.addSourceToVideo(noSleepVideo, 'mp4', this.mp4);

            noSleepVideo.addEventListener('loadedmetadata', function () {
                if (noSleepVideo.duration <= 1) {
                    noSleepVideo.setAttribute('loop', '');
                } else {
                    noSleepVideo.addEventListener('timeupdate', function () {
                        if (noSleepVideo.currentTime > 0.5) {
                            noSleepVideo.currentTime = Math.random();
                        }
                    });
                }
            });

            document.body.appendChild(noSleepVideo);
            this.noSleepVideo = noSleepVideo;
        }

        this.hasInit = true;
        this.isStart = false;
    }

    public static start() {
        if (!this.isStart) {
            console.log("nosleep start");
            if (this.oldIOS) {
                this.stop();
                this.noSleepTimer = window.setInterval(() => {
                    if (!document.hidden) {
                        window.location.href = window.location.href.split('#')[0];
                        window.setTimeout(window.stop, 0);
                    }
                }, 15000);
            } else {
                this.noSleepVideo.play();
            }
            this.isStart = true;
        }
    }

    public static stop() {
        if (this.oldIOS) {
            if (this.noSleepTimer) {
                window.clearInterval(this.noSleepTimer);
                this.noSleepTimer = 0;
            }
        } else {
            this.noSleepVideo.pause();
        }
        this.isStart = false;
    }

    static addSourceToVideo(element: HTMLVideoElement, type: string, dataURI: string) {
        let source = document.createElement('source');
        source.src = dataURI;
        source.type = 'video/' + type;
        element.appendChild(source);
    }

}