import { Injectable } from '@angular/core';
import * as signalR from "@microsoft/signalr";
import { HubConnection } from '@microsoft/signalr';
import { environment } from '../../environments/environment';

@Injectable({
  providedIn: 'root'
})

const WHO_IS_SPY_URL = (id: string = null) => { return `${environment.apiUrl}/WhoIsSpy${(id ? `/${id}` : '')}` }
const WHO_IS_SPY_HUB_URL = (userName: string = null) => { return `${environment.apiUrl}/WhoIsSpyHub?usename=${userName}` }
export class WhoIsSpyService {
  private connection: HubConnection;
  private userName: string;
  constructor() { }

  initGame(userName: string) {
    this.userName = userName;
    this.connection = new signalR.HubConnectionBuilder()
      .withUrl(WHO_IS_SPY_HUB_URL(userName))
      .build();

    this.connection.on("messageReceived", (username: string, message: string) => {
      let m = document.createElement("div");

      // m.innerHTML =
      //     `<div class="message-author">${username}</div><div>${message}</div>`;

      // divMessages.appendChild(m);
      // divMessages.scrollTop = divMessages.scrollHeight;
    });
    this.connection.start().catch(err => document.write(err));
  }

  someoneJoinGame(userName: string) {

  }

  joinGame(gameroomId: string) {
    this.connection.send("test", ['start', 50])
  }
}
