import { Injectable, Input } from '@angular/core';
import {
  DialogCloseResult,
  DialogRef,
  DialogService,
} from '@progress/kendo-angular-dialog';
import { NotificationService } from '@progress/kendo-angular-notification';
import { Subject } from 'rxjs';

@Injectable({
  providedIn: 'root',
})
export class MessageService {
  @Input() title: string;
  @Input() message: string;

  confirmResult: Subject<boolean>;
  public duration = 500;

  constructor(
    private dialogService: DialogService,
    private notificationService: NotificationService
  ) {
    this.confirmResult = new Subject<boolean>();
  }

  showMessage(message: string) {
    this.notificationService.show({
      content: message,
      animation: {
        type: 'slide',
        duration: this.duration,
      },
      position: { horizontal: 'right', vertical: 'bottom' },
      type: { style: 'success' },
    });
  }

  showMessageError(message: string) {
    this.notificationService.show({
      content: message,
      animation: {
        type: 'slide',
        duration: this.duration,
      },
      position: { horizontal: 'right', vertical: 'bottom' },
      type: { style: 'error' },
    });
  }

  public showConfirmation(title: string, message: string) {
    const dialog: DialogRef = this.dialogService.open({
      title,
      content: message,
      actions: [{ text: 'Não' }, { text: 'Sim', primary: true }],
      width: 450,
      height: 200,
      minWidth: 250,
    });

    dialog.result.subscribe((result) => {
      if (result instanceof DialogCloseResult) {
        this.confirmResult.next(false);
      } else {
        console.log('action', result);
        if (result.text === 'Sim') {
          this.confirmResult.next(true);
        } else {
          this.confirmResult.next(false);
        }
      }
    });
  }

  public showInformation(title: string, message: string) {
    const dialog: DialogRef = this.dialogService.open({
      title,
      content: message,
      actions: [{ text: 'Ok', primary: true }],
      width: 450,
      height: 200,
      minWidth: 250,
    });

    dialog.result.subscribe((result) => {
      if (result instanceof DialogCloseResult) {
        this.confirmResult.next(false);
      } else {
        console.log('action', result);
        if (result.text === 'Ok') {
          this.confirmResult.next(true);
        } else {
          this.confirmResult.next(false);
        }
      }
    });
  }
  public showModalError(title: string, message: string) {
    const dialog: DialogRef = this.dialogService.open({
      title,
      content: message,
      actions: [{ text: 'Ok', primary: true }],
      width: 450,
      height: 200,
      minWidth: 250,
    });

    dialog.result.subscribe((result) => {
      if (result instanceof DialogCloseResult) {
        this.confirmResult.next(false);
      } else {
        console.log('action', result);
        if (result.text === 'Ok') {
          this.confirmResult.next(true);
        } else {
          this.confirmResult.next(false);
        }
      }
    });
  }
}
