import * as nls from 'vscode-nls';
import { relativeTimeTo } from './date';
import { CodingMergeRequestState, MergeRequestState } from './types';

const localize = nls.loadMessageBundle();

enum MergeRequestStatusSimple {
	CANMERGE = 'canmerge',
	CLOSED = 'closed',
	MERGED = 'merged',
	ABNORMAL = 'abnormal',
	CANNOTMERGE = 'cannotmerge',
}

export const statusIconMap = {
	[MergeRequestStatusSimple.CANMERGE]: '🟢', // 可合并
	[MergeRequestStatusSimple.CANNOTMERGE]: '🔴', // 不可自动合并
	[MergeRequestStatusSimple.MERGED]: '🔵', // 已合并
	[MergeRequestStatusSimple.CLOSED]: '⚪', // 已关闭
	[MergeRequestStatusSimple.ABNORMAL]: '🟡', // 状态异常
};

const getMergeRequestStatus = (mergeRequestState: CodingMergeRequestState): MergeRequestStatusSimple => {
	switch (mergeRequestState) {
		case CodingMergeRequestState.CANMERGE:
			return MergeRequestStatusSimple.CANMERGE;

		case CodingMergeRequestState.CANNOTMERGE:
			return MergeRequestStatusSimple.CANNOTMERGE;

		case CodingMergeRequestState.ACCEPTED:
			return MergeRequestStatusSimple.MERGED;

		case CodingMergeRequestState.ABNORMAL:
			return MergeRequestStatusSimple.ABNORMAL;

		case CodingMergeRequestState.REFUSED:
		case CodingMergeRequestState.CANCEL:
		default:
			return MergeRequestStatusSimple.CLOSED;
	}
};

export const getMergeRequestTreeItemDescription = (mergeRequest: MergeRequestState) => {
	const mergeRequestStatus = getMergeRequestStatus(mergeRequest.status);

	switch (mergeRequestStatus) {
		case MergeRequestStatusSimple.CANMERGE:
			return localize(
				'merge request open description',
				'opened {0} by {1}',
				relativeTimeTo(mergeRequest.createdAt),
				mergeRequest.author.name,
			);

		case MergeRequestStatusSimple.CANNOTMERGE:
			return localize(
				'merge request can not merge description',
				'opened {0} by {1}',
				relativeTimeTo(mergeRequest.createdAt),
				mergeRequest.author.name,
			);

		case MergeRequestStatusSimple.MERGED:
			// FIXME, mergeRequest.author 可能不对
			return localize(
				'merge request merge description',
				'merged by {0} was merged {1}',
				mergeRequest.author.name,
				relativeTimeTo(mergeRequest.updatedAt),
			);

		case MergeRequestStatusSimple.ABNORMAL:
			// FIXME, mergeRequest.author 可能不对
			return localize(
				'merge request abnormal description',
				'opened {0} by {1}',
				mergeRequest.author.name,
				relativeTimeTo(mergeRequest.createdAt),
			);

		case MergeRequestStatusSimple.CLOSED:
		default:
			// FIXME, mergeRequest.author 可能不对
			return localize(
				'merge request close description',
				'closed by {0} was closed {1}',
				mergeRequest.author.name,
				relativeTimeTo(mergeRequest.updatedAt),
			);
	}
};
