/**
 *  Copyright © 2025, 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.screencaster;

import static net.javacomm.share.Constants.DOMAIN;
import static net.javacomm.share.Constants.HTTPS;
import jakarta.ws.rs.ProcessingException;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.awt.MouseInfo;
import java.awt.Point;
import java.io.IOException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;



/**
 * Der MousePointer wird an den Server gesendet.
 */
public class Mousepointer extends Thread {

  private final static Logger log = LogManager.getLogger(Mousepointer.class);

  private volatile Point mousepointer;
  private Point old = new Point(0, 0);
  private final String conferencekey;
  private Client client;
  private String pseudoid;

  /**
   * Der Mousepointer wird für die Übertragung vorbereitet.
   * 
   * @param pseudoid
   *                      der Mousepointer stammt von diesem Anwender
   * @param conferencekey
   *                      alle Mousepointerempfänger
   */
  public Mousepointer(String pseudoid, String conferencekey) {
    this.pseudoid = pseudoid;
    this.conferencekey = conferencekey;
    client = ClientBuilder.newClient().property(ClientProperties.CONNECT_TIMEOUT, 2000)
        .property(ClientProperties.READ_TIMEOUT, 200);
    client.register(MultiPartFeature.class);

  }



  @Override
  public void run() {
    StringBuilder url = new StringBuilder(HTTPS).append(DOMAIN)
        .append("/javacommserver/screenshot/mousepointer");
    while (!Thread.currentThread().isInterrupted()) {
      mousepointer = MouseInfo.getPointerInfo().getLocation();
      try {
        Thread.sleep(80);
        if (mousepointer.equals(old)) continue;
        old = new Point(mousepointer);
        // senden
        try(FormDataMultiPart multipart = new FormDataMultiPart()) {
          multipart.field("x", (int) mousepointer.getX(), MediaType.TEXT_PLAIN_TYPE);
          multipart.field("y", (int) mousepointer.getY(), MediaType.TEXT_PLAIN_TYPE);
          multipart.field("conferencekey", conferencekey);
          multipart.field("pseudoid", pseudoid);
          try(
            Response response = client.target(url.toString()).request(MediaType.TEXT_PLAIN)
                .put(Entity.entity(multipart, multipart.getMediaType()));
          ) {

          }
        }
        catch (ProcessingException e) {
          log.info(e.getMessage());
        }
        catch (IOException e) {
          log.warn(e.getMessage());
        }
      }
      catch (InterruptedException e) {
        break;
      }
    }
    client.close();
  }



  /**
   * An welcher Bildschirmposition befindet sich der Mauszeiger?
   * 
   * @return diese Mausposition
   */
  public Point getMousepoint() {
    return mousepointer != null ? new Point(mousepointer) : new Point(0, 0);
  }

}
