/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import { RunOnceScheduler } from 'vs/base/common/async';
import Event, { Emitter } from 'vs/base/common/event';
import { KeyCode } from 'vs/base/common/keyCodes';
import { Disposable } from 'vs/base/common/lifecycle';
import { IClipboardEvent, ICompositionEvent, IKeyboardEventWrapper, ISimpleModel, ITextAreaWrapper, ITypeData, TextAreaState, TextAreaStrategy, createTextAreaState } from 'vs/editor/common/controller/textAreaState';
import { Range } from 'vs/editor/common/core/range';
import { EndOfLinePreference } from 'vs/editor/common/editorCommon';

const enum ReadFromTextArea {
	Type,
	Paste
}

export interface IBrowser {
	isIPad: boolean;
	isChrome: boolean;
	isEdgeOrIE: boolean;
	isFirefox: boolean;
	enableEmptySelectionClipboard: boolean;
}

export interface IPasteData {
	text: string;
	pasteOnNewLine: boolean;
}

export interface ICompositionStartData {
	showAtLineNumber: number;
	showAtColumn: number;
}

export class TextAreaHandler extends Disposable {

	private _onKeyDown = this._register(new Emitter<IKeyboardEventWrapper>());
	public onKeyDown: Event<IKeyboardEventWrapper> = this._onKeyDown.event;

	private _onKeyUp = this._register(new Emitter<IKeyboardEventWrapper>());
	public onKeyUp: Event<IKeyboardEventWrapper> = this._onKeyUp.event;

	private _onCut = this._register(new Emitter<void>());
	public onCut: Event<void> = this._onCut.event;

	private _onPaste = this._register(new Emitter<IPasteData>());
	public onPaste: Event<IPasteData> = this._onPaste.event;

	private _onType = this._register(new Emitter<ITypeData>());
	public onType: Event<ITypeData> = this._onType.event;

	private _onCompositionStart = this._register(new Emitter<ICompositionStartData>());
	public onCompositionStart: Event<ICompositionStartData> = this._onCompositionStart.event;

	private _onCompositionUpdate = this._register(new Emitter<ICompositionEvent>());
	public onCompositionUpdate: Event<ICompositionEvent> = this._onCompositionUpdate.event;

	private _onCompositionEnd = this._register(new Emitter<ICompositionEvent>());
	public onCompositionEnd: Event<ICompositionEvent> = this._onCompositionEnd.event;

	private Browser: IBrowser;
	private textArea: ITextAreaWrapper;
	private model: ISimpleModel;
	private flushAnyAccumulatedEvents: () => void;

	private selection: Range;
	private selections: Range[];
	private hasFocus: boolean;

	private asyncTriggerCut: RunOnceScheduler;

	private lastCompositionEndTime: number;

	private textAreaState: TextAreaState;
	private textareaIsShownAtCursor: boolean;

	private lastCopiedValue: string;
	private lastCopiedValueIsFromEmptySelection: boolean;

	private _nextCommand: ReadFromTextArea;

