// Written by retoor@molodetz.nl

// This code defines a custom HTML element called ChatWindowElement that provides a chat interface within a shadow DOM, handling connection callbacks, displaying messages, and user interactions.

// No external imports or includes other than standard DOM and HTML elements.

// The MIT License (MIT)
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

class ChatWindowElement extends HTMLElement {
  receivedHistory = false;
  channel = null;
  constructor() {
    super();
    this.attachShadow({ mode: "open" });
    this.component = document.createElement("section");
    this.app = app;
    this.shadowRoot.appendChild(this.component);
  }

  get user() {
    return this.app.user;
  }

  async connectedCallback() {
    const link = document.createElement("link");
    link.rel = "stylesheet";
    link.href = "/base.css";
    this.component.appendChild(link);
    this.component.classList.add("chat-area");

    this.container = document.createElement("section");
    this.container.classList.add("chat-area", "chat-window");

    const chatHeader = document.createElement("div");
    chatHeader.classList.add("chat-header");

    const chatTitle = document.createElement("h2");
    chatTitle.classList.add("chat-title");
    chatTitle.classList.add("no-select");
    chatTitle.innerText = "Loading...";
    chatHeader.appendChild(chatTitle);
    this.container.appendChild(chatHeader);

    const channels = await app.rpc.getChannels();
    const channel = channels[0];
    this.channel = channel;
    chatTitle.innerText = channel.name;

    const channelElement = document.createElement("message-list");
    channelElement.setAttribute("channel", channel.uid);
    this.container.appendChild(channelElement);

    const chatInput = document.createElement("chat-input");
    chatInput.chatWindow = this;
    chatInput.addEventListener("submit", (e) => {
      app.rpc.sendMessage(channel.uid, e.detail);
    });
    this.container.appendChild(chatInput);

    this.component.appendChild(this.container);

    const messages = await app.rpc.getMessages(channel.uid);
    messages.forEach((message) => {
      if (!message["user_nick"]) return;
      channelElement.addMessage(message);
    });

    const me = this;
    channelElement.addEventListener("message", (message) => {
      if (me.user.uid !== message.detail.user_uid) app.playSound(0);

      message.detail.element.scrollIntoView({ block: "end" });
    });
  }
}

customElements.define("chat-window", ChatWindowElement);
