import { IUseCase } from './base/IUseCase';
import DialogsRepository from '../../Data/repository/DialogsRepository';
import EventMessagesRepository from '../../Data/repository/EventMessagesRepository';
import { DialogEntity } from '../entity/DialogEntity';
import { RemoteMessageDTO } from '../../Data/dto/message/RemoteMessageDTO';
import { DialogType } from '../entity/DialogTypes';
import { GroupDialogEntity } from '../entity/GroupDialogEntity';
import { PrivateDialogEntity } from '../entity/PrivateDialogEntity';
import { NotificationTypes } from '../entity/NotificationTypes';
import EventMessageType from '../entity/EventMessageType';
import { stringifyError } from '../../utils/parse';

export class UnJoinFromTheDialogUseCase
  implements IUseCase<void, boolean>
{
  private dialogRepository: DialogsRepository;

  private eventMessagesRepository: EventMessagesRepository;

  private dialogToLeave: DialogEntity;

  constructor(
    eventMessagesRepository: EventMessagesRepository,
    dialogRepository: DialogsRepository,
    dialog: DialogEntity,
  ) {
    this.eventMessagesRepository = eventMessagesRepository;
    this.dialogRepository = dialogRepository;
    this.dialogToLeave = dialog;
  }

  async execute(): Promise<boolean> {
    console.log('execute UnJoinFromTheDialogUseCase');
    let resultOperation = false;

    const remoteMessageDTO: RemoteMessageDTO = new RemoteMessageDTO();

    remoteMessageDTO.dialogId = this.dialogToLeave.id;
    remoteMessageDTO.notification_type = NotificationTypes.DELETE_LEAVE_DIALOG;
    remoteMessageDTO.date_sent = Date.now();

    await this.dialogRepository
      .unjoinUsersFromDialog(this.dialogToLeave.id)
      .then(() => {
        resultOperation = true;
        console.log('Leave from Dialog successes');

        return resultOperation;
      })
      .catch((reason) => {
        console.log(
          'have exception in leave Dialog : ',
          stringifyError(reason),
        );
        resultOperation = false;
      });


    return resultOperation;
  }
}