	constructor(Browser: IBrowser, strategy: TextAreaStrategy, textArea: ITextAreaWrapper, model: ISimpleModel, flushAnyAccumulatedEvents: () => void) {
		super();
		this.Browser = Browser;
		this.textArea = textArea;
		this.model = model;
		this.flushAnyAccumulatedEvents = flushAnyAccumulatedEvents;
		this.selection = new Range(1, 1, 1, 1);
		this.selections = [new Range(1, 1, 1, 1)];
		this._nextCommand = ReadFromTextArea.Type;

		this.asyncTriggerCut = new RunOnceScheduler(() => this._onCut.fire(), 0);

		this.lastCopiedValue = null;
		this.lastCopiedValueIsFromEmptySelection = false;
		this.textAreaState = createTextAreaState(strategy);

		this.hasFocus = false;

		this.lastCompositionEndTime = 0;

		this._register(this.textArea.onKeyDown((e) => this._onKeyDownHandler(e)));
		this._register(this.textArea.onKeyUp((e) => this._onKeyUp.fire(e)));
		this._register(this.textArea.onKeyPress((e) => this._onKeyPressHandler(e)));

		this.textareaIsShownAtCursor = false;

		this._register(this.textArea.onCompositionStart((e) => {

			if (this.textareaIsShownAtCursor) {
				return;
			}

			this.textareaIsShownAtCursor = true;

			// In IE we cannot set .value when handling 'compositionstart' because the entire composition will get canceled.
			if (!this.Browser.isEdgeOrIE) {
				this.setTextAreaState('compositionstart', this.textAreaState.toEmpty());
			}

			this._onCompositionStart.fire({
				showAtLineNumber: this.selection.startLineNumber,
				showAtColumn: this.selection.startColumn
			});
		}));

		this._register(this.textArea.onCompositionUpdate((e) => {
			this.textAreaState = this.textAreaState.fromText(e.data);
			let typeInput = this.textAreaState.updateComposition();
			this._onType.fire(typeInput);
			this._onCompositionUpdate.fire(e);
		}));

		let readFromTextArea = () => {
			this.textAreaState = this.textAreaState.fromTextArea(this.textArea);
			let typeInput = this.textAreaState.deduceInput();
			// console.log('==> DEDUCED INPUT: ' + JSON.stringify(typeInput));
			if (this._nextCommand === ReadFromTextArea.Type) {
				if (typeInput.text !== '') {
					this._onType.fire(typeInput);
				}
			} else {
				this.executePaste(typeInput.text);
				this._nextCommand = ReadFromTextArea.Type;
			}
		};

		this._register(this.textArea.onCompositionEnd((e) => {
			// console.log('onCompositionEnd: ' + e.data);
			this.textAreaState = this.textAreaState.fromText(e.data);
			let typeInput = this.textAreaState.updateComposition();
			this._onType.fire(typeInput);

			// Due to isEdgeOrIE (where the textarea was not cleared initially)
			// we cannot assume the text at the end consists only of the composited text
			if (Browser.isEdgeOrIE) {
				// In Chrome v49, the text at the time of the compositionend event is not really the final text
				// for the mac dead key input method.
				// N.B: This can be removed in Chrome v53
				this.textAreaState = this.textAreaState.fromTextArea(this.textArea);
			}

			this.lastCompositionEndTime = (new Date()).getTime();
			if (!this.textareaIsShownAtCursor) {
				return;
			}
			this.textareaIsShownAtCursor = false;

			this._onCompositionEnd.fire();
		}));

		this._register(this.textArea.onInput(() => {
			// console.log('onInput: ' + this.textArea.getValue());
			if (this.textareaIsShownAtCursor) {
				// console.log('::ignoring input event because the textarea is shown at cursor: ' + this.textArea.getValue());
				return;
			}

			readFromTextArea();
		}));

		// --- Clipboard operations

		this._register(this.textArea.onCut((e) => {
			// Ensure we have the latest selection => ask all pending events to be sent
			this.flushAnyAccumulatedEvents();
			this._ensureClipboardGetsEditorSelection(e);
			this.asyncTriggerCut.schedule();
		}));

		this._register(this.textArea.onCopy((e) => {
			// Ensure we have the latest selection => ask all pending events to be sent
			this.flushAnyAccumulatedEvents();
			this._ensureClipboardGetsEditorSelection(e);
		}));

		this._register(this.textArea.onPaste((e) => {
			if (e.canUseTextData()) {
				this.executePaste(e.getTextData());
			} else {
				if (this.textArea.getSelectionStart() !== this.textArea.getSelectionEnd()) {
					// Clean up the textarea, to get a clean paste
					this.setTextAreaState('paste', this.textAreaState.toEmpty());
				}
				this._nextCommand = ReadFromTextArea.Paste;
			}
		}));

		this._writePlaceholderAndSelectTextArea('ctor');
	}

	public dispose(): void {
		this.asyncTriggerCut.dispose();
		super.dispose();
	}

	// --- begin event handlers

	public setStrategy(strategy: TextAreaStrategy): void {
		this.textAreaState = this.textAreaState.toStrategy(strategy);
	}

	public setHasFocus(isFocused: boolean): void {
		if (this.hasFocus === isFocused) {
			// no change
			return;
		}
		this.hasFocus = isFocused;
		if (this.hasFocus) {
			this._writePlaceholderAndSelectTextArea('focusgain');
		}
	}

