import * as vscode from 'vscode';
import { GitChangeType } from '../common';

export class FileTypeDecorationProvider implements vscode.FileDecorationProvider {
	private _disposables: vscode.Disposable[] = [];
	private _gitHubReposListeners: vscode.Disposable[] = [];
	private _pullRequestListeners: vscode.Disposable[] = [];
	private _fileViewedListeners: vscode.Disposable[] = [];

	_onDidChangeFileDecorations: vscode.EventEmitter<vscode.Uri | vscode.Uri[]> = new vscode.EventEmitter<
		vscode.Uri | vscode.Uri[]
	>();

	onDidChangeFileDecorations: vscode.Event<vscode.Uri | vscode.Uri[]> = this._onDidChangeFileDecorations.event;

	constructor() {
		this._disposables.push(vscode.window.registerFileDecorationProvider(this));
	}

	provideFileDecoration(
		uri: vscode.Uri,
		_token: vscode.CancellationToken,
	): vscode.ProviderResult<vscode.FileDecoration> {
		if (!uri.query) {
			return;
		}

		const fileChangeUriParams = JSON.parse(uri.query);
		if (fileChangeUriParams && fileChangeUriParams.status !== undefined) {
			return {
				propagate: false,
				badge: this.letter(fileChangeUriParams.status),
				color: this.color(fileChangeUriParams.status),
				// tooltip: this.tooltip(fileChangeUriParams)
			};
		}

		const mrParams = JSON.parse(uri.query);

		if (mrParams && mrParams.status !== undefined) {
			return {
				propagate: false,
				badge: this.letter(mrParams.status),
				color: this.color(mrParams.status),
				tooltip: mrParams.status,
			};
		}

		return undefined;
	}

	gitColors(status: GitChangeType): string | undefined {
		switch (status) {
			case GitChangeType.MODIFY:
				return 'gitDecoration.modifiedResourceForeground';
			case GitChangeType.ADD:
				return 'gitDecoration.addedResourceForeground';
			case GitChangeType.DELETE:
				return 'gitDecoration.deletedResourceForeground';
			case GitChangeType.RENAME:
				return 'gitDecoration.renamedResourceForeground';
			case GitChangeType.UNKNOWN:
				return undefined;
			// case GitChangeType.UNMERGED:
			// 	return 'gitDecoration.conflictingResourceForeground';
		}
	}

	remoteReposColors(status: GitChangeType): string | undefined {
		switch (status) {
			case GitChangeType.MODIFY:
				return 'remoteHub.decorations.modifiedForegroundColor';
			case GitChangeType.ADD:
				return 'remoteHub.decorations.addedForegroundColor';
			case GitChangeType.DELETE:
				return 'remoteHub.decorations.deletedForegroundColor';
			case GitChangeType.RENAME:
				return 'remoteHub.decorations.incomingRenamedForegroundColor';
			case GitChangeType.UNKNOWN:
				return undefined;
			// case GitChangeType.UNMERGED:
			// 	return 'remoteHub.decorations.conflictForegroundColor';
		}
	}

	color(status: GitChangeType /* viewedState?: ViewedState */): vscode.ThemeColor | undefined {
		let color: string | undefined = vscode.extensions.getExtension('vscode.git')
			? this.gitColors(status)
			: this.remoteReposColors(status);
		return color ? new vscode.ThemeColor(color) : undefined;
	}

	letter(status: GitChangeType /* viewedState?: ViewedState */): string {
		switch (status) {
			case GitChangeType.MODIFY:
				return 'M';
			case GitChangeType.ADD:
				return 'A';
			case GitChangeType.DELETE:
				return 'D';
			case GitChangeType.RENAME:
				return 'R';
			case GitChangeType.UNKNOWN:
				return 'U';
			// case GitChangeType.UNMERGED:
			// 	return 'C';
		}

		return '';
	}

	// tooltip(change: FileChangeNodeUriParams | PRUriParams) {
	// 	if ((change.status === GitChangeType.RENAME) && change.previousFileName) {
	// 		return `Renamed ${change.previousFileName} to ${path.basename(change.fileName)}`;
	// 	}
	// }

	dispose() {
		this._disposables.forEach(dispose => dispose.dispose());
		this._gitHubReposListeners.forEach(dispose => dispose.dispose());
		this._pullRequestListeners.forEach(dispose => dispose.dispose());
		this._fileViewedListeners.forEach(dispose => dispose.dispose());
	}
}
