import { hud, navigationShow, navigationHide } from "./components/hud.js";
import { Screens } from "./components/Screens.js";
import { DataProvider } from "./DataProvider.js";
import { FavoritesStore } from "./FavoritesStore.js";
import Toasts from "./components/toasts.js"
import Helper from "./components/helper.js"
import { SmartHomeApi } from "./sh/SmartHomeApi.js";
import { routes } from "./routes.js";
import Modals from "./components/modals.js";
import confirmPopup from "./components/confirm-popup.js";
import advancedSelect from "./components/advanced-select.js";
import editableString from "./components/editable-string.js";

document.addEventListener("DOMContentLoaded", e => {
	console.log("App init");

	if(document.location.hash.indexOf("#!/") == -1) {
		document.location.hash = "#!/";
	}

	window.DataProvider = new DataProvider();
	window.FavoritesStore = FavoritesStore;
	window.Toasts = Toasts;
	window.Helper = Helper;
	window.Modals = Modals;
	window.confirmPopup = confirmPopup;
	window.advancedSelect = advancedSelect;
	window.editableString = editableString;

	hud();
	const sh_api = new SmartHomeApi({
	  base_url: API_BASEURL,
	  token: "YOUR_TOKEN",
	  timeout_ms: 10000,
	  on_unauthorized: ({ error }) => console.log("auth problem:", error),
	  proxy_path: "/proxy.php",
	});

	const screens = new Screens(".screens", ".load-screen", ".error-screen");
	
	routes(screens, sh_api);

	console.log(screens.getScreens());

	screens.onSwitch((scr, alias) => {
		navigationHide();
	});

	screens.onSwitch((scr, alias, route) => {
		document.querySelectorAll(`.app .hud .navigation .nav-link[data-route].active`).forEach(item => item.classList.remove("active"));
		document.querySelector(`.app .hud .navigation .nav-link[data-route="${route}"]`)?.classList.add("active");
	});

	screens.routing();

	window.Screens = screens;
});