import { describeMergeRequestCommits, mergeRequestFileDiff, modifyCloseMR, modifyMergeMR } from '../codingApi';

export class MergeRequestModel {
	static ID = 'MergeRequestModel';
	public mergeId: number;
	public title: string;
	public description: string;
	public createAt: number;
	public updatedAt: number;
	public sshUrl: string;
	public httpsUrl: string;

	public depot: {
		id: number;
		name: string;
	};

	public author: {
		id: number;
		name: string;
	};

	public currentUser: {
		id: number;
		name: string;
	};

	public status: string;
	public conflicts?: string[];
	public base: string;
	public head: string;

	constructor(
		depot: { id: number; name: string },
		mrState: Record<string, any>,
		currentUser: { id: number; name: string },
	) {
		this.mergeId = mrState.mergeId;
		this.base = mrState.base;
		this.head = mrState.head;
		this.title = mrState.title;
		this.description = mrState.description;
		this.status = mrState.status;
		this.conflicts = mrState.conflicts;
		this.updatedAt = mrState.updatedAt;
		this.createAt = mrState.createAt;
		this.httpsUrl = mrState.httpsUrl;
		this.sshUrl = mrState.sshUrl;
		// this.bodyHTML = issue.bodyHTML;
		// this.html_url = issue.url;
		this.author = mrState.author;
		this.depot = { id: depot.id, name: depot.name };
		// this.milestone = issue.milestone;
		this.currentUser = currentUser;
	}

	initializeMergeRequestFileViewState() {}

	async getCommits() {
		const data = await describeMergeRequestCommits({ mergeId: this.mergeId, depotId: this.depot.id });
		return data;
	}

	getCommitChangedFiles(commit: string) {}

	// getFile() {

	// }

	// openDiff() {

	// }

	// getFileChanges() {

	// }

	async getFileChangesInfo() {
		const data = await mergeRequestFileDiff({ mergeId: this.mergeId, depotId: this.depot.id });
		return data;
	}

	getMergeability() {}

	async approve(message: string) {
		const data = await modifyMergeMR({ mergeId: this.mergeId, depotId: this.depot.id, message });

		return data;
	}

	async close() {
		const data = await modifyCloseMR({ mergeId: this.mergeId, depotId: this.depot.id });

		return data;
	}

	/**
	 * Using these contexts is fragile in a multi-root workspace where multiple PRs are checked out.
	 * If you have two active PRs that have the same file path relative to their rootdir, then these context can get confused.
	 */
	public setFileViewedContext() {
		// commands.setContext(contexts.VIEWED_FILES, Array.from(this._viewedFiles));
		// commands.setContext(contexts.UNVIEWED_FILES, Array.from(this._unviewedFiles));
	}

	private clearFileViewedContext() {
		// commands.setContext(contexts.VIEWED_FILES, []);
		// commands.setContext(contexts.UNVIEWED_FILES, []);
	}
}
