/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
import { Emitter, Event } from 'vs/base/common/event';
import { splitGlobAware } from 'vs/base/common/glob';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { TestTag } from 'vs/workbench/api/common/extHostTypeConverters';
import { IObservableValue, MutableObservableValue } from 'vs/workbench/contrib/testing/common/observableValue';

export interface ITestExplorerFilterState {
	_serviceBrand: undefined;

	/** Current filter text */
	readonly text: IObservableValue<string>;

	/** Test ID the user wants to reveal in the explorer */
	readonly reveal: MutableObservableValue<string | undefined>;

	/** Event that fires when {@link focusInput} is invoked. */
	readonly onDidRequestInputFocus: Event<void>;

	/**
	 * Glob list to filter for based on the {@link text}
	 */
	readonly globList: readonly { include: boolean; text: string }[];

	/**
	 * The user requested to filter for only the specified tags.
	 */
	readonly onlyTags: ReadonlySet<string>;

	/**
	 * Focuses the filter input in the test explorer view.
	 */
	focusInput(): void;

	/**
	 * Replaces the filter {@link text}.
	 */
	setText(text: string): void;

	/**
	 * Sets whether the {@link text} is filtering for a special term.
	 */
	isFilteringFor(term: TestFilterTerm): boolean;

	/**
	 * Sets whether the {@link text} includes a special filter term.
	 */
	toggleFilteringFor(term: TestFilterTerm, shouldFilter?: boolean): void;
}

export const ITestExplorerFilterState = createDecorator<ITestExplorerFilterState>('testingFilterState');

const tagRe = /@([^ ,:]+)/g;
const trimExtraWhitespace = (str: string) => str.replace(/\s\s+/g, ' ').trim();

export class TestExplorerFilterState implements ITestExplorerFilterState {
	declare _serviceBrand: undefined;
	private readonly focusEmitter = new Emitter<void>();
	/**
	 * Mapping of terms to whether they're included in the text.
	 */
	private termFilterState: { [K in TestFilterTerm]?: true } = {};

	/** @inheritdoc */
	public globList: { include: boolean; text: string }[] = [];

	/** @inheritdoc */
	public onlyTags = new Set<string>();

	/** @inheritdoc */
	public readonly text = new MutableObservableValue('');

	public readonly reveal = new MutableObservableValue</* test ID */string | undefined>(undefined);

	public readonly onDidRequestInputFocus = this.focusEmitter.event;

	/** @inheritdoc */
	public focusInput() {
		this.focusEmitter.fire();
	}

	/** @inheritdoc */
	public setText(text: string) {
		if (text === this.text.value) {
			return;
		}

		this.termFilterState = {};
		this.globList = [];
		this.onlyTags.clear();

		let globText = '';
		let lastIndex = 0;
		for (const match of text.matchAll(tagRe)) {
			let nextIndex = match.index! + match[0].length;

			const tag = match[0];
			if (allTestFilterTerms.includes(tag as TestFilterTerm)) {
				this.termFilterState[tag as TestFilterTerm] = true;
			}

			// recognize and parse @ctrlId:tagId or quoted like @ctrlId:"tag \\"id"
			if (text[nextIndex] === ':') {
				nextIndex++;

				let delimiter = text[nextIndex];
				if (delimiter !== `"` && delimiter !== `'`) {
					delimiter = ' ';
				} else {
					nextIndex++;
				}

				let tagId = '';
				while (nextIndex < text.length && text[nextIndex] !== delimiter) {
					if (text[nextIndex] === '\\') {
						tagId += text[nextIndex + 1];
						nextIndex += 2;
					} else {
						tagId += text[nextIndex];
						nextIndex++;
					}
				}

				this.onlyTags.add(TestTag.namespace(match[1], tagId));
				nextIndex++;
			}

			globText += text.slice(lastIndex, match.index);
			lastIndex = nextIndex;
		}

		globText += text.slice(lastIndex).trim();

		if (globText.length) {
			for (const filter of splitGlobAware(globText, ',').map(s => s.trim()).filter(s => !!s.length)) {
				if (filter.startsWith('!')) {
					this.globList.push({ include: false, text: filter.slice(1).toLowerCase() });
				} else {
					this.globList.push({ include: true, text: filter.toLowerCase() });
				}
			}
		}

		this.text.value = text; // purposely afterwards so everything is updated when the change event happen
	}

	/** @inheritdoc */
	public isFilteringFor(term: TestFilterTerm) {
		return !!this.termFilterState[term];
	}

	/** @inheritdoc */
	public toggleFilteringFor(term: TestFilterTerm, shouldFilter?: boolean) {
		const text = this.text.value.trim();
		if (shouldFilter !== false && !this.termFilterState[term]) {
			this.setText(text ? `${text} ${term}` : term);
		} else if (shouldFilter !== true && this.termFilterState[term]) {
			this.setText(trimExtraWhitespace(text.replace(term, '')));
		}
	}
}

export const enum TestFilterTerm {
	Failed = '@failed',
	Executed = '@executed',
	CurrentDoc = '@doc',
	Hidden = '@hidden',
}

export const allTestFilterTerms: readonly TestFilterTerm[] = [
	TestFilterTerm.Failed,
	TestFilterTerm.Executed,
	TestFilterTerm.CurrentDoc,
	TestFilterTerm.Hidden,
];
