// Copyright (C) 2018-2023 Iñaki Ucar
//
// This file is part of simmer.
//
// simmer is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// simmer 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with simmer. If not, see <http://www.gnu.org/licenses/>.

#ifndef simmer__process_source_h
#define simmer__process_source_h

#include <simmer/process.h>
#include <simmer/process/order.h>
#include <simmer/process/arrival.h>
#include <simmer/activity.h>
#include <any>

#define STDANYCAST std::any_cast
#if defined(__clang__) && defined(__apple_build_version__)
#if __apple_build_version__ <= 10001045
namespace simmer { namespace _std {

  template <class T>
  T any_cast(const std::any& operand) {
    if (!operand.has_value() || typeid(T) != operand.type())
      throw std::runtime_error("bad_any_cast");
    return *std::any_cast<T>(&operand);
  }

}}
#undef  STDANYCAST
#define STDANYCAST simmer::_std::any_cast
#endif
#endif

namespace simmer {
  /**
   * Abstract class for source processes.
   */
  class Source : public Process {
    friend class Arrival;

  public:
    typedef USET<Arrival*> ArrSet;

    /**
    * Constructor.
    * @param sim             a pointer to the simulator
    * @param name_prefix     name prefix for each new arrival
    * @param mon             int that indicates whether this entity must be monitored
    * @param trj             a user-defined R trajectory
    * @param source          some source for arrivals
    * @param order           priority, preemptible, restart
    */
    Source(Simulator* sim, const std::string& name_prefix, int mon,
           const REnv& trj, const Order& order)
      : Process(sim, name_prefix, mon, PRIORITY_MIN), count(0), order(order),
        first_activity(internal::head(trj)), trj(trj) {}

    virtual void reset() {
      count = 0;
      ahead.clear();
    }

    virtual bool deactivate() {
      for (auto arrival : ahead) {
        arrival->deactivate();
        delete arrival;
        count--;
      }
      ahead.clear();
      return Process::deactivate();
    }

    int get_n_generated() const { return count; }

    REnv get_trajectory() const { return trj; }

    virtual void set_source(const std::any& new_source) = 0;

    void set_trajectory(const REnv& new_trj) {
      trj = new_trj;
      first_activity = internal::head(trj);
    }

  protected:
    int count;                /**< number of arrivals generated */
    Order order;
    Activity* first_activity;

    Arrival* new_arrival(double delay) {
      // format the name and create the next arrival
      std::string arr_name = MakeString() << name << count++;
      Arrival* arrival = new Arrival(
        sim, arr_name, is_monitored(), order, first_activity, count, this);
      ahead.emplace(arrival);

      if (sim->verbose) sim->print("source", name, "new", arr_name,
          MakeString() << (sim->now() + delay));

      // schedule the arrival
      sim->schedule(delay, arrival, first_activity && first_activity->priority ?
                      first_activity->priority : count);

      return arrival;
    }

    void unregister_arrival(Arrival* arrival) {
      ahead.erase(arrival);
    }

    bool check_stop(double delay) {
      if (delay < 0 || RNum::is_na(delay))
        return true;
      return false;
    }

  private:
    REnv trj;
    ArrSet ahead;
  };

} // namespace simmer

#endif
