import {Selector} from 'testcafe';
import {ClientFunction} from 'testcafe';

let isLocal = true;
let isMobile = true;

const checkMatrix = async (matrixStringSelector, shouldValues, timeout) => {
    let delta = 0.0001;
    let step = 100;
    timeout = (typeof timeout !== "undefined") ? timeout : 3000;
    let matrixString = await matrixStringSelector;
    let values = matrixString.substring(7, matrixString.length - 1).split(",");
    for (let i = 0; i < values.length; i++) {
        if (typeof shouldValues[i] === "undefined") {
            shouldValues[i] = 0;
        }
        if (shouldValues[i] - delta > values[i] || shouldValues[i] + delta < values[i]) {
            if (timeout > 0) {
                await new Promise((r) => {
                    setTimeout(r, step);
                });
                return await checkMatrix(matrixStringSelector._reExecute(), shouldValues, timeout - step)
            }
            else {
                console.error(values[i] +" not "+ shouldValues[i] + " with", values, shouldValues, i );
                return false;
            }

        }
    }
    return true;
};

const goBack = ClientFunction(() => window.history.back());
const testLocalStorageSet = ClientFunction((key, value) => {
    localStorage.setItem(key, value)
});
const replaceRandom = ClientFunction((sequence) => {
    window.index = 0;
    window.sequence = sequence;
    window.Math.random = function () {
        let res = sequence[window.index];
        window.index = (window.index + 1) % sequence.length;
        return res;
    }
});

async function beforeEachTest(t) {
    // await replaceRandom();
    await waitForMainMenu(t);
}

async function afterEachTest(t) {
    return await t.eval(() => indexedDB.deleteDatabase('wordRotator'));
}

async function waitForMainMenu(t) {

    while (!(await Selector("#play-button").exists)) {
        // console.log("nextIterate");
        await t.wait(100);
    }
    await t.wait(1000);
}

if (isLocal) {
    fixture`Play`
        .page`https://127.0.0.1/pwa/wordRotator/publicTest/`.beforeEach(async t => {
        // .page`https://192.168.0.51/pwa/wordRotator/publicTest/`.beforeEach(async t => {
        await beforeEachTest(t);
    }).afterEach(async t => {
        await afterEachTest(t);
    });
}
else {
    fixture`betaPlay`
        .page`http://beta.wordrotator.silas.link`
        .httpAuth({
            username: 'admin',
            password: '20luxl200'
        }).beforeEach(async t => {
        await beforeEachTest(t);
        // await t.wait(25000);
    }).afterEach(async t => {
        await afterEachTest(t);
    });
}
let dragDimen = 280;

if (isMobile){
    dragDimen = 90;
}

const extraCoins = 50;
const coinsPerLevel = 5;
const helpCost = 25;

const SEGMENT = {
    ONE: 0,
    TWO: 1,
    THREE: 2,
    FOUR: 3,
    FIVE: 4,
    SIX: 5,
    SEVEN: 6,
    EIGHT: 7,
    NINE: 8,
    TEN: 9,
    ELEVEN: 10,
    TWELVE: 11,
    THIRTEEN: 12,
    FOURTEEN: 13,
    FIFTEEN: 14,
    SIXTEEN: 15,
    SEVENTEEN: 16,
    EIGHTEEN: 17,
    NINETEEN: 18,
    TWENTY: 19,
    TWENTYONE: 20
};

