import { Component, OnInit } from '@angular/core';
import { MatDialogRef } from '@angular/material/dialog';
import {
  InsertPredefinedReplyDialogComponent
} from '@shared/plugins/common-dialogs/tickets/insert-predefined-reply-dialog/insert-predefined-reply-dialog.component';
import { IMessageTemplateModel } from '@fleio-api/message-templates/model/message-template.model';
import { MessageTemplatesApiService } from '@fleio-api/message-templates/message-templates-api.service';
import { ConfigService } from '@shared/config/config.service';
import { IBaseDialog } from '@shared/common-dialogs/base-dialog';
import { IAction } from '@objects-view/interfaces/actions/action';

@Component({
  selector: 'app-insert-mass-message-template-dialog',
  templateUrl: './insert-mass-message-template-dialog.component.html',
  styleUrls: ['./insert-mass-message-template-dialog.component.scss']
})
export class InsertMassMessageTemplateDialogComponent implements IBaseDialog, OnInit {
  loading = false;
  messageTemplates: Array<IMessageTemplateModel>;
  dialogTitle = $localize`Insert mass message template`;
  dialogActions: Array<IAction> = [];

  constructor(
    public dialogRef: MatDialogRef<InsertPredefinedReplyDialogComponent>,
    private messageTemplatesApiService: MessageTemplatesApiService,
    public config: ConfigService,
  ) { }

  insert(template: IMessageTemplateModel) {
    this.dialogRef.close(template);
  }

  ngOnInit(): void {
    this.loading = true;
    this.messageTemplatesApiService.list(
      {
        page_size: 10000,
        ordering: 'name',
        filtering: 'category__name:mass_message'
      }
    ).subscribe(response => {
      this.messageTemplates = response.objects;
    }).add(() => {
      this.loading = false;
    });
  }

}
