import type { Uri } from 'vscode';
import { MarkdownString, ThemeIcon, TreeItem, TreeItemCollapsibleState } from 'vscode';
import { formatDate, fromNow } from '@gitlens/utils/date.js';
import { getAvatarUri } from '../../avatars.js';
import type { GitUri } from '../../git/gitUri.js';
import { formatCurrentUserDisplayName } from '../../git/utils/-webview/commit.utils.js';
import type { Draft } from '../../plus/drafts/models/drafts.js';
import { configuration } from '../../system/-webview/configuration.js';
import type { DraftsView } from '../draftsView.js';
import type { ViewsWithCommits } from '../viewBase.js';
import { ContextValues, getViewNodeId, ViewNode } from './abstract/viewNode.js';

export class DraftNode extends ViewNode<'draft', ViewsWithCommits | DraftsView> {
	constructor(
		uri: GitUri,
		view: ViewsWithCommits | DraftsView,
		protected override parent: ViewNode,
		public readonly draft: Draft,
	) {
		super('draft', uri, view, parent);

		this.updateContext({ draft: draft });
		this._uniqueId = getViewNodeId(this.type, this.context);
	}

	override get id(): string {
		return this._uniqueId;
	}

	override toClipboard(): Promise<string> {
		return this.getUrl();
	}

	override getUrl(): Promise<string> {
		return this.view.container.drafts.generateWebUrl(this.draft.id);
	}

	getChildren(): ViewNode[] {
		return [];
	}

	getTreeItem(): TreeItem {
		const label = this.draft.title ?? `Draft (${this.draft.id})`;
		const item = new TreeItem(label, TreeItemCollapsibleState.None);

		const dateFormat = configuration.get('defaultDateFormat') ?? 'MMMM Do, YYYY h:mma';

		// Only show updated time if it is more than a 30s after the created time
		const showUpdated = this.draft.updatedAt.getTime() - this.draft.createdAt.getTime() >= 30000;

		item.id = this.id;
		let contextValue: string = ContextValues.Draft;
		if (this.draft.isMine) {
			contextValue += '+mine';
		}
		item.contextValue = contextValue;
		item.description = fromNow(this.draft.updatedAt);
		item.command = {
			title: 'Open',
			command: 'gitlens.views.draft.open',
			arguments: [this],
		};

		let avatarUri: Uri | undefined;
		if (this.view.config.avatars && this.draft.author != null) {
			avatarUri = this.draft.author.avatarUri ?? getAvatarUri(this.draft.author.email);
		}

		item.iconPath =
			avatarUri ?? new ThemeIcon(this.draft.type === 'suggested_pr_change' ? 'gitlens-code-suggestion' : 'cloud');

		item.tooltip = new MarkdownString(
			`${label}${this.draft.description ? `\\\n${this.draft.description}` : ''}\n\nCreated ${
				this.draft.author?.name
					? ` by ${this.draft.isMine ? formatCurrentUserDisplayName(this.draft.author.name) : this.draft.author.name}`
					: ''
			} ${fromNow(this.draft.createdAt)} &nbsp; _(${formatDate(this.draft.createdAt, dateFormat)})_${
				showUpdated
					? ` \\\nLast updated ${fromNow(this.draft.updatedAt)} &nbsp; _(${formatDate(
							this.draft.updatedAt,
							dateFormat,
						)})_`
					: ''
			}`,
		);

		return item;
	}
}
