import * as vscode from 'vscode';
import { MergeRequestModel } from '../../coding/mergeRequestModel';
import { FileChangeNode } from '../nodes/fileChangeNode';
import { DiffGroupNode } from './diffGroupNode';
import { TreeNode } from './treeNode';

export class MergeRequestNode extends TreeNode implements vscode.TreeItem {
	constructor(private _mergeRequestModel: MergeRequestModel) {
		super();
	}

	async getChildren(): Promise<TreeNode[]> {
		const { mergeId } = this._mergeRequestModel;

		return this._mergeRequestModel.getFileChangesInfo().then(async ({ MergeRequestFileDiff }) => {
			const baseCommit = MergeRequestFileDiff.OldSha;
			const headCommit = MergeRequestFileDiff.NewSha;

			const diffNodes = MergeRequestFileDiff?.FileDiffs.map(item => {
				// const activeTextEditor = vscode.window.activeTextEditor;
				// const opts: vscode.TextDocumentShowOptions = {
				// 	preserveFocus: true,
				// 	viewColumn: vscode.ViewColumn.Active,
				// };

				// Check if active text editor has same path as other editor. we cannot compare via
				// URI.toString() here because the schemas can be different. Instead we just go by path.
				// if (activeTextEditor && activeTextEditor.document.uri.path === uri.path) {
				// 	opts.selection = activeTextEditor.selection;
				// }

				const treeItem = new FileChangeNode(item.Path, '', item.ChangeType, baseCommit, headCommit, mergeId);
				return treeItem;
			});

			// const descriptionNode = new DescriptionNode('Description', new vscode.ThemeIcon('globe'), this._mergeRequestModel);
			const dirNode = new DiffGroupNode(this, '文件');
			dirNode.addFiles(diffNodes);

			return [dirNode];
		});
	}

	getTreeItem(): vscode.TreeItem {
		return this;
	}
}
