/*

Copyright (c) 2018 - 2025 PhotoPrism UG. All rights reserved.

    This program is free software: you can redistribute it and/or modify
    it under Version 3 of the GNU Affero General Public License (the "AGPL"):
    <https://docs.photoprism.app/license/agpl>

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    The AGPL is supplemented by our Trademark and Brand Guidelines,
    which describe how our Brand Assets may be used:
    <https://www.photoprism.app/trademark>

Feel free to send an email to hello@photoprism.app if you have questions,
want to support our work, or just want to say hello.

Additional information can be found in our Developer Guide:
<https://docs.photoprism.app/developer-guide/>

*/

import $api from "common/api";
import { $config } from "app/session";
import $notify from "notify.js";

function poll(interval, maxAttempts) {
  let attempts = 0;

  const executePoll = async (resolve, reject) => {
    attempts++;

    try {
      const xhr = new XMLHttpRequest();
      xhr.open("GET", $config.apiUri + "/status", false);
      xhr.send(null);

      if (xhr.status === 200 && xhr.response && xhr.response.includes("operational")) {
        // console.log("response:", xhr.response);
        return resolve();
      }
    } catch (e) {
      // console.log("status:", e);
    }

    if (maxAttempts && attempts === maxAttempts) {
      return reject(new Error("exceeded max attempts"));
    }

    setTimeout(executePoll, interval, resolve, reject);
  };

  return new Promise(executePoll);
}

export function restart(uri) {
  $notify.blockUI("progress");
  $notify.wait();
  $notify.ajaxStart();

  return $api
    .post("server/stop")
    .then(() => {
      return poll(1000, 180)
        .then(() => {
          if (uri) {
            window.location.href = uri;
            window.location.reload();
          } else {
            window.location.reload();
          }
        })
        .catch(() => {
          $notify.ajaxEnd();
          $notify.error("Failed to restart server");
          $notify.unblockUI();
        });
    })
    .catch(() => {
      $notify.ajaxEnd();
      $notify.error("Failed to restart server");
      $notify.unblockUI();
    });
}
