/**
 *  Copyright © 2020-2026, Luis Andrés Lange <https://javacomm.net>
 *
 *  This Source Code Form is subject to the terms of the Mozilla Public
 *  License, v. 2.0. If a copy of the MPL was not distributed with this
 *  file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 *  ----------------------------------------------------------------------------
 *
 *  Exhibit B - "Incompatible With Secondary Licenses" Notice
 *
 *  This Source Code Form is "Incompatible With Secondary Licenses",
 *  as defined by the Mozilla Public License, v. 2.0.
 *
 *  In short:
 *  - This file may be used, modified, and distributed under MPL 2.0 only.
 *  - It may NOT be relicensed under GPL, LGPL, AGPL, or any other Secondary License.
 *
 *  Rationale:
 *  - Ensures that the code remains MPL-2.0.
 *  - Avoids legal conflicts with GPL-licensed libraries (e.g., VideoLAN).
 *  - Maximizes usability for commercial and security-critical applications.
 *
 */
package net.javacomm.client.chat;

import java.awt.BorderLayout;
import java.awt.Color;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import net.javacomm.multilingual.schema.ISO639;
import net.javacomm.multilingual.schema.KEY;
import net.javacomm.protocol.UserOnline;
import net.javacomm.window.manager.Control;
import net.javacomm.window.manager.WindowAnrufenEvent;
import net.javacomm.window.manager.WindowAnrufenListener;
import net.javacomm.window.manager.WindowManagerInternalFrame;



public class JPrivateChatFrame extends WindowManagerInternalFrame implements WindowAnrufenListener {

  private static final long serialVersionUID = 1791664713483858966L;

  public final static int WIDTH = 330;
  public final static int HEIGHT = 420;
  private JPanel mainPanel = new JPanel(new BorderLayout());
  private FrameAction frameAction = new FrameAction();
  private PropertyChangeSupport changes = new PropertyChangeSupport(this);
  private JPrivateChatPanel privatchatPanel = new JPrivateChatPanel();

  public JPrivateChatFrame() {
    addInternalFrameListener(frameAction);
    setContentPane(mainPanel);
    mainPanel.add(BorderLayout.CENTER, privatchatPanel);

    privatchatPanel.addPrivatechatListener(event -> {
      // HIT, CLOSE und Nickname wird nach oben weitergeleitet
      changes.firePropertyChange(event);
    });

  }



  public void addPersonListener(PropertyChangeListener l) {
    changes.addPropertyChangeListener(l);
  }



  public void removeAllListener() {
    for (PropertyChangeListener l : changes.getPropertyChangeListeners()) {
      removePersonListener(l);
    }
    privatchatPanel.removeAllListener();
  }



  public void removePersonListener(PropertyChangeListener l) {
    changes.removePropertyChangeListener(l);
  }



  @Override
  public void ringRing(WindowAnrufenEvent event) {
    extText = event.getNickname();
    getMultiAbnehmen().setLanguage(KEY.FRAME_ABNEHMEN, event.getCode(), extText);
    getMultiAnrufen().setLanguage(KEY.TELEFON_ANRUFEN, event.getCode(), extText);
  }



  @Override
  public void setLanguage(ISO639 code) {
    setLanguage(KEY.PRIVATGESPRAECH, code);
    privatchatPanel.setLanguage(code);
    treeView(code);
  }



  public void setUserOnlinelist(List<UserOnline> list) {
    privatchatPanel.setUserList(list);
  }



  @Override
  public void setBackgroundGradient(Color color1, Color color2) {
    windowManager.setBackgroundGradient(color1, color2);
  }

  /*****************************************************************************/
  /*                                                                           */
  /* Innerclass FrameAction */
  /*                                                                           */
  /*****************************************************************************/

  class FrameAction extends InternalFrameAdapter {

    @Override
    public void internalFrameClosing(InternalFrameEvent event) {
      changes.firePropertyChange("JPersonChat", Control.NULL, Control.QUIT);
    }

  }

  public void updateSearchTextFieldColors() {
    privatchatPanel.updateColors();
  }



  public void setNickname(String nickname) {
    privatchatPanel.setNickname(nickname);
  }

}