test('Play', async t => {
    let levelNumber = 1;

    await waitForMainMenu(t);
    await t
    //Main Menu
        .click(Selector('#play-button')).wait(500)

        //firstTutorial
        .expect(Selector('.tutorial-text .step-1').visible).eql(true)
        .expect(Selector('.tutorial-text .step-2').visible).eql(false)
        .click(Selector('.segment-parent').nth(SEGMENT.THREE))
        .expect(Selector('.tutorial-text .step-2').visible).eql(true)
        .expect(Selector('.tutorial-text .step-1').visible).eql(false)
        .click(Selector('.segment-parent').nth(SEGMENT.THREE))
        .expect(Selector('.tutorial-text .step-2').visible).eql(true);
        await t.click(Selector('.segment-parent').nth(SEGMENT.THREE))
        .expect(Selector('#continue-button').visible).eql(true)
        .click(Selector('#continue-button'))
        .wait(1000);

    levelNumber++;
    //first Level
    await t.expect(await checkMatrix(Selector('.segment.segment-parent.rotate-90').nth(0).getStyleProperty('transform'), [0, 1, -1, 0])).ok()
        .expect(await checkMatrix(Selector('.segment.segment-parent.rotate-90').nth(1).getStyleProperty('transform'), [0, 1, -1, 0])).ok()
        .expect(await checkMatrix(Selector('.segment.segment-parent.rotate-270').getStyleProperty('transform'), [0, -1, 1, 0])).ok()
        .expect(Selector('#won-text').visible).eql(false)
        .expect(Selector('#continue-button').visible).eql(false)
        .expect(Selector('#level-number').textContent).eql("2")
        .click(Selector("#help-button"))
        .click(Selector('.segment-parent').nth(SEGMENT.THREE))
        // .expect(await checkMatrix(Selector('.segment-parent').nth(SEGMENT.THREE).getStyleProperty('transform'), [1, 0, 0,1, 0])).ok()
        .click(Selector('.segment-parent').nth(SEGMENT.ONE))
        .click(Selector('.segment-parent').nth(SEGMENT.TWO))
        .click(Selector('.segment-parent').nth(SEGMENT.ONE))
        .click(Selector('.segment-parent').nth(SEGMENT.TWO))
        .click(Selector('.segment-parent').nth(SEGMENT.ONE))
        .click(Selector('.segment-parent').nth(SEGMENT.TWO))
        .expect(await checkMatrix(Selector('.segment-parent').nth(SEGMENT.ONE).getStyleProperty('transform'), [1, 0, 0, 1])).ok()
        .expect(await checkMatrix(Selector('.segment-parent').nth(SEGMENT.TWO).getStyleProperty('transform'), [1, 0, 0, 1])).ok()
        .expect(Selector('#won-text').visible).eql(true)
        .expect(Selector('#continue-button').visible).eql(true)
        .click(Selector('#continue-button'))
        .expect(Selector('.coin-counter').innerText).contains("" + (coinsPerLevel * levelNumber + extraCoins - helpCost));

    levelNumber++;

    //TWOLevel
    await t.expect(Selector('.segment-parent').nth(SEGMENT.ONE).textContent).eql("BRTH")
        .expect(Selector('.segment-parent').nth(SEGMENT.TWO).textContent).eql("AUER")
        .expect(Selector('.segment-parent').nth(SEGMENT.THREE).textContent).eql("ERAP")
        .expect(Selector('.segment-parent').nth(SEGMENT.FOUR).textContent).eql("EIIE")
        .expect(await checkMatrix(Selector('.segment.segment-parent.rotate-270').nth(0).getStyleProperty('transform'), [0, -1, 1, 0, ])).ok()
        .expect(await checkMatrix(Selector('.segment.segment-parent.rotate-180').getStyleProperty('transform'), [-1, 0, 0, -1, ])).ok()
        .expect(await checkMatrix(Selector('.segment.segment-parent.rotate-360').getStyleProperty('transform'), [1, 0, 0, 1, ])).ok()
        .expect(Selector('#continue-button').visible).eql(false)
        .expect(Selector('#level-number').textContent).eql("3")
        .expect(Selector('#won-text').visible).eql(false)
        .click(Selector('.segment-parent').nth(SEGMENT.TWO))
        .click(Selector('.segment-parent').nth(SEGMENT.ONE))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .click(Selector('.segment-parent').nth(SEGMENT.TWO))
        .expect(await checkMatrix(Selector('.segment-parent').nth(SEGMENT.TWO).getStyleProperty('transform'), [1, 0, 0, 1, ])).ok()
        .click(Selector('.segment-parent').nth(SEGMENT.TWO))
        .expect(await checkMatrix(Selector('.segment-parent').nth(SEGMENT.TWO).getStyleProperty('transform'), [1, 0, 0, 1, ])).ok()
        .expect(Selector('#won-text').visible).eql(true)
        .expect(Selector('#continue-button').visible).eql(true)
        .click(Selector('#continue-button'))
        .expect(Selector('.coin-counter').innerText).contains("" + (coinsPerLevel * levelNumber + extraCoins - helpCost));

    levelNumber++;
    //THREE Level
    await t.expect(Selector('.segment-parent').nth(SEGMENT.ONE).textContent).eql("ZEHO")
        .expect(Selector('.segment-parent').nth(SEGMENT.TWO).textContent).eql("ITCH")
        .expect(Selector('.segment-parent').nth(SEGMENT.THREE).textContent).eql("STSA")
        .expect(Selector('.segment-parent').nth(SEGMENT.FOUR).textContent).eql("RAIS")
        .expect(Selector('.segment-parent').nth(SEGMENT.FIVE).textContent).eql("FEON")
        .click(Selector('.segment-parent').nth(SEGMENT.ONE))
        .click(Selector('.segment-parent').nth(SEGMENT.FIVE))
        .click(Selector('.segment-parent').nth(SEGMENT.THREE))
        .click(Selector('.segment-parent').nth(SEGMENT.ONE))
        .click(Selector('.segment-parent').nth(SEGMENT.FIVE))
        .click(Selector('.segment-parent').nth(SEGMENT.ONE))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .click(Selector('.segment-parent').nth(SEGMENT.FIVE))
        .click(Selector('#continue-button'))
        .expect(Selector('.coin-counter').innerText).contains("" + (coinsPerLevel * levelNumber + extraCoins - helpCost));

    levelNumber++;

    await t.drag(Selector('.segment-parent').nth(SEGMENT.ONE), -dragDimen, 4, {
        offsetX: dragDimen + 50,
        offsetY: 17
    })
        .click(Selector('.segment-parent').nth(SEGMENT.TWELVE))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .click(Selector('.segment-parent').nth(SEGMENT.SEVEN))
        .click(Selector('.segment-parent').nth(SEGMENT.FIVE))
        .click(Selector('.segment-parent').nth(SEGMENT.TWO))
        .click(Selector('.segment-parent').nth(SEGMENT.TWELVE))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .click(Selector('.segment-parent').nth(SEGMENT.SEVEN))
        .click(Selector('.segment-parent').nth(SEGMENT.ELEVEN))
        .click(Selector('.segment-parent').nth(SEGMENT.TWELVE))
        .click(Selector('.segment-parent').nth(SEGMENT.SEVEN))
        .click(Selector('#continue-button'));
    levelNumber++;


    //4. Level
    await t
        .expect(Selector('.segment-parent').nth(SEGMENT.ONE).textContent).eql("FERÜINCKPHREYSSI")
        .expect(Selector('.segment-parent').nth(SEGMENT.TWO).textContent).eql("FERÜ")
        .expect(Selector('.segment-parent').nth(SEGMENT.THREE).textContent).eql("INCK")
        .expect(Selector('.segment-parent').nth(SEGMENT.FOUR).textContent).eql("PHRE")
        .expect(Selector('.segment-parent').nth(SEGMENT.FIVE).textContent).eql("YSSI")
        .expect(Selector('.segment-parent').nth(SEGMENT.SIX).textContent).eql("BEKO")
        .expect(Selector('.segment-parent').nth(SEGMENT.SEVEN).textContent).eql("RACH")
        .expect(Selector('.segment-parent').nth(SEGMENT.EIGHT).textContent).eql("GUFA")
        .expect(Selector('.segment-parent').nth(SEGMENT.NINE).textContent).eql("SSLL")
        .expect(Selector('.segment-parent').nth(SEGMENT.TEN).textContent).eql("IKDEERNZTUTONGPF")
        .expect(Selector('.segment-parent').nth(SEGMENT.ELEVEN).textContent).eql("IKDE")
        .expect(Selector('.segment-parent').nth(SEGMENT.TWELVE).textContent).eql("ERNZ")
        .expect(Selector('.segment-parent').nth(SEGMENT.THIRTEEN).textContent).eql("TUTO")
        .expect(Selector('.segment-parent').nth(SEGMENT.FOURTEEN).textContent).eql("NGPF")
        .drag(Selector('.segment-parent').nth(SEGMENT.TEN), dragDimen, 4, {
            offsetX: dragDimen/5,
            offsetY: 10
        })
        .drag(Selector('.segment-parent').nth(SEGMENT.ONE), dragDimen, 3, {
            offsetX: dragDimen/5,
            offsetY: 10
        })
        .drag(Selector('.segment-parent').nth(SEGMENT.TEN).find('div').withText('I'), dragDimen, 4, {
            offsetX: dragDimen/5,
            offsetY: dragDimen/5
        })
        .drag(Selector('.segment-parent').nth(SEGMENT.ONE).find('div').withText('S'), 10, dragDimen, {
            offsetX: dragDimen,
            offsetY: dragDimen/5
        })
        .drag(Selector('.segment-parent').nth(SEGMENT.TEN), dragDimen, 4, {
            offsetX: 10,
            offsetY: dragDimen/5
        })
        .click(Selector('.segment-parent').nth(SEGMENT.NINE))
        .click(Selector('.segment-parent').nth(SEGMENT.EIGHT))
        .click(Selector('.segment-parent').nth(SEGMENT.NINE))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .click(Selector('.segment-parent').nth(SEGMENT.ELEVEN))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .click(Selector('.segment-parent').nth(SEGMENT.ELEVEN))
        .click(Selector('.segment-parent').nth(SEGMENT.FIVE))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .click(Selector('.segment-parent').nth(SEGMENT.ELEVEN))
        .click(Selector('.segment-parent').nth(SEGMENT.TWELVE))
        .click(Selector('.segment-parent').nth(SEGMENT.FIVE))
        .click(Selector('.segment-parent').nth(SEGMENT.SIX))
        .click(Selector('.segment-parent').nth(SEGMENT.TWELVE))
        .click(Selector('.segment-parent').nth(SEGMENT.SEVEN))
        .click(Selector('.segment-parent').nth(SEGMENT.SIX))
        .expect(Selector('#continue-button').visible).eql(true)
        .expect(Selector('#won-text').visible).eql(true)
        .click(Selector('#continue-button'))
        .expect(Selector('.coin-counter').innerText).contains("" + (coinsPerLevel * levelNumber + extraCoins - helpCost));

    levelNumber++;

    //5. Level
    await t.expect(Selector('#level-number').textContent).eql("" + levelNumber)
        .expect(Selector('.segment-parent').nth(SEGMENT.ONE).textContent).eql("BEANTOBEALBALEUM")
        .expect(Selector('.segment-parent').nth(SEGMENT.SIX).textContent).eql("NUTUNGNGRGHAIEUS")
        .expect(await checkMatrix(Selector('.segment-parent').nth(SEGMENT.ONE).getStyleProperty('transform'), [1, 0, 0, 1, ])).ok()
        .expect(await checkMatrix(Selector('.segment-parent').nth(SEGMENT.SIX).getStyleProperty('transform'), [0, 1, -1, 0, ])).ok()

        .click(Selector('.segment-parent').nth(SEGMENT.THREE))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .click(Selector('.segment-parent').nth(SEGMENT.TWO))
        .drag(Selector('.segment-parent').nth(SEGMENT.SIX).find('div').withText('U'), 3, dragDimen, {
            offsetX: 10,
            offsetY: dragDimen/5
        })
        .click(Selector('.segment-parent').nth(SEGMENT.SEVEN))
        .click(Selector('.segment-parent').nth(SEGMENT.THREE))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .click(Selector('.segment-parent').nth(SEGMENT.FIVE))
        .click(Selector('.segment-parent').nth(SEGMENT.THREE))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .click(Selector('.segment-parent').nth(SEGMENT.SEVEN))
        .expect(Selector('#won-text').visible).eql(true)
        .expect(Selector('#continue-button').visible).eql(true)
        .click(Selector('#continue-button'))
        .expect(Selector('.coin-counter').innerText).contains("" + (coinsPerLevel * levelNumber + extraCoins - helpCost));

    levelNumber++;
    //Level 6
    await t.expect(Selector('#level-number').textContent).eql("" + levelNumber)
        .expect(Selector('.segment-parent').nth(SEGMENT.ONE).textContent).eql("FEGEHLISARBEBERE")
        .expect(await checkMatrix(Selector('.segment-parent').nth(SEGMENT.ONE).getStyleProperty('transform'), [1, 0, 0, 1, ])).ok()
        .expect(await checkMatrix(Selector('.segment-parent').nth(SEGMENT.TEN).getStyleProperty('transform'), [-1, 0, 0, -1, ])).ok()
        .expect(Selector('.segment-parent').nth(SEGMENT.TEN).textContent).eql("ITITSPSCIKARAROT")

        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .click(Selector('.segment-parent').nth(SEGMENT.SEVEN))
        .click(Selector('.segment-parent').nth(SEGMENT.NINE))
        .drag(Selector('.segment-parent').nth(SEGMENT.TEN).find('div').withText('A'), dragDimen, -4, {
            offsetX: 10,
            offsetY: 10
        })
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .click(Selector('.segment-parent').nth(SEGMENT.SIX))
        .click(Selector('.segment-parent').nth(SEGMENT.TWENTY))
        .click(Selector('.segment-parent').nth(SEGMENT.TWELVE))
        .click(Selector('.segment-parent').nth(SEGMENT.NINE))
        .click(Selector('.segment-parent').nth(SEGMENT.THIRTEEN))
        .click(Selector('.segment-parent').nth(SEGMENT.TWENTYONE))
        .click(Selector('.segment-parent').nth(SEGMENT.TWO))
        .click(Selector('.segment-parent').nth(SEGMENT.EIGHT))
        .click(Selector('.segment-parent').nth(SEGMENT.ELEVEN))
        .click(Selector('.segment-parent').nth(SEGMENT.NINE))
        .click(Selector('.segment-parent').nth(SEGMENT.TWELVE))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .click(Selector('.segment-parent').nth(SEGMENT.FOURTEEN))
        .click(Selector('.segment-parent').nth(SEGMENT.SEVEN))
        .click(Selector('.segment-parent').nth(SEGMENT.SIXTEEN))
        .click(Selector('.segment-parent').nth(SEGMENT.TWELVE))
        .click(Selector('.segment-parent').nth(SEGMENT.NINETEEN))
        .click(Selector('.segment-parent').nth(SEGMENT.TWENTYONE))
        .click(Selector('.segment-parent').nth(SEGMENT.TWENTY))
        .click(Selector('.segment-parent').nth(SEGMENT.THIRTEEN))
        .click(Selector('.segment-parent').nth(SEGMENT.TWENTY))
        .click(Selector('.segment-parent').nth(SEGMENT.SEVENTEEN))
        .click(Selector('.segment-parent').nth(SEGMENT.SIXTEEN))
        .click(Selector('.segment-parent').nth(SEGMENT.FOURTEEN))
        .click(Selector('.segment-parent').nth(SEGMENT.NINETEEN))
        .click(Selector('.segment-parent').nth(SEGMENT.TWENTYONE))
        .drag(Selector('.segment-parent').nth(SEGMENT.TEN).find('div').withText('P'), dragDimen, -3, {
            offsetX: 10,
            offsetY: 10
        })
        .expect(await checkMatrix(Selector('div').withText('P').nth(11).find('.segment.segment-parent.layer-2.rotate-360').getStyleProperty('transform'), [1, 0, 0, 1, ])).ok()
        .expect(Selector('#continue-button').visible).eql(true)
        .expect(Selector('#won-text').visible).eql(true)
        .click(Selector('#continue-button'))
        .expect(Selector('.coin-counter').innerText).contains("" + (coinsPerLevel * levelNumber + extraCoins - helpCost));

    levelNumber++;
    //Level 7
    await t.expect(await checkMatrix(Selector('.segment-parent').nth(SEGMENT.SIX).getStyleProperty('transform'), [1, 0, 0, 1, ])).ok()
        .expect(Selector('.segment-row > .child-container').childElementCount).eql(3)

        .drag(Selector('.segment-parent').nth(SEGMENT.ONE), 4, dragDimen, {
            offsetX: 10,
            offsetY: 10
        })
        .click(Selector('.segment-parent').nth(SEGMENT.SEVEN))
        .click(Selector('.segment-parent').nth(SEGMENT.THIRTEEN))
        .click(Selector('.segment-parent').nth(SEGMENT.TWO))
        .drag(Selector('.segment-parent').nth(SEGMENT.ELEVEN), 14, dragDimen, {
            offsetX: 10,
            offsetY: 10
        })
        .click(Selector('.segment-parent').nth(SEGMENT.THREE))
        .click(Selector('.segment-parent').nth(SEGMENT.EIGHT))
        .click(Selector('.segment-parent').nth(SEGMENT.SEVEN))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .click(Selector('.segment-parent').nth(SEGMENT.FIVE))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .click(Selector('.segment-parent').nth(SEGMENT.SEVEN))
        .click(Selector('.segment-parent').nth(SEGMENT.THREE))
        .click(Selector('.segment-parent').nth(SEGMENT.EIGHT))
        .click(Selector('.segment-parent').nth(SEGMENT.NINE))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .drag(Selector('.segment-parent').nth(SEGMENT.ELEVEN), 14, dragDimen, {
            offsetX: 10,
            offsetY: 10
        })
        .click(Selector('#help-button'))
        .wait(5000)
        .expect(Selector('.coin-counter').innerText).contains("" + (coinsPerLevel * levelNumber - helpCost + extraCoins - helpCost))

        .expect(Selector('#won-text').visible).eql(true)
        .expect(Selector('#continue-button').visible).eql(true)
        .click(Selector('#continue-button'))
        .expect(Selector('#site-content').childElementCount).eql(1);
});
test('LoadLastLevel', async t => {
    await waitForMainMenu(t);
    await t.click(Selector('#play-button')).wait(500)
        .expect(Selector('.segment-parent').nth(SEGMENT.ONE).hasClass('locked')).ok()
        .expect(await checkMatrix(Selector('.segment-parent').nth(SEGMENT.ONE).getStyleProperty('transform'), [1, 0, 0, 1, ])).ok()
        .expect(await checkMatrix(Selector('.segment-parent').nth(SEGMENT.TWO).getStyleProperty('transform'), [1, 0, 0, 1, ])).ok()
        .expect(await checkMatrix(Selector('.segment-parent').nth(SEGMENT.THREE).getStyleProperty('transform'), [0, -1, 1, 0, ])).ok()
}).before(async t => {
    await testLocalStorageSet("currentLevel", "{\"id\":15,\"rotations\":[0,0,270],\"locks\":[false,true,true]}");
});
test('LevelRotation', async t => {
    await waitForMainMenu(t);
    await t
        .click(Selector('.segment-parent').nth(SEGMENT.THREE))
        .click(Selector('.segment-parent').nth(SEGMENT.THREE))
        .click(Selector('.segment-parent').nth(SEGMENT.THREE))
        .expect(await checkMatrix(Selector('.segment-parent').nth(SEGMENT.THREE).getStyleProperty('transform'), [0, -1, 1, 0, ])).ok()
        .wait(1000)
        .expect(await checkMatrix(Selector('.segment-parent').nth(SEGMENT.THREE).getStyleProperty('transform'), [0, -1, 1, 0, ])).ok()
        .expect(await checkMatrix(Selector('.segment-parent').nth(SEGMENT.FOUR).getStyleProperty('transform'), [0, 1, -1, 0, ])).ok()
        .click(Selector('.segment-parent').nth(SEGMENT.THREE))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR)).wait(500)
        .expect(Selector('.segment-parent').nth(SEGMENT.ONE).textContent).eql("AHAU")
        .expect(Selector('.segment-parent').nth(SEGMENT.TWO).textContent).eql("NUSB")
        .expect(Selector('.segment-parent').nth(SEGMENT.THREE).textContent).eql("NGAU");
    await goBack();
    await t
        .click(Selector('.segment-parent').nth(SEGMENT.THREE))
        .click(Selector('.segment-parent').nth(SEGMENT.THREE))
        .click(Selector('.segment-parent').nth(SEGMENT.THREE))
        .expect(await checkMatrix(Selector('.segment-parent').nth(SEGMENT.THREE).getStyleProperty('transform'), [0, -1, 1, 0, ])).ok()
        .wait(2000)
        .expect(await checkMatrix(Selector('.segment-parent').nth(SEGMENT.THREE).getStyleProperty('transform'), [0, -1, 1, 0, ])).ok()
        .expect(await checkMatrix(Selector('.segment-parent').nth(SEGMENT.FOUR).getStyleProperty('transform'), [0, 1, -1, 0, ])).ok()
        .click(Selector('.segment-parent').nth(SEGMENT.THREE))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .click(Selector('.segment-parent').nth(SEGMENT.FOUR))
        .expect(Selector('.segment-parent').nth(SEGMENT.ONE).textContent).eql("AHAU")
        .expect(Selector('.segment-parent').nth(SEGMENT.TWO).textContent).eql("NUSB")
        .expect(Selector('.segment-parent').nth(SEGMENT.THREE).textContent).eql("NGAU");

}).before(async t => {
    await replaceRandom([0.5]);
});
test('SendUserstats', async t => {
    await waitForMainMenu(t);
    let matomoCheck = ClientFunction(() => {
        return new Promise((resolve) => {
            function check() {
                window["_paq"].push([function () {
                    resolve(!this["isUserOptedOut"]());
                }]);
            }

            window.addEventListener("load", () => {
                check();
            });
            if (document.readyState === 'complete') {
                check();
            }
        });
    });

    await t
        .click(Selector(".action [data-translation-title='settings']"))
        .expect(Selector("#play-sound").checked).ok()
        .expect(Selector("#play-music").checked).ok()
        .expect(Selector("#track-switch").checked).ok();
    let res = await matomoCheck();
    await t.expect(res).ok()
        .click(Selector(".switch [data-translation='track']"))
        .expect(Selector("#track-switch").checked).eql(false);
    res = await matomoCheck();
    await t.expect(res).eql(false)
        .click(Selector(".switch [data-translation='track']"))
        .expect(Selector("#track-switch").checked).ok();
    res = await matomoCheck();
    await t.expect(res).ok()
}).before(async t => {
    await testLocalStorageSet("matomoShouldTrack", "1");
});

