/**
 * @file FactoryGraphManager.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_FACTORY_GRAPH_MANAGER_HPP
#define ARTIS_FACTORY_FACTORY_GRAPH_MANAGER_HPP

#include "Base.hpp"
#include "JsonReader.hpp"
#include "PoolRouter.hpp"
#include "Router.hpp"

#include <artis-star/kernel/pdevs/GraphManager.hpp>

namespace artis::factory {

struct FactoryGraphManagerParameters {
  Factory factory;
};

class FactoryGraphManager
  : public artis::pdevs::GraphManager<artis::common::DoubleTime, artis::common::NoParameters, FactoryGraphManagerParameters> {
public:
  enum sub_models {
    GENERATOR, ROUTER, POOL_ROUTER, MACHINE = 10000
  };

  FactoryGraphManager(Coordinator *coordinator, const artis::common::NoParameters &parameters,
                      const FactoryGraphManagerParameters &graph_parameters) :
    artis::pdevs::GraphManager<artis::common::DoubleTime, artis::common::NoParameters, FactoryGraphManagerParameters>(
      coordinator, parameters, graph_parameters) {
    unsigned int machine_index = 0;
    std::map<unsigned int, Program> programs;

    for (const auto &product: graph_parameters.factory._products) {
      unsigned int product_id = product.first;
      Program program;

      for (const auto &pool_job: product.second._pool_jobs) {
        std::vector<uint8_t> machines;

        for (const auto &machine_job: pool_job._machine_jobs) {
          machines.push_back(machine_job._machine_id);
        }
        program.emplace_back((uint8_t) pool_job._pool_id, machines);
      }
      programs[product_id] = program;
    }

    ProductionOrderGeneratorParameters generator_parameters{programs,
                                                            graph_parameters.factory._generator._random_seed,
                                                            graph_parameters.factory._generator._min_send_speed_rate,
                                                            graph_parameters.factory._generator._max_send_speed_rate};

    _generator = new GeneratorSimulator("G", generator_parameters);
    _router = new RouterSimulator("R", {graph_parameters.factory._pools.size()});

    this->add_child(ROUTER, _router);
    this->add_child(GENERATOR, _generator);

    for (const auto &p: graph_parameters.factory._pools) {
      _pool_routers.push_back(
        new PoolRouterSimulator("P_R_" + std::to_string(std::get<0>(p)),
                                PoolRouterParameters{std::get<1>(p), std::get<2>(p).size()}));
      this->add_child(POOL_ROUTER + std::get<0>(p), _pool_routers.back());
      out({_router, artis::factory::Router::outputs::OUT_P + std::get<0>(p)})
        >> in({_pool_routers.back(), artis::factory::PoolRouter::inputs::IN});
      out({_pool_routers.back(), artis::factory::PoolRouter::outputs::OUT})
        >> in({_router, artis::factory::Router::inputs::IN_P + std::get<0>(p)});
      for (const auto &m: std::get<2>(p)) {
        switch (m->machine_type) {
          case PROCESSOR: {
            auto new_processor = new ProcessorSimulator(
              "M_" + std::to_string(m->pool_id) + "_" + std::to_string(m->machine_id), (ProcessorParameters &) (*m));

            _processors.push_back(new_processor);
            this->add_child(MACHINE + machine_index, new_processor);
            out({_pool_routers.back(), artis::factory::PoolRouter::outputs::OUT_M + m->machine_id})
              >> in({new_processor, artis::factory::Processor::inputs::IN});
            out({new_processor, artis::factory::Processor::outputs::OUT})
              >> in({_pool_routers.back(), artis::factory::PoolRouter::inputs::IN_M + m->machine_id});
            ++machine_index;
            break;
          }
          case SINK: {
            // TODO
            break;
          }
          case SEPARATOR: {
            // TODO
            break;
          }
          case COMBINER : {
            // TODO
            break;
          }
          case CONVEYOR :{
            // TODO
            break;
          }
          default: {}
        }
      }
    }
    out({_generator, artis::factory::ProductionOrderGenerator::outputs::OUT})
      >> in({_router, artis::factory::Router::inputs::IN});
  }

private:
  typedef artis::pdevs::Simulator<artis::common::DoubleTime, artis::factory::ProductionOrderGenerator, artis::factory::ProductionOrderGeneratorParameters> GeneratorSimulator;
  typedef artis::pdevs::Simulator<artis::common::DoubleTime, artis::factory::Router, artis::factory::RouterParameters> RouterSimulator;
  typedef artis::pdevs::Simulator<artis::common::DoubleTime, artis::factory::PoolRouter, artis::factory::PoolRouterParameters> PoolRouterSimulator;
  typedef artis::pdevs::Simulator<artis::common::DoubleTime, artis::factory::Processor, artis::factory::ProcessorParameters> ProcessorSimulator;

  GeneratorSimulator *_generator;
  RouterSimulator *_router;
  std::vector<PoolRouterSimulator *> _pool_routers;
  std::vector<ProcessorSimulator *> _processors;
};

}

#endif //ARTIS_FACTORY_FACTORY_GRAPH_MANAGER_HPP
