import { Context } from "@nuxt/types";
import { AbstractRegistry } from "~/src/registry";

/**
 * An item which may appear in the sidebar
 */
export type TaskActionItem = TaskAction|typeof Separator;

/**
 * Represents a seperation divider in the sidebar
 */
export const Separator = Symbol('separator');

/**
 * A task action provides a means of interacting
 * with a task.
 */
export interface TaskAction {
	name: string;
	icon: string;
	warn?: boolean;
	when?: (task: any, view: any) => boolean;
	handle: (task: any, view: any) => void;
}

// TODO: Move each item to its own file (e.g. /registry/taskActions/join.ts)

export class TaskActionRegistry extends AbstractRegistry<TaskActionItem> {

	public constructor(context: Context) {
		super(context);

		this.register('join', {
			name: 'Join task',
			icon: 'mdi-account-plus',
			handle() {
				// TODO Implement
			}
		}, 0);

		this.register('dates', {
			name: 'Start date',
			icon: 'mdi-calendar',
			handle() {
				// TODO Implement
			}
		}, 10);

		this.register('set-parent', {
			name: 'Set parent',
			icon: 'mdi-folder-home',
			when(task) {
				return !task.parent;
			},
			handle() {
				// TODO Implement
			}
		}, 20);

		this.register('remove-parent', {
			name: 'Remove parent',
			icon: 'mdi-folder-remove',
			when(task) {
				return !!task.parent;
			},
			handle() {
				// TODO Implement
			}
		}, 30);

		this.register('add-child', {
			name: 'Add child',
			icon: 'mdi-text-box-plus',
			handle() {
				// TODO Implement
			}
		}, 40);

		this.register('separator-1', Separator, 50);

		// this.register('move', {
		// 	name: 'Move',
		// 	icon: 'mdi-arrow-right-bold-box',
		// 	handle() {
		// 		// TODO Implement
		// 	}
		// }, 60);

		this.register('duplicate', {
			name: 'Duplicate',
			icon: 'mdi-content-duplicate',
			handle() {
				// TODO Implement
			}
		}, 70);

		this.register('follow', {
			name: 'Follow',
			icon: 'mdi-bell',
			handle() {
				// TODO Implement
			}
		}, 80);

		this.register('separator-2', Separator, 90);

		this.register('archive', {
			name: 'Archive',
			icon: 'mdi-archive',
			warn: true,
			handle() {
				// TODO Implement
			}
		}, 100);

		this.register('delete', {
			name: 'Delete',
			icon: 'mdi-delete',
			warn: true,
			when(_, view) {
				return !view.isNewTask;
			},
			handle(task, view) {
				view.$api.query('QueryTaskViewRemove', {
					id: task.id
				});

				view.closeTask();
			}
		}, 110);

	}
	
}