test('SendUserstatsInPrivacyPolicy', async t => {
    await waitForMainMenu(t);
    let matomoCheck = ClientFunction(() => {
        return new Promise((resolve) => {
            function check() {
                window["_paq"].push([function () {
                    resolve(!this["isUserOptedOut"]());
                }]);
            }

            window.addEventListener("load", () => {
                check();
            });
            if (document.readyState === 'complete') {
                check();
            }
        });
    });

    await t
        .click(Selector(".action [data-translation-title='settings']"))
        .click(Selector("#privacy-policy-button"))
        .expect(Selector("#track-switch").checked).ok();
    let res = await matomoCheck();
    await t.expect(res).ok()
        .click(Selector(".switch [data-translation='track']"))
        .expect(Selector("#track-switch").checked).eql(false);
    res = await matomoCheck();
    await t.expect(res).eql(false)
        .click(Selector(".switch [data-translation='track']"))
        .expect(Selector("#track-switch").checked).ok();
    res = await matomoCheck();
    await t.expect(res).ok()
}).before(async t => {
    await testLocalStorageSet("matomoShouldTrack", "1");
});

test('Themes', async t => {
    await waitForMainMenu(t);
    await t
        .expect(Selector("body.dark").visible).ok()
        .click(Selector(".action [data-translation-title='settings']"))
        .expect(Selector("#theme-name").innerText).eql("Dunkel")
        .click(Selector("#theme-name"))
        .click(Selector(".name [data-translation]").nth(3))
        .wait(100)
        .expect(Selector("#theme-name").innerText).eql("Grün")
        .expect(Selector("body.green").visible).ok()
}).before(async t => {
    await testLocalStorageSet("currentTheme", "dark");
});

test('Sharing', async t => {

    let overrideWindowOpener = ClientFunction(() => {
        window.open = (url, blank, noopener) => {
            window.lastWindowOpened = [url, blank, noopener];
        }
    });

    let getLastWindowOpened = ClientFunction(() => {
        return window.lastWindowOpened;
    });


    await waitForMainMenu(t);
    await overrideWindowOpener();

    await t
        .expect(Selector(".share-icon").nth(0).exists).eql(false)
        .expect(Selector(".share-icon").nth(1).exists).eql(false)
        .click(Selector("#share-button"))
        .expect(Selector(".share-icon").nth(0).visible).ok()
        .expect(Selector(".share-icon").nth(1).visible).ok()
        .click(Selector(".share-icon").nth(0))
        .expect(Selector(".share-icon").nth(0).exists).eql(false)
        .expect(Selector(".share-icon").nth(1).exists).eql(false);
    if (isLocal) {
        console.log(await getLastWindowOpened());
        await t.expect(await getLastWindowOpened()).eql(["https://web.whatsapp.com/send?text=" + encodeURIComponent("127.0.0.1/pwa/wordRotator/publicTest/"), "_blank", "noopener"]);
    }
    else {
        console.log(await getLastWindowOpened());
        await t.expect(await getLastWindowOpened()).eql(["https://web.whatsapp.com/send?text=" + encodeURIComponent("beta.wordrotator.silas.link/"), "_blank", "noopener"]);
    }
    await t
        .click(Selector("#share-button"))
        .expect(Selector(".share-icon").nth(0).visible).ok()
        .expect(Selector(".share-icon").nth(1).visible).ok()
        .click(Selector(".share-icon").nth(1))
        .expect(Selector(".share-icon").nth(0).exists).eql(false)
        .expect(Selector(".share-icon").nth(1).exists).eql(false);
    if (isLocal) {
        console.log(await getLastWindowOpened());
        await t.expect(await getLastWindowOpened()).eql(["https://t.me/share/url?url=" + encodeURIComponent("127.0.0.1/pwa/wordRotator/publicTest/"), "_blank", "noopener"]);
    }
    else {
        console.log(await getLastWindowOpened());
        await t.expect(await getLastWindowOpened()).eql(["https://t.me/share/url?url=" + encodeURIComponent("beta.wordrotator.silas.link/"), "_blank", "noopener"]);
    }
});