/*
 Copyright (C) 2016 - 2024 3NSoft Inc.
 
 This program is free software: you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free Software
 Foundation, either version 3 of the License, or (at your option) any later
 version.
 
 This program is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 See the GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License along with
 this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { CoreConf } from 'core-3nweb-client-lib';
import { CoreDriver, makeCoreDriver } from '../core';
import { ElectronIPCConnectors, SocketIPCConnectors } from '../core/w3n-connectors';
import { logError } from '../confs';
import { DevToolsAppAllowance } from '../process-args';
import { SystemPlaces } from '../system/apps/installer/system-places';
import { AppDownloader } from '../system/apps/downloader';
import { UserAppInfo } from '../desktop-integration';
import { Subject } from 'rxjs';
import { WrapStartupCAPs, DevAppParams, DevAppParamsGetter, DevSiteParamsGetter } from '../test-stand';
import { Component, Service, AppsByDomain } from '../app-n-components';
import { sleep } from '../lib-common/processes/sleep';
import { NamedProcs } from '../lib-common/processes/named-procs';
import { MAIN_GUI_ENTRYPOINT } from '../lib-common/manifest-utils';
import { LAUNCHER_APP_DOMAIN, STARTUP_APP_DOMAIN } from '../bundle-confs';
import { Sites } from '../site-runner';
import { ScreenGUIPlacements } from '../window-utils/screen-gui-placements';
import { InitProc } from '.';
import { StartupApp } from '../app-n-components/startup-app';
import { defer } from '../lib-common/processes/deferred';
import { GetAppStorage } from '../app-n-components/app';


type UserAppsEvents = 'start-closing' | {
	type: 'closed';
	canClosePlatform: boolean;
} | {
	type: 'app-installed' | 'app-downloaded';
	app: UserAppInfo;
} | {
	type: 'app-removed';
	appId: string;
};

type SysUtils = web3n.system.SysUtils;
type Apps = web3n.system.apps.Apps;
type Platform = web3n.system.platform.Platform;

const platformComponentName = {
	guiPlacement: 'gui.placement'
};


export class UserApps {

	private readonly core: CoreDriver;
	private readonly sysPlaces: SystemPlaces;
	private readonly appDownloader: AppDownloader;

	private startupApp: StartupApp|undefined = undefined;
	private readonly apps: AppsByDomain;

	// XXX sites should get similar treatment to app
	private readonly siteStartingProcs = new NamedProcs();
	private readonly sites: Sites;

	private readonly broadcast = new Subject<UserAppsEvents>();
	readonly event$ = this.broadcast.asObservable();

	constructor(
		makeDriver: typeof makeCoreDriver,
		conf: CoreConf,
		private readonly guiConnectors: ElectronIPCConnectors,
		sockConnectors: SocketIPCConnectors,
		makeTitleGenerator: InitProc['makeTitleGenerator'],
		devApps: DevAppParamsGetter|undefined,
		private readonly devSites: DevSiteParamsGetter|undefined,
		private readonly devToolsAllowance: DevToolsAppAllowance,
		private readonly getPlatform: () => Platform
	) {
		this.sysPlaces = new SystemPlaces(() => this.core.storages);
		this.appDownloader = new AppDownloader(this.sysPlaces);
		const makeSystemCapFns = () => systemCAPsFrom(
			this.sysPlaces,
			this.appDownloader,
			this.openApp.bind(this),
			this.apps.closeAppToUninstall.bind(this.apps),
			this.getPlatform()
		);
		this.core = makeDriver(
			conf, makeSystemCapFns,
			this.startAppWithCmd.bind(this),
			this.logout.bind(this),
			this.getServiceToHandleCall.bind(this),
			this.getAppFSResourceFor.bind(this)
		);
		const guiPlacementFS = this.core.whenReady()
		.then(() => this.sysPlaces.getPlatformComponentFS(
			'local', platformComponentName.guiPlacement
		));
		this.apps = new AppsByDomain(
			this.sysPlaces.findInstalledApp.bind(this.sysPlaces),
			this.core.makeCAPsForAppComponent.bind(this.core),
			this.getAppStorage.bind(this),
			this.guiConnectors, sockConnectors,
			makeTitleGenerator(() => this.userId),
			new ScreenGUIPlacements(guiPlacementFS),
			devApps
		);
		this.sites = new Sites(
			this.guiConnectors,
			makeTitleGenerator(() => this.userId),
			this.core.makeCAPsForSiteComponent.bind(this.core)
		);
		Object.seal(this);
	}

	get userId(): string {
		return this.core.getUserId();
	}

	get coreStarted(): boolean {
		return this.core.isStarted();
	}

	async listInstalled(): Promise<UserAppInfo[]> {
		const infos: UserAppInfo[] = [];
		const allApps = await this.sysPlaces.listApps();
		for (const { id, current } of allApps) {
			infos.push({
				id, name: id, isInstalled: !!current
			});
		}
		return infos;
	}

	updateWindowTitles(): void {
		this.apps.updateWindowTitles();
	}

	startCoreDirectly(): ReturnType<CoreDriver['start']> {
		return this.core.start();
	}

	openStartupApp(
		usersToFilterOut: string[]
	): Promise<{ init: Promise<boolean>; }> {
		return this.openRegularOrDevelopmentStartupApp(
			() => StartupApp.instantiate(
				usersToFilterOut, this.devToolsAllowance(STARTUP_APP_DOMAIN),
				() => this.core.start(),
				this.guiConnectors.connectStartupW3N.bind(this.guiConnectors)
			)
		);
	}

	openDevStartupApp(
		usersToFilterOut: string[],
		devParams: DevAppParams, wrapCAP: WrapStartupCAPs
	): ReturnType<UserApps['openStartupApp']> {
		return this.openRegularOrDevelopmentStartupApp(
			() => StartupApp.instantiateDev(
				usersToFilterOut, devParams,
				() => this.core.start(), wrapCAP,
				this.guiConnectors.connectStartupW3N.bind(this.guiConnectors)
			)
		);
	}

	private async openRegularOrDevelopmentStartupApp(
		instantiate: () => ReturnType<typeof StartupApp.instantiate>
	): ReturnType<UserApps['openStartupApp']> {
		if (this.core.isStarted() || this.startupApp) {
			throw new Error(`Startup was already started`);
		}
		const { coreInit, startProc, startupApp } = instantiate();
		this.startupApp = startupApp;
		await startProc;
		const windowClosed = defer<false>();
		this.startupApp.doWhenWindowCompletes(() => windowClosed.resolve(false));
		const init = Promise.race([
			coreInit.then(() => true),
			windowClosed.promise
		]);
		return { init };
	}

	closeStartupApp(): void {
		if (this.startupApp) {
			this.startupApp.close();
			this.startupApp = undefined;
		}
	}

	focusStartupWindow(): void {
		if (this.startupApp) {
			this.startupApp.focusWindow();
		}
	}

	async openApp(appDomain: string): Promise<void> {
		const app = await this.apps.get(
			appDomain, this.devToolsAllowance(appDomain)
		);
		// XXX replace launchDefault with launch(component?, cmd?)
		app.launchDefault();
	}

	async openAppLauncher(): Promise<void> {
		await this.openApp(LAUNCHER_APP_DOMAIN);
	}

	async doUserSystemStartup(): Promise<void> {
		await this.openApp(LAUNCHER_APP_DOMAIN);
		// XXX need a more dynamic choice of what should be loaded on startup 
		const chatAppDomain = 'chat.app.privacysafe.io';
		const contactsAppDomain = 'contacts.app.privacysafe.io';
		[ chatAppDomain, contactsAppDomain ]
		.map(domain =>
			this.apps.get(domain, this.devToolsAllowance(domain))
			.then(chat => chat.loadOnStartup())
			.catch(err => console.error(err))
		);

	}

	private async startAppWithCmd(
		callerApp: string, callerComponent: string,
		appDomain: string, cmd: string, ...params: any[]
	): Promise<void> {
		const app = await this.apps.get(
			appDomain, this.devToolsAllowance(appDomain)
		);
		await app.handleCmd({ cmd, params }, callerApp, callerComponent);
	}

	async openSite(siteDomain: string): Promise<void> {
		const entrypoint = MAIN_GUI_ENTRYPOINT;
		if (this.sites.focusOnSiteComponentIfOpened(siteDomain, entrypoint)) {
			return;
		}

		let startedProc = this.siteStartingProcs.getP<void>(siteDomain);
		if (startedProc) {
			return startedProc;
		}

		const devParams = (this.devSites ?
			this.devSites(siteDomain, entrypoint) : undefined
		);
		startedProc = (devParams ?
			this.sites.devOpenSiteComponent(devParams, entrypoint) :
			this.sites.openSiteComponent(siteDomain, entrypoint)
		);
		return this.siteStartingProcs.addStarted(siteDomain, startedProc);
	}

	private async getServiceToHandleCall(
		caller: Component, appDomain: string, service: string
	): Promise<Service> {
		const app = await this.apps.get(
			appDomain, this.devToolsAllowance(appDomain)
		);
		return await app.getServiceToHandleCall(caller, service);
	}

	async closeAllApps(): Promise<void> {
		await this.apps.stopAndCloseAllApps();
	}

	async exit(closePlatform = false): Promise<void> {
		this.broadcast.next('start-closing');
		await this.closeAllApps();
		await this.core.close().catch(logError);
		this.broadcast.next({
			type: 'closed', canClosePlatform: closePlatform
		});
		await sleep(1);
		this.broadcast.complete();
	}

	private async logout(closePlatform: boolean): Promise<void> {
		this.exit(closePlatform);	// we don't wait for this to end
	};

	private async getAppFSResourceFor(
		resourceAppDomain: string|null|undefined, resourceName: string,
		requestingApp: string, requestingComponent: string
	): ReturnType<web3n.shell.GetFSResource> {
		if (!resourceAppDomain) {
			resourceAppDomain = requestingApp;
		}
		const app = await this.apps.get(
			resourceAppDomain, this.devToolsAllowance(resourceAppDomain)
		);
		return await app.exposedFSResource(
			resourceName, requestingApp, requestingComponent
		);
	}

	private getAppStorage(appDomain: string): GetAppStorage {
		return type => {
			if (type === 'local') {
				return this.core.storages.makeLocalFSForApp(
					reverseDomain(appDomain)
				);
			} else if (type === 'synced') {
				return this.core.storages.makeSyncedFSForApp(
					reverseDomain(appDomain)
				);
			} else {
				throw new Error(`Unknown storage type ${type}`);
			}
		};
	}

}
Object.freeze(UserApps.prototype);
Object.freeze(UserApps);


function reverseDomain(domain: string): string {
	return domain.split('.').reverse().join('.');
}

function systemCAPsFrom(
	sysPlaces: SystemPlaces, appDownloader: AppDownloader,
	launch: UserApps['openApp'],
	closeAppToUninstall: AppsByDomain['closeAppToUninstall'],
	platform: Platform
): SysUtils {
	const apps: Apps = {
		opener: {
			listApps: sysPlaces.listApps.bind(sysPlaces),
			openApp: launch,
			getAppVersions: sysPlaces.getAppVersions.bind(sysPlaces),
			getAppManifest: sysPlaces.getAppManifest.bind(sysPlaces),
			getAppFileBytes: sysPlaces.getAppFileBytes.bind(
				sysPlaces
			),
			watchApps: sysPlaces.watchApps.bind(sysPlaces)
		},
		downloader: {
			downloadWebApp: appDownloader.downloadWebApp.bind(
				appDownloader
			),
			getAppChannels: appDownloader.getAppChannels.bind(
				appDownloader
			),
			getLatestAppVersion: appDownloader.getLatestAppVersion.bind(
				appDownloader
			),
			getAppVersionFilesList: appDownloader.getAppVersionFilesList.bind(
				appDownloader
			)
		},
		installer: {
			unpackBundledApp: sysPlaces.unpackBundledApp.bind(
				sysPlaces
			),
			installApp: sysPlaces.installApp.bind(sysPlaces),
			uninstallApp: async id => {
				await closeAppToUninstall(id);
				await sysPlaces.uninstallApp(id);
			},
			removeAppPack: sysPlaces.removeAppPack.bind(sysPlaces)
		}
	};
	return { apps, platform };
}


Object.freeze(exports);