import { Component, ElementRef, Inject, OnInit, ViewChild } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { IObjectController } from '../interfaces/object-controller';
import { IAction } from '../interfaces/actions/action';
import { NotificationService } from '@shared/ui-api/notification.service';
import { RefreshService } from '@shared/ui-api/refresh.service';
import { CallbackAction } from '../actions/callback-action';
import { of } from 'rxjs';
import { ActionTypes } from '@objects-view/actions/action-types';

@Component({
  selector: 'app-object-details-dialog',
  templateUrl: './object-details-dialog.component.html',
  styleUrls: ['./object-details-dialog.component.scss']
})
export class ObjectDetailsDialogComponent implements OnInit {
  @ViewChild('dialogContent') dialogContent: ElementRef;
  public actionTypes = ActionTypes;
  executingAction = false;
  primaryAction = null;
  constructor(
    public dialogRef: MatDialogRef<ObjectDetailsDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: {
      additionalClasses: string[];
      dialogTitle: string;
      objectController: IObjectController;
      actions: IAction[];
      unlimitedWidth: boolean;
    },
    public notificationService: NotificationService,
    public refreshService: RefreshService,
  ) {
    if (!this.data.additionalClasses) {
      this.data.additionalClasses = [];
    }
    this.data.additionalClasses.push('on-dialog-card');
    if (this.data.actions) {
      this.data.actions = this.setActionCallback(this.data.actions);
    }
  }

  setActionCallback(actions: IAction[]): IAction[] {
    for (const action of actions) {
      if (action instanceof CallbackAction && !action.callback) {
        (action as CallbackAction).callback = (callbackAction) => this.objectActionCallback(callbackAction);
      }
      if (action.primary) {
        this.primaryAction = action;
        this.data.objectController.dialogActionCallback = () => this.primaryActionCallback();
      }
    }

    return actions;
  }

  ngOnInit(): void {
  }

  close() {
    this.dialogRef.close(false);
  }

  primaryActionCallback() {
    return this.objectActionCallback(this.primaryAction);
  }

  objectActionCallback(action: CallbackAction) {
    if (this.data.objectController.actionCallback) {
      this.executingAction = true;
      this.data.objectController.actionCallback(action).subscribe(
        (result) => {
          if (result.success) {
            this.dialogRef.close(true);
          } else {
            this.dialogContent.nativeElement.scroll({
              top: 0,
              left: 0,
              behavior: 'smooth'
            });
          }
          this.executingAction = false;
        }
      );
    }

    return of(null);
  }
}
