import { Component, Input, OnDestroy } from "@angular/core";
import { ActionItem } from "@vcd/ui-components";
import { EntityActionExtensionMenuItemClickEvent } from "../../../app/framework/common/extensibility/entity-action/entity-action-extension-menu.component";
import { EntityActionExtensionService } from "../../../app/framework/common/extensibility/entity-action/entity-action-extension.service";
import { VM_ACTION_COMPONENT_SCOPE } from "../../../app/framework/common/extensibility/entity-action/entity-action.extension-point";
import { EntityActionComponentContainer } from "../../../app/framework/common/extensibility/extension-components.directive";
import { ExtensibleComponent } from "../../framework/extensions/compute/extensible-compute.component";
import { SubscriptionTracker } from "../../../app/subscription-tracker.service";

@Component({
    selector: "vm-actions",
    templateUrl: "./vm-actions.component.html",
    providers: [
        SubscriptionTracker
    ]
})
export class VmActionsComponent extends ExtensibleComponent implements OnDestroy {
    extensionActionItems: ActionItem<unknown, unknown>[] = [];

    /**
     * Load extension defined entity actions
     */
    @Input() extensionComponents: string;

    /**
     * {@link ExtensibleComponent.entityActionExtensionComponents}
     * List of wrapper objects containing the logic to fetch extension action item, perform an action when the action
     * item is clicked and also the set of translations required for displaying the extension action text content
     */
    override set entityActionExtensionComponents(entityActionExtensionComponents: EntityActionComponentContainer[]) {
        super.entityActionExtensionComponents = entityActionExtensionComponents;
        this.prepareAndUpdateExtensionActionItems();
    }

    override get entityActionExtensionComponents(): EntityActionComponentContainer[] {
        return super.entityActionExtensionComponents;
    }

    private _entityUrn: string = "vm-1";

    constructor
    (
        private entityActionExtensionService: EntityActionExtensionService,
        private subscriptionTracker: SubscriptionTracker,
    ) {
        super();
        this.prepareAndUpdateExtensionActionItems();
        this.extensionComponents = VM_ACTION_COMPONENT_SCOPE;

        this.subscriptionTracker.subscribe(
            this.entityActionExtensionService.menuItemClick,
            (event: EntityActionExtensionMenuItemClickEvent) => this.performExtensionEntityAction(event)
        );
    }

    ngOnDestroy(): void {
        console.log(`[${this.constructor.name}] has been destroyed.`);
    }

    private prepareAndUpdateExtensionActionItems(): void {
        this.subscriptionTracker.subscribe(
            this.entityActionExtensionService.getExtensionActionItems(
                this._entityUrn,
                this.entityActionExtensionComponents
            ),
            (actionItems) => {
                this.extensionActionItems = actionItems;
            }
        );
    }

    protected override refresh() {
        console.log("Refresh");
    }

    protected override handleError(errMsg: string) {
        console.log("handleError", errMsg);
    }
}