// 忽略异常
const IGNORE_EXCEPTION = (funs) => funs.forEach(fun => { try { fun(); } catch (error) { } });

// 资源加载后回调
const LOADED = (element, callback) => {
    let loaded = false;
    if (typeof callback === 'function') {
        element.onload = element.onreadystatechange = () => {
            if (!loaded && (!element.readyState || /loaded|complete/.test(element.readyState))) {
                element.onload = element.onreadystatechange = null;
                loaded = true;
                callback();
            }
        }
    }
};

// 字符串模板
const HEREDOC = (fn) => fn.toString().split('\n').slice(1, -1).join('\n') + '\n';

// 自定义字体
const FONT_PINGYONG = HEREDOC(() => {/*
    <style type="text/css">
        @font-face {
            font-family: 'PinyonScript';
            font-style: normal;
            font-weight: 400;
            src: local('Pinyon Script'), local('PinyonScript'), url(data:font/woff2;base64,[encoded data, 30984 characters]) format('woff2');
            unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
        }
    </style>
 */});

// 雨伞背景
const ADD_UMBRELLA_BACKGROUND = (callback) => {
    const SCRIPT = document.createElement('script');
    //SCRIPT.src = '//s0.pstatp.com/cdn/expire-1-M/canvas-nest/2.0.4/canvas-nest.js';
	SCRIPT.src = '//cdn.jsdelivr.net/gh/ldxw/CDN@v0.02.1/scfone-theme-js/canvas-nest.js/2.0.4/canvas-nest.js';
    SCRIPT.opacity = '0.1';
    SCRIPT.zIndex = '-99';
    SCRIPT.count = '10';
    LOADED(SCRIPT, callback);
    document.body.appendChild(SCRIPT);
};

const ADD_IMGAGE_BACKGROUND = () => {
    //const IMAGE = 'url(https://img12.360buyimg.com/img/jfs/t1/56992/25/15607/359235/5dc93624E8223dc25/41cac0ada12d3ad9.jpg)'
	const IMAGE = 'url(//cdn.jsdelivr.net/gh/ldxw/CDN@v0.02.1/scfone-theme-js/img/bg.jpg)'
    const DIV = document.createElement('div');
    DIV.style.background = IMAGE;
    DIV.style.pointerEvents = 'none';
    DIV.style.backgroundRepeat = 'no-repeat';
    DIV.style.backgroundSize = 'cover';
    DIV.style.position = 'fixed';
    DIV.style.left = '0';
    DIV.style.top = '0';
    DIV.style.width = '100%';
    DIV.style.height = '100%';
    DIV.style.filter = 'blur(2px)';
    DIV.style.msFilter = 'blur(2px)';
    DIV.style.webkitFilter = 'blur(2px)';
    DIV.style.mozFilter = 'blur(2px)';
    DIV.style.oFilter = 'blur(2px)';
    DIV.style.opacity = '0.2';
    document.body.appendChild(DIV);
};

// 移除 README.md
const REMOVE_README = () => {
    const TBODY = document.querySelector('#list-table').querySelector('tbody');
    const TRS = TBODY.querySelectorAll('tr');
    let readme = null;
    TRS.forEach(TR => {
        const A = TR.querySelector('a');
        if (A && A.href.indexOf('README.md') >= 0) {
            readme = TR;
            return;
        }
    });
    readme ? TBODY.removeChild(readme) : null;
};

// 时间和 IP 居中显示，时间走动
const ADD_TIME_AND_IP = () => {
    // 居中并显示
    const CENTER = document.createElement('center');
    CENTER.style.paddingBottom = '40px';
    const FONT = document.body.querySelector('font');
    FONT.id = 'date';
    FONT.removeAttribute('color');
    CENTER.appendChild(FONT);
    document.body.insertBefore(CENTER, document.body.querySelector('#mask'));

    // 时间走动
    setInterval(() => {
        const ADDZERO = num => num < 10 ? '0' + num : num; const FUN_WEEK = week => '日一二三四五六'[week];

        const DATE = new Date();
        const YEAR = DATE.getFullYear();
        const MONTH = DATE.getMonth() + 1;
        const DAY = DATE.getDate();
        const HOURS = DATE.getHours();
        const MINUTES = DATE.getMinutes();
        const SECONDS = DATE.getSeconds();
        const WEEK = DATE.getDay();

        const ELEMENT = document.querySelector('#date');
        const IP = ELEMENT.innerHTML.split(' ').pop();
        ELEMENT.innerHTML = 'yyyy-mm-dd hh:MM:ss 星期w'
            .replace('yyyy', YEAR)
            .replace('mm', ADDZERO(MONTH))
            .replace('dd', ADDZERO(DAY))
            .replace('hh', ADDZERO(HOURS))
            .replace('MM', ADDZERO(MINUTES))
            .replace('ss', ADDZERO(SECONDS))
            .replace('w', FUN_WEEK(WEEK))
            + ' ' + IP;
    }, 1000);
};

