// ==UserScript==

// @name         anfragen-pool-ux-improver
// @namespace    https://test-anfragen-pool.swrapp.net/*
// @version      2024-08-02_14-36

// @resource     DASHBOARD_CSS https://gitlab.ard.de/Jan.Seipel/test-ap-ux-improver/-/raw/main/stylesheets/test-ap-style-dashboard.css
// @resource     TICKETZOOM_CSS https://gitlab.ard.de/Jan.Seipel/test-ap-ux-improver/-/raw/main/stylesheets/test-ap-style-ticketzoom.css
// @resource     TICKETFREETEXT_CSS https://gitlab.ard.de/Jan.Seipel/test-ap-ux-improver/-/raw/main/stylesheets/test-ap-style-ticketfreetext.css
// @resource     TICKETPHONE_CSS https://gitlab.ard.de/Jan.Seipel/test-ap-ux-improver/-/raw/main/stylesheets/test-ap-style-ticketphone.css

// @description  passt den IDA-Anfragen-Pool an die Anforderungen von IDA-Desk an
// @author       Jan Seipel
// @match        https://test-anfragen-pool.swrapp.net/*
// @exclude      https://test-anfragen-pool.swrapp.net/otrs/index.pl?Action=Admin*
// @match        https://anfragen-pool.swrapp.net/*
// @exclude      https://anfragen-pool.swrapp.net/otrs/index.pl?Action=Admin*


// @grant        GM_getResourceText
// @grant        GM_addStyle

// ==/UserScript==
window.addEventListener('load', function() {
    'use strict';

    // ====== CUSTOM STYLESHEETS HINZUFÜGEN ======

    // Checke, auf welcher Seite du dich befindest um
    // jeweils ein anderes Stylesheet zu laden
    const URL = document.URL

    // Alle verfügbaren Stylesheets & deren URL-Identifier
    const stylings = [
        {"id": "AgentTicketZoom", "stylesheet": "TICKETZOOM_CSS"},
        {"id": "AgentTicketFreeText", "stylesheet": "TICKETFREETEXT_CSS"},
        {"id": "AgentTicketPhone", "stylesheet": "TICKETPHONE_CSS"},
    ]

    // Dashboard-Stylesheet auf allen Seiten laden,
    // damit überall die Labels angezeigt werden
    GM_addStyle(GM_getResourceText("DASHBOARD_CSS"))

    // Zusätzliche Stylesheets je nach URL laden
    for (let style of stylings) {
        URL.includes(style.id) && GM_addStyle(GM_getResourceText(style.stylesheet))
    }

    // ====== WEITERE JAVASCRIPT FUNKTIONEN ======

    // Erfassungsmaske Telefonticket immer
    // im neuen Fenster öffnen
    document.querySelector("#ToolBar li.PhoneTicket > a").setAttribute("target","_blank")

    // automatischer Reload nach x Sekunden
    if (URL.includes("AgentDashboard") || URL === "https://anfragen-pool.swrapp.net/otrs/index.pl" || URL === "https://test-anfragen-pool.swrapp.net/otrs/index.pl") {
        setInterval(function(){
            if ( // stoppt autom. Reload, wenn bestimmte Dialogfenster geöffnet sind
                document.querySelectorAll("#Overlay").length === 0 &&
                ![...document.querySelectorAll("div[id*='setting']")].map(d=>d.style.display).includes("block") &&
                document.querySelectorAll("div[class*='SettingsWidget Expanded']").length === 0
            ) {
                window.location.reload(1);
                console.log("reloaded") //  
            } else {
                console.log("overlay now detected")
            }
        }, 1000*120); // hier den Sekundenwert einstellen
        
        
        // Ticket-ID kürzen, aber einen Link anbieten, der sich im neuen Tab öffnet
        const replaceTicketLinks = () => {        
            const ticketLinks = [...document.querySelectorAll("td > a[href*='TicketID']")]
            ticketLinks.map((d) => {
                d.innerHTML = "↗️";
                d.style.textAlign = "center";
                d.style.width = "100%";
                d.target = "_blank";
                d.title = "Ticket in neuem Tab öffnen";
            })
    
            const columnName = document.querySelector(".TicketNumber")
            columnName.innerHTML = " "
        }
        this.setInterval(() => {
            replaceTicketLinks()
        }, 100)
    }

    // warnhinweis oben einfügen, falls man sich gerade auf dem Testsystem befindet

    if (URL.includes("test-anfragen-pool.swrapp.net")) {
        const warning = document.createElement("div");
        warning.innerHTML = "Achtung: Testsystem!";
        warning.style.color = "red";
        warning.style.position = "absolute";
        warning.style.top = "40px";
        warning.style.textAlign = "center";
        warning.style.width = "100%";
        warning.style.paddingTop = "10px";
        warning.style.fontSize = "2em";
        document.body.appendChild(warning);
    }

}, false);