/**
 * ResilientActivityActionsButton WIDGET
 * 
 * Resilient component widget to show dropdown buttons for each activity allowed in the current ActivityDomain's. Allowing for activity execution.
 * 
 * Input's:
 *  - activityDomain: The entity domain instance. Must be a ActivityDomain
 *
 * Event's:
 *  - activityInvoked: Fired when one of the buttons is pressed, that in turn fires server-side activity execution. The event sends a string, with the activity key of the fired activity
 * 
 * Usage
 *  - In the intended *.component.html insert the code:
 *    <resilient-activity-actions-button [activityDomain]="_entity()" (activityInvoked)="_activityInvokedHandler($event)"></resilient-activity-actions-button>
 *  - In the *.component.ts insert the code:
 *    import { ResilientActivityActionsButtonComponent } from 'app/resilient/resilient-activity/actions-button/resilient-activity-actions-button.component';
 *    ...
 *    _entity = input<IActivityDomain | null>(null);
 *    ...
 *    _activityInvokedHandler(message: string): void {
 *      <react to the activity invoked event>
 *    }
 */
 
import { Component, Output, Input, inject, OnInit, EventEmitter  } from '@angular/core';
import { RouterModule } from '@angular/router';
import { HttpResponse } from '@angular/common/http';
import { map, tap, filter } from 'rxjs';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';

import SharedModule from 'app/shared/shared.module';
import { IActivityInfo, IActivityInvoke, IActivityInvokeResponse } from '../resilient-activity.model';
import { ResilientActivityService } from '../service/resilient-activity.service';
import { ResilientActivityActionsButtonDialogComponent, CONFIRM_EVENT } from './dialog/resilient-activity-actions-button-dialog.component';
import { IActivityDomain } from 'app/entities/activity/activity.model';

@Component({
  standalone: true,
  selector: 'resilient-activity-actions-button',
  templateUrl: './resilient-activity-actions-button.component.html',
  imports: [SharedModule, RouterModule, ResilientActivityActionsButtonDialogComponent]
})
export class ResilientActivityActionsButtonComponent implements OnInit {
  @Output() activityInvoked = new EventEmitter<string>();
  
  @Input() activityDomain: IActivityDomain | null = null; // Assumes an Entity instance that extends ActivityDomain
  
  activityCollection: IActivityInfo[] = [];
  
  protected resilientActivityService = inject(ResilientActivityService);
  protected modalService = inject(NgbModal);
  
  ngOnInit(): void {
    if (this.activityDomain && !this.activityDomain.activityProgressKey) {
      this.loadActivitiesForActivityDomain();
    }
  }
  
  previousState(): void {
    window.history.back();
  }

  doActivity(activityKey: string): void {
    const modalRef = this.modalService.open(ResilientActivityActionsButtonDialogComponent, { size: 'lg', backdrop: 'static' });
    // Set modal dialog properties
    modalRef.componentInstance.activityKey = activityKey;
    
    modalRef.closed
      .pipe(
        filter(reason => reason === CONFIRM_EVENT),
        tap(() => this.invokeActivity(activityKey)),
      )
      .subscribe();
  }
  
  protected invokeActivity(activityKey: string): void {
    // Can't invoke activity without a domain id
    if (!this.activityDomain?.id) return;
    // Can't invoke activity without a domain class
    if (!this.activityDomain?.activityDomainClass) return;
    
    const activityInvoke: IActivityInvoke = {
      activityKey: activityKey,
      activityDomainClass: this.activityDomain?.activityDomainClass,
      activityDomainId: this.activityDomain?.id,
    };
    
    this.resilientActivityService
      .doActivity(activityInvoke)
      .subscribe((activityInvokeResponse: IActivityInvokeResponse | null) => {
        this.activityInvoked.emit(activityInvokeResponse?.activityKey); // Emit the event with the message
      });
  }
  
  protected loadActivitiesForActivityDomain(): void {
    // Its a ActivityDomain?
    if (this.activityDomain?.activityDomainClass) {
      // Get ActivityDomain.state
      const activityDomainState = this.activityDomain?.state;
      
      this.resilientActivityService
        .activityInfo(this.activityDomain?.activityDomainClass, activityDomainState)
        .pipe(map((res: HttpResponse<IActivityInfo[]>) => res.body ?? []))
        .subscribe((activities: IActivityInfo[]) => (this.activityCollection = activities));
    }
  }

}