	public setCursorSelections(primary: Range, secondary: Range[]): void {
		this.selection = primary;
		this.selections = [primary].concat(secondary);
		this._writePlaceholderAndSelectTextArea('selection changed');
	}

	// --- end event handlers

	private setTextAreaState(reason: string, textAreaState: TextAreaState): void {
		if (!this.hasFocus) {
			textAreaState = textAreaState.resetSelection();
		}

		textAreaState.applyToTextArea(reason, this.textArea, this.hasFocus);
		this.textAreaState = textAreaState;
	}

	private _onKeyDownHandler(e: IKeyboardEventWrapper): void {
		if (e.equals(KeyCode.Escape)) {
			// Prevent default always for `Esc`, otherwise it will generate a keypress
			// See https://msdn.microsoft.com/en-us/library/ie/ms536939(v=vs.85).aspx
			e.preventDefault();
		}
		this._onKeyDown.fire(e);
	}

	private _onKeyPressHandler(e: IKeyboardEventWrapper): void {
		if (!this.hasFocus) {
			// Sometimes, when doing Alt-Tab, in FF, a 'keypress' is sent before a 'focus'
			return;
		}
	}

	// ------------- Operations that are always executed asynchronously

	private executePaste(txt: string): void {
		if (txt === '') {
			return;
		}

		let pasteOnNewLine = false;
		if (this.Browser.enableEmptySelectionClipboard) {
			pasteOnNewLine = (txt === this.lastCopiedValue && this.lastCopiedValueIsFromEmptySelection);
		}
		this._onPaste.fire({
			text: txt,
			pasteOnNewLine: pasteOnNewLine
		});
	}

	public writePlaceholderAndSelectTextAreaSync(): void {
		this._writePlaceholderAndSelectTextArea('focusTextArea');
	}

	private _writePlaceholderAndSelectTextArea(reason: string): void {
		if (!this.textareaIsShownAtCursor) {
			// Do not write to the textarea if it is visible.
			if (this.Browser.isIPad) {
				// Do not place anything in the textarea for the iPad
				this.setTextAreaState(reason, this.textAreaState.toEmpty());
			} else {
				this.setTextAreaState(reason, this.textAreaState.fromEditorSelection(this.model, this.selection));
			}
		}
	}

	// ------------- Clipboard operations

	private _ensureClipboardGetsEditorSelection(e: IClipboardEvent): void {
		let whatToCopy = this._getPlainTextToCopy();
		if (e.canUseTextData()) {
			e.setTextData(whatToCopy);
		} else {
			this.setTextAreaState('copy or cut', this.textAreaState.fromText(whatToCopy));
		}

		if (this.Browser.enableEmptySelectionClipboard) {
			if (this.Browser.isFirefox) {
				// When writing "LINE\r\n" to the clipboard and then pasting,
				// Firefox pastes "LINE\n", so let's work around this quirk
				this.lastCopiedValue = whatToCopy.replace(/\r\n/g, '\n');
			} else {
				this.lastCopiedValue = whatToCopy;
			}

			let selections = this.selections;
			this.lastCopiedValueIsFromEmptySelection = (selections.length === 1 && selections[0].isEmpty());
		}
	}

	private _getPlainTextToCopy(): string {
		let newLineCharacter = this.model.getEOL();
		let selections = this.selections;

		if (selections.length === 1) {
			let range: Range = selections[0];
			if (range.isEmpty()) {
				if (this.Browser.enableEmptySelectionClipboard) {
					let modelLineNumber = this.model.convertViewPositionToModelPosition(range.startLineNumber, 1).lineNumber;
					return this.model.getModelLineContent(modelLineNumber) + newLineCharacter;
				} else {
					return '';
				}
			}

			return this.model.getValueInRange(range, EndOfLinePreference.TextDefined);
		} else {
			selections = selections.slice(0).sort(Range.compareRangesUsingStarts);
			let result: string[] = [];
			for (let i = 0; i < selections.length; i++) {
				result.push(this.model.getValueInRange(selections[i], EndOfLinePreference.TextDefined));
			}

			return result.join(newLineCharacter);
		}
	}
}
