/**
 *  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.
 *
 */
package net.javacomm.client.base;

import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.MediaType;
import java.util.List;
import javax.swing.SwingWorker;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import net.javacomm.protocol.CBlock;
import net.javacomm.restserver.Wrapper;
import net.javacomm.share.Constants;
import net.javacomm.transfer.TransferUser;



public class SaveKontoaktivitaetenWorker extends SwingWorker<Boolean, Void> {

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

  private String pseudoid;
  private CBlock block;
  private List<TransferUser> userliste;
  private Boolean result;

  public SaveKontoaktivitaetenWorker(String pseudoid, CBlock cblock, List<TransferUser> userliste) {
    this.pseudoid = pseudoid;
    this.block = cblock;
    this.userliste = userliste;
  }



  @Override
  protected Boolean doInBackground() {
    try(Client client = ClientBuilder.newClient()) {
      client.register(MultiPartFeature.class);
      StringBuilder url = new StringBuilder(Constants.PROTOCOL).append(Constants.DOMAIN)
          .append("/javacommserver/administrator/benutzerstatus");
      WebTarget webtarget = client.target(url.toString());
      try(FormDataMultiPart multipart = new FormDataMultiPart()) {
        multipart.field("pseudoid", pseudoid);
        multipart.field("cblock", block.toString());
        multipart.field("userliste", Wrapper.toString(userliste));

        result = webtarget.request(MediaType.TEXT_PLAIN)
            .post(Entity.entity(multipart, multipart.getMediaType()), Boolean.class);

      }
    }
    catch (Exception e) {
      log.warn(e.getMessage());
    }
    return result;
  }

}
