var timer = 0;
function addEvent() {
    var a = document.querySelector(".edit-stream-info-buttons");
    a.addEventListener("click", () => {
        let button = a.querySelector("button");
        let style = getComputedStyle(button).backgroundColor;
        if (getComputedStyle(button).backgroundColor === "rgb(119, 44, 232)" || getComputedStyle(button).backgroundColor === "rgb(145, 71, 255)") {
            sendPrism("clickStart");
            clearInterval(timer)
            timer = setInterval(() => {
                button = a.querySelector("button");
                if (getComputedStyle(button).backgroundColor === "rgb(145, 71, 255)" || getComputedStyle(button).backgroundColor === "rgb(119, 44, 232)") {
                    sendPrism("clickEnd");
                    clearInterval(timer);
                }
            }, 100);
        }
    });
}

function tipTest() {
    var a = document.querySelector(".snackbar-list__items");

    var observer = new MutationObserver(function (mutationRecoards, observer) {
        let tipContainer = a.querySelector(".tw-snackbar");
        if (tipContainer) {
            let style = getComputedStyle(tipContainer);
            sendPrism(style.backgroundColor === "rgb(0, 245, 147)" ? "saveSuccess" : "saveFailed");
        } else {
            //console.log('tip no result')
        }
    });

    observer.observe(a, {
        childList: true
    });
}

function sendPrism(type = "", message = "") {
    type && console.log('send to prism params',type, message)
    if (typeof window.sendToCpp === "function" && type) {
        window.sendToCpp("twitch", JSON.stringify({ data: type }));
    }
}

var bindTimer = 0;

bindTimer = setInterval(() => {
    if (document.querySelector(".edit-stream-info-buttons button")) {
        tipTest();
        addEvent();
        console.log("bindSuccess");
        sendPrism("pageShow");
        clearInterval(bindTimer);
    } else {
        console.log("waitBind");
    }
}, 600);

(function changeStyle() {
    const styleTag = document.createElement('style');
    styleTag.innerHTML = `
 .edit-broadcast-scrollable {
    max-height: 100vh !important;
  }
  div.edit-stream-info-buttons {
    display:none; !important;
  }

  div.edit-stream-info-popout {
    background-color: #272727 !important;
  }
`;
    document.head.appendChild(styleTag)
})();

window.addEventListener('clickSave', function clickSave() {
    const btn = document.querySelectorAll('.edit-stream-info-buttons button')[0];
    btn.click()
    console.log('clickSave')
    setTimeout(()=>{
        sendPrism("saveSuccess");
    },3000)
})


function checkInputRed(id) {
    var targetDiv = document.getElementById(id);
    if (!targetDiv) return false;
    var parent = targetDiv.parentNode;
    var grandparent = parent.parentNode;
    var lastChild = grandparent.lastElementChild.lastElementChild;
    return getComputedStyle(lastChild).color === 'rgb(255, 130, 128)'
}

setInterval(() => {
    if (checkInputRed("edit-broadcast-title-formgroup") && checkInputRed("edit-broadcast-go-live-formgroup")) {
        return location.reload()
    }

    let errorTip = document.getElementById("edit-broadcast-title-formgroup-error")
    if (errorTip) {
        sendPrism('isErrorInput', errorTip.innerText);
    } else {
        sendPrism('isSuccessInput')
    }
}, 1000)
