import { Component, Inject, OnInit } from "@angular/core";
import { MatDialogRef, MAT_DIALOG_DATA } from "@angular/material/dialog";
import { marker } from "@biesbjerg/ngx-translate-extract-marker";
import { AccountService } from "../../server-services/account.service";
import { AuthService } from "../../server-services/auth.service";
import { DialogueService } from "../../server-services/dialogue.service";
import { AccountAvatarRecord } from "../../server-services/query-records/account-records";
import { createContactQuery, getContactListQuery } from "../../server-services/querys";
import { DialogService } from "../../services/dialog.service";
import { RouterHandler } from "../../services/router-handler.service";
import { ServerRestApiService } from "../../services/server-rest-api.service";
import { NanoDialogBase } from "../nano-base-dialog";

@Component({
  selector: "app-contact-dialog",
  templateUrl: "./contact-dialog.component.html",
  styleUrls: ["./contact-dialog.component.scss"],
})
export class ContactDialogComponent extends NanoDialogBase<ContactDialogComponent> implements OnInit {
  
  public user: AccountAvatarRecord;
  public addContactInProgress: boolean = false;
  public addContactDone: boolean = false;
  public isThisMe: boolean = false;
  
  constructor(
    public dialogRef: MatDialogRef<ContactDialogComponent>,
    @Inject(MAT_DIALOG_DATA)
    public data: AccountAvatarRecord,
    private dialogueService: DialogueService,
    private routerHandler: RouterHandler,
    private dialogService: DialogService,
    private serverRestApiService: ServerRestApiService,
    private accountService: AccountService
  ) {
    super(dialogRef);
    this.user = data;
    if (!this.user){
      this.dialogRef.close();
    }

    this.accountService.getMe().then((me)=>{
      this.isThisMe = me.id == this.user.id;
    })

    this.serverRestApiService
      .query({
        query: getContactListQuery,
      })
      .then((contactList: AccountAvatarRecord[]) => {
        this.addContactDone = contactList.includes(this.user);
      });
  }

  ngOnInit(): void {}

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

  public addContact(){
    if (!this.addContactDone && !this.addContactInProgress){
      this.addContactInProgress = true;
      this.serverRestApiService
      .mutate({
        query: createContactQuery,
        variables: {
          peerId: this.user.id,
        },
      })
      .then((res) => {
        this.addContactDone = true;
      })
      .catch(() => {
        this.dialogService.openAlertDialog(
          marker("Error"),
          marker("Error happened during the operation.")
        );
      }).finally(()=>{
        this.addContactInProgress = false;
      })
    }    
  }

  public openChat(){
    this.dialogueService
    .getData(this.user.id)
    .then((dialogueData) => {
      // there is already a dialogue for this user
      this.routerHandler.navigate(["room", this.user.id, "chat", "private"]);
      this.dialogRef.close();
    })
    .catch((err) => {
      // we need to create a new dialogue
      this.dialogueService
        .createDialogue(this.user.id)
        .then((ok) => {
          this.routerHandler.navigate([
            "room",
            this.user.id,
            "chat",
            "private",
          ]);
        })
        .catch((err) => {
          this.dialogService.openAlertDialog(
            marker("Error"),
            marker("Error happened during the operation")
          );
        });
    });
  }
}