// 其他 UI
const CHANGE_OHTER_UI = () => {
    // 标题
    document.head.innerHTML += FONT_PINGYONG
    const TITLE = document.body.querySelector('.title');
    TITLE.style.margin = '0';
    TITLE.style.lineHeight = '2em';
    TITLE.style.fontSize = '300%';
    TITLE.style.fontFamily = 'PinyonScript';

    // 语言选择框
    const LANGUAGES = document.body.querySelector('.changelanguage');
    if (screen.width < 800) {
        // 移动端隐藏
        LANGUAGES.style.display = 'none';
    } else {
        LANGUAGES.style.position = 'fixed';
        LANGUAGES.style.top = '5px';
        LANGUAGES.style.outline = 'none';
        LANGUAGES.style.borderRadius = '2em';
        LANGUAGES.style.height = '1.5em';
    }

    // 左上角管理
    const OPERATE = document.body.querySelector('.operate');
    if (OPERATE) {
        OPERATE.style.position = 'fixed';
        OPERATE.style.top = '5px';
        OPERATE.style.left = '5px';
        OPERATE.style.border = '1px #00000066 solid';
        OPERATE.style.borderRadius = '2em';
        OPERATE.style.background = 'white';
        OPERATE.style.minWidth = '3em';
        OPERATE.style.textAlign = 'center';
        OPERATE.style.lineHeight = '1.3em';
    }

    // BODY
    document.body.style = '';
    document.body.zIndex = '99';
    document.body.style.margin = '0';
    document.body.style.padding = '0';
    // 文件列表和 README 阴影
    const LIST_WRAPPERs = document.body.querySelectorAll('.list-wrapper');
    LIST_WRAPPERs.forEach(WRAPPER => {
        WRAPPER.style.boxShadow = '0 0 32px 0 rgba(128, 128, 128, 0.18)';
    });
    // 文件列表圆角
    const LTFILEs = document.body.querySelectorAll('.list-table .file');
    LTFILEs.forEach(FILE => {
        FILE.style.borderRadius = '25px 0px 0px 25px';
    });
    const LTSIZEs = document.body.querySelectorAll('.list-table .size');
    LTSIZEs.forEach(SIZE => {
        SIZE.style.borderRadius = '0px 25px 25px 0px';
    });
    // 文件路径行距
    const THEADER = document.body.querySelector('.list-header-container .table-header');
    THEADER.style.lineHeight = '1.3em';

    // 移动端隐藏修改时间
    if (screen.width < 800) {
        const LTUPDATEATs = document.body.querySelectorAll('.list-table .updated_at');
        LTUPDATEATs.forEach(UPDATEAT => {
            UPDATEAT.style.display = 'none';
        });
    }
    // 按钮
    const BUTTONs = document.body.querySelectorAll('button');
    BUTTONs.forEach(BUTTON => {
        BUTTON.style.border = '1px #00000066 solid';
        BUTTON.style.borderRadius = '2em';
        BUTTON.style.background = 'white';
    });
    const SUBMITs = document.body.querySelectorAll('input[type="submit" i]');
    SUBMITs.forEach(SUBMIT => {
        SUBMIT.style.border = '1px #00000066 solid';
        SUBMIT.style.borderRadius = '2em';
        SUBMIT.style.background = 'white';
        SUBMIT.style.minWidth = '5em';
    });
    const FILE_BUTTONs = document.body.querySelectorAll('input[type="button" i]');
    FILE_BUTTONs.forEach(FBUTTON => {
        FBUTTON.style.border = '1px #00000066 solid';
        FBUTTON.style.borderRadius = '2em';
        FBUTTON.style.background = 'white';
        FBUTTON.style.minWidth = '5em';
        FBUTTON.style.height = '1.9em';
    });
    // 输入框
    const PASSWORDs = document.body.querySelectorAll('input[type="password" i], input[type="file" i]');
    PASSWORDs.forEach(PASSWORD => {
        PASSWORD.style.outline = 'none';
        PASSWORD.style.textAlign = 'center';
        PASSWORD.style.lineHeight = '1.3em';
        PASSWORD.style.border = '1px #00000066 solid';
        PASSWORD.style.borderRadius = '2em';
        PASSWORD.style.background = 'white';
        PASSWORD.style.minWidth = '10em';
    });
    // URL
    const URL = document.body.querySelector('#url');
    if (URL) {
        URL.style.resize = 'none';
        URL.style.outline = 'none';
        URL.style.textAlign = 'center';
        URL.style.height = 'initial';
        URL.style.borderRadius = '2em';
    }
};

// 主函数
const MAIN_HANDLER = () => {
    IGNORE_EXCEPTION([
        ADD_IMGAGE_BACKGROUND,
        ADD_UMBRELLA_BACKGROUND,
        CHANGE_OHTER_UI,
        REMOVE_README,
        ADD_TIME_AND_IP
    ]);
    document.body.removeAttribute('hidden');
};

// 文档加载完毕执行主函数
document.addEventListener('DOMContentLoaded', MAIN_HANDLER);