import {
    AndroidBridge,
    App, applyPolyfills,
    InitPromise, MenuAction,
    ShareManager,
    SmsShareButton, TelegramShareButton,
    Theme,
    ThemeManager,
    Translator,
    WhatsappShareButton
} from "./lib/pwa-lib";
import {LoginSite, RegistrationSite} from "./lib/pwa-user-management";

import "./lib/pwa-lib"
import "./lib/pwa-core"
import "./lib/pwa-assets"
import "./lib/pwa-code-management"
import "./lib/pwa-user-management"
import "./lib/pwa-contact"

import {SettingsManager, SettingsSite} from "./lib/pwa-core";

import {
    InstallManager,
    Matomo,
    MatomoShareButton,
    MyStorageManager,
    SoundManager
} from "./lib/pwa-assets";
import {MenuSite} from "../module/Application/pwa/js/site/MenuSite";
import {WordRotatorSettingFragment} from "../module/Application/pwa/js/Fragment/WordRotatorSettingFragment";

import {PrivacyPolicySite} from "../module/Application/pwa/js/site/PrivacyPolicySite";
import {CreditsSite} from "../module/Application/pwa/js/site/CreditsSite";
import {SelectWordsSite} from "../module/Application/pwa/js/site/SelectWordsSite";
import {DeleteWordsSite} from "../module/Application/pwa/js/site/DeleteWordsSite";

import './settings'

window.onerror = (e, u, l) => {
    console.error(e, u, l);
};

applyPolyfills();

ThemeManager.addTheme(new Theme('red', 'red'));
ThemeManager.addTheme(new Theme("blue", "blue"));
ThemeManager.addTheme(new Theme("black", "black"));
ThemeManager.addTheme(new Theme("green", "green"));
ThemeManager.addTheme(new Theme("pink", "pink"));
ThemeManager.addTheme(new Theme("dark", "dark"));

ShareManager.addShareButton(new MatomoShareButton(new WhatsappShareButton('img/whatsapp.svg'), "whatsapp", true));
ShareManager.addShareButton(new MatomoShareButton(new SmsShareButton('img/sms.svg'), "sms", true));
ShareManager.addShareButton(new MatomoShareButton(new TelegramShareButton('img/telegram.svg'), "telegram", true));
// ShareManager.addShareButton(new CopyShareButton('img/copy.svg'));

let app = new App();

AndroidBridge.addDefinition(() => {
    window["app"] = app;
    window["app"]["pause"] = app.pause;
    window["app"]["resume"] = app.resume;
    window["app"]["setAppEndListener"] = app.setAppEndListener;
});

SettingsSite.setTemplate("html/application/setting-template.html");
// SettingsSite.shouldAddSettingsAction = false;

RegistrationSite.addAction = false;
LoginSite.addLoginAction = false;

InitPromise.resolve(app).then(async function () {
    SettingsSite.settingsAction.showFor = MenuAction.SHOW_ALWAYS;

    let settingsManager = SettingsManager.getInstance();

    let soundManager = SoundManager.getInstance();
    soundManager.play(SoundManager.CHANNELS.MUSIC, {
        audio: "sound/brightAndBeautifull__.mp3",
        loop: true,
        volume: 0.6,
        muted: (settingsManager.getSetting("play-music", "1") !== "1")
    }).catch(e => console.error(e));

    app.start(MenuSite);
    Translator.setLanguage("de");

    InstallManager.setCanInstallListener(e => {});
    if (InstallManager.isInstalled()){
        MyStorageManager.getInstance().persist();
    }

    window["applyAndroidBridge"] = AndroidBridge.applyDefinitions;
});

// console.log("trying push... 1");
// navigator.serviceWorker["ready"].then(function (registration) {
//     console.log("trying push... 2");
//     registration["pushManager"]["subscribe"]({
//         "userVisibleOnly": true,
//         "applicationServerKey": new Uint8Array([0x4, 0x53, 0xb7, 0x8d, 0xc6, 0xd1, 0x1a, 0xd4, 0x8b, 0xb2, 0xeb, 0x82, 0xf0, 0x9e, 0x12, 0xf6, 0xd, 0x32, 0x18, 0xa, 0x35, 0xf, 0x2d, 0x4c, 0x5, 0x29, 0x15, 0x95, 0x23, 0xb1, 0xd3, 0xab, 0x87, 0x88, 0x85, 0x1d, 0xc0, 0x98, 0x6e, 0x65, 0xe3, 0xcb, 0xa2, 0x28, 0x63, 0x7, 0x34, 0x9b, 0xfa, 0x46, 0x9a, 0x49, 0xcc, 0x70, 0x7a, 0xdd, 0xbe, 0x1e, 0xfc, 0xde, 0xcc, 0xb3, 0x5b, 0xcb, 0xf4])
//     }).then(function (sub) {
//         console.log("trying push... 3");
//         console.log("Subscription", sub);
//     }).catch(function (e) {
//         // if ('Notification' in window && Notification.permission === 'denied') {
//         //     console.warn('Permission for notifications was denied');
//         // } else {
//         //     console.error('Unable to subscribe to push', e);
//         // }
//     });
// });