/**
 * @file PoolRouter.hpp
 * @author The ARTIS Development Team
 * See the AUTHORS or Authors.txt file
 */

/*
 * ARTIS - the multimodeling and simulation environment
 * This file is a part of the ARTIS environment
 *
 * Copyright (C) 2013-2023 ULCO http://www.univ-littoral.fr
 *
 * This program 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 3 of the License, or
 * (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef ARTIS_FACTORY_POOL_ROUTER_HPP
#define ARTIS_FACTORY_POOL_ROUTER_HPP

#include "Base.hpp"
#include "ProductionOrder.hpp"

#include <deque>

namespace artis::factory {

struct PoolRouterParameters {
  uint type;
  std::size_t machine_number;
};

class PoolRouter : public Dynamics<PoolRouter, PoolRouterParameters> {
public:
  struct inputs {
    enum values {
      IN, IN_M = 1000
    };
  };

  struct outputs {
    enum values {
      OUT, OUT_M = 1000
    };
  };

  struct vars {
    enum values {
      WAITING_PO_NUMBER
    };
  };

  PoolRouter(const std::string &name, const Context<PoolRouter, PoolRouterParameters> &context)
    : Dynamics<PoolRouter, PoolRouterParameters>(name, context),
      _parameters(context.parameters()), _available_machines(context.parameters().machine_number) {
    input_port({inputs::IN, "in"});
    output_port({outputs::OUT, "out"});
    for (unsigned int i = 0; i < context.parameters().machine_number; ++i) {
      output_port({outputs::OUT_M + i, "out_m" + std::to_string(i)});
      input_port({inputs::IN_M + i, "in_m" + std::to_string(i)});
      _available_machines[i] = true;
    }
    observables({{vars::WAITING_PO_NUMBER, "waiting_po_number"}});
  }

  ~PoolRouter() override = default;

  void dint(const Time & /* t */) override;

  void dext(const Time & /* t */, const Time & /* e */, const Bag & /* bag*/) override;

  void start(const Time & /* t */) override;

  Time ta(const Time & /* t */) const override;

  Bag lambda(const Time & /* t */) const override;

  artis::common::event::Value observe(const Time &t, unsigned int index) const override;

private:
  typedef std::deque<std::unique_ptr<ProductionOrder>> ProductionOrders;

  ProductionOrders::const_iterator next_po() const;

  struct Phase {
    enum values {
      INIT, WAIT, SEND
    };

    static std::string to_string(const values &value) {
      switch (value) {
        case INIT:
          return "INIT";
        case WAIT:
          return "WAIT";
        case SEND:
          return "SEND";
      }
      return "";
    }
  };

  // parameters
  PoolRouterParameters _parameters;

  // state
  Phase::values _phase;
  ProductionOrders _pending_po;
  std::vector<bool> _available_machines;
};

} // namespace artis::factory

#endif