// SPDX-License-Identifier: Apache-2.0
// Copyright 2020 - 2026 Pionix GmbH and Contributors to EVerest
#include "evse_manager_consumer_API.hpp"

#include <everest_api_types/auth/wrapper.hpp>
#include <everest_api_types/energy/API.hpp>
#include <everest_api_types/energy/codec.hpp>
#include <everest_api_types/energy/wrapper.hpp>
#include <everest_api_types/evse_board_support/API.hpp>
#include <everest_api_types/evse_board_support/codec.hpp>
#include <everest_api_types/evse_board_support/wrapper.hpp>
#include <everest_api_types/evse_manager/API.hpp>
#include <everest_api_types/evse_manager/codec.hpp>
#include <everest_api_types/evse_manager/wrapper.hpp>
#include <everest_api_types/generic/codec.hpp>
#include <everest_api_types/generic/string.hpp>
#include <everest_api_types/iso15118_charger/API.hpp>
#include <everest_api_types/iso15118_charger/codec.hpp>
#include <everest_api_types/iso15118_charger/wrapper.hpp>
#include <everest_api_types/isolation_monitor/API.hpp>
#include <everest_api_types/isolation_monitor/codec.hpp>
#include <everest_api_types/isolation_monitor/wrapper.hpp>
#include <everest_api_types/power_supply_DC/API.hpp>
#include <everest_api_types/power_supply_DC/codec.hpp>
#include <everest_api_types/power_supply_DC/wrapper.hpp>
#include <everest_api_types/powermeter/codec.hpp>
#include <everest_api_types/powermeter/wrapper.hpp>
#include <everest_api_types/uk_random_delay/API.hpp>
#include <everest_api_types/uk_random_delay/codec.hpp>
#include <everest_api_types/uk_random_delay/wrapper.hpp>
#include <everest_api_types/utilities/AsyncApiRequestReply.hpp>
#include <everest_api_types/utilities/codec.hpp>

#include <everest/logging.hpp>

namespace {
template <class T> T const& to_external_api(T const& val) {
    return val;
}

} // namespace

namespace module {

namespace API_types_ext = API_types::evse_manager;
namespace API_powermeter = API_types::powermeter;
namespace API_iso = API_types::iso15118_charger;
namespace API_energy = API_types::energy;
namespace API_evse_bsp = API_types::evse_board_support;
namespace API_imd = API_types::isolation_monitor;
namespace API_dc = API_types::power_supply_DC;
namespace API_random_delay = API_types::uk_random_delay;
namespace API_generic = API_types::generic;
using ev_API::deserialize;

void evse_manager_consumer_API::init() {
    invoke_init(*p_main);

    API_types_entry::CommunicationParameters comm_params{};
    comm_params.heartbeat_period_ms = config.cfg_heartbeat_interval_ms;
    comm_params.communication_check_period_s = config.cfg_communication_check_to_s;
    comm_params.request_reply_timeout_s = config.cfg_request_reply_to_s;
    helper.init(comm_params);
}

void evse_manager_consumer_API::ready() {
    invoke_ready(*p_main);

    generate_api_cmd_get_evse();
    generate_api_cmd_enable_disable();
    generate_api_cmd_pause_charging();
    generate_api_cmd_resume_charging();
    generate_api_cmd_stop_transaction();
    generate_api_cmd_force_unlock();
    generate_api_cmd_random_delay_enable();
    generate_api_cmd_random_delay_disable();
    generate_api_cmd_random_delay_cancel();
    generate_api_cmd_random_delay_set_duration_s();

    generate_api_var_session_event();
    generate_api_var_hlc_session_failed();
    generate_api_var_session_info(); // special, not just forwarded
    generate_api_var_ev_info();
    generate_api_var_powermeter();
    generate_api_var_evse_id();
    generate_api_var_hw_capabilities();
    generate_api_var_enforced_limits();
    generate_api_var_selected_protocol();
    generate_api_var_powermeter_public_key_ocmf();
    generate_api_var_supported_energy_transfer_modes();

    generate_api_var_ac_nr_of_phases_available();
    generate_api_var_ac_pp_ampacity();
    generate_api_var_dlink_ready();
    generate_api_var_isolation_measurement();
    generate_api_var_dc_voltage_current();
    generate_api_var_dc_mode();
    generate_api_var_dc_capabilities();
    generate_api_var_random_delay_countdown();

    helper.generate_api_var_communication_check(&comm_check);
    comm_check.start(config.cfg_communication_check_to_s);
    helper.setup_heartbeat_generator(&comm_check, config.cfg_heartbeat_interval_ms);
    helper.publish_ready_beacon();
}

auto evse_manager_consumer_API::forward_api_var(std::string const& var) {
    using namespace API_types_ext;
    using namespace API_powermeter;
    using namespace API_generic;
    using namespace API_energy;
    using namespace API_evse_bsp;
    using namespace API_iso;
    using namespace API_imd;
    using namespace API_dc;
    using namespace API_random_delay;
    const auto topic = helper.get_topics().everest_to_extern(var);
    return [this, topic](auto const& val) {
        try {
            auto&& external = to_external_api(val);
            auto&& payload = serialize(external);
            mqtt_v.publish(topic, payload);
        } catch (const std::exception& e) {
            EVLOG_warning << "Variable: '" << topic << "' failed with -> " << e.what();
        } catch (...) {
            EVLOG_warning << "Invalid data: Cannot convert internal to external or serialize it.\n" << topic;
        }
    };
}

void evse_manager_consumer_API::generate_api_cmd_get_evse() {
    helper.subscribe_api_topic("get_evse", [this](std::string const& data) {
        API_generic::RequestReply msg;
        if (deserialize(data, msg)) {
            auto reply = API_types_ext::to_external_api(r_evse_manager->call_get_evse());
            mqtt_v.publish(msg.replyTo, serialize(reply));
            return true;
        }
        return false;
    });
}

void evse_manager_consumer_API::generate_api_cmd_enable_disable() {
    helper.subscribe_api_topic("enable_disable", [this](std::string const& data) {
        API_generic::RequestReply msg;
        if (deserialize(data, msg)) {
            API_types_ext::EnableDisableRequest payload;
            if (deserialize(msg.payload, payload)) {
                auto reply = r_evse_manager->call_enable_disable(payload.connector_id, to_internal_api(payload.source));
                mqtt_v.publish(msg.replyTo, API_generic::serialize(reply));
                return true;
            }
        }
        return false;
    });
}

void evse_manager_consumer_API::generate_api_cmd_pause_charging() {
    helper.subscribe_api_topic("pause_charging", [this](std::string const& data) {
        API_generic::RequestReply msg;
        if (deserialize(data, msg)) {
            auto result = r_evse_manager->call_pause_charging();
            mqtt_v.publish(msg.replyTo, result);
            return true;
        }
        return false;
    });
}

void evse_manager_consumer_API::generate_api_cmd_resume_charging() {
    helper.subscribe_api_topic("resume_charging", [this](std::string const& data) {
        API_generic::RequestReply msg;
        if (deserialize(data, msg)) {
            auto result = r_evse_manager->call_resume_charging();
            mqtt_v.publish(msg.replyTo, result);
            return true;
        }
        return false;
    });
}

void evse_manager_consumer_API::generate_api_cmd_stop_transaction() {
    helper.subscribe_api_topic("stop_transaction", [this](std::string const& data) {
        auto result = false;
        API_generic::RequestReply msg;
        if (deserialize(data, msg)) {
            API_types_ext::StopTransactionRequest_External payload;
            if (deserialize(msg.payload, payload)) {
                result = r_evse_manager->call_stop_transaction(API_types_ext::to_internal_api(payload));
                mqtt_v.publish(msg.replyTo, result);
                return true;
            }
        }
        return false;
    });
}

void evse_manager_consumer_API::generate_api_cmd_force_unlock() {
    helper.subscribe_api_topic("force_unlock", [this](std::string const& data) {
        API_generic::RequestReply msg;
        if (deserialize(data, msg)) {
            int payload;
            if (deserialize(msg.payload, payload)) {
                auto result = r_evse_manager->call_force_unlock(payload);
                mqtt_v.publish(msg.replyTo, result);
            }
            return true;
        }
        return false;
    });
}

void evse_manager_consumer_API::generate_api_cmd_random_delay_enable() {
    if (not r_random_delay.empty()) {
        helper.subscribe_api_topic("random_delay_enable", [this](std::string const&) {
            r_random_delay[0]->call_enable();
            return true;
        });
    }
}

void evse_manager_consumer_API::generate_api_cmd_random_delay_disable() {
    if (not r_random_delay.empty()) {
        helper.subscribe_api_topic("random_delay_disable", [=](std::string const&) {
            r_random_delay[0]->call_disable();
            return true;
        });
    }
}

void evse_manager_consumer_API::generate_api_cmd_random_delay_cancel() {
    if (not r_random_delay.empty()) {
        helper.subscribe_api_topic("random_delay_cancel", [=](std::string const&) {
            r_random_delay[0]->call_cancel();
            return true;
        });
    }
}

void evse_manager_consumer_API::generate_api_cmd_random_delay_set_duration_s() {
    if (not r_random_delay.empty())
        helper.subscribe_api_topic("random_delay_set_duration_s", [=](std::string const& data) {
            int32_t duration;
            if (deserialize(data, duration)) {
                r_random_delay[0]->call_set_duration_s(duration);
                return true;
            }
            return false;
        });
}

void evse_manager_consumer_API::generate_api_var_session_event() {
    r_evse_manager->subscribe_session_event(forward_api_var("session_event"));
}

void evse_manager_consumer_API::generate_api_var_hlc_session_failed() {
    r_evse_manager->subscribe_hlc_session_failed(forward_api_var("hlc_session_failed"));
}

void evse_manager_consumer_API::generate_api_var_ev_info() {
    r_evse_manager->subscribe_ev_info(forward_api_var("ev_info"));
}

void evse_manager_consumer_API::generate_api_var_powermeter() {
    r_evse_manager->subscribe_powermeter(forward_api_var("powermeter"));
}

void evse_manager_consumer_API::generate_api_var_evse_id() {
    r_evse_manager->subscribe_evse_id(forward_api_var("evse_id"));
}

void evse_manager_consumer_API::generate_api_var_hw_capabilities() {
    r_evse_manager->subscribe_hw_capabilities(forward_api_var("hw_capabilities"));
}

void evse_manager_consumer_API::generate_api_var_enforced_limits() {
    r_evse_manager->subscribe_enforced_limits(forward_api_var("enforced_limits"));
}

void evse_manager_consumer_API::generate_api_var_selected_protocol() {
    r_evse_manager->subscribe_selected_protocol(forward_api_var("selected_protocol"));
}

void evse_manager_consumer_API::generate_api_var_supported_energy_transfer_modes() {
    r_evse_manager->subscribe_supported_energy_transfer_modes(forward_api_var("supported_energy_transfer_modes"));
}

void evse_manager_consumer_API::generate_api_var_powermeter_public_key_ocmf() {
    r_evse_manager->subscribe_powermeter_public_key_ocmf(forward_api_var("powermeter_public_key_ocmf"));
}

void evse_manager_consumer_API::generate_api_var_ac_nr_of_phases_available() {
    if (not r_evse_bsp.empty()) {
        r_evse_bsp[0]->subscribe_ac_nr_of_phases_available(forward_api_var("ac_nr_of_phases_available"));
    }
}

void evse_manager_consumer_API::generate_api_var_ac_pp_ampacity() {
    if (not r_evse_bsp.empty()) {
        r_evse_bsp[0]->subscribe_ac_pp_ampacity(forward_api_var("ac_pp_ampacity"));
    }
}

void evse_manager_consumer_API::generate_api_var_dlink_ready() {
    if (not r_slac.empty()) {
        r_slac[0]->subscribe_dlink_ready(forward_api_var("dlink_ready"));
    }
}

void evse_manager_consumer_API::generate_api_var_isolation_measurement() {
    if (not r_imd.empty()) {
        r_imd[0]->subscribe_isolation_measurement(forward_api_var("isolation_measurement"));
    }
}

void evse_manager_consumer_API::generate_api_var_dc_voltage_current() {
    if (not r_ps_dc.empty()) {
        r_ps_dc[0]->subscribe_voltage_current(forward_api_var("dc_voltage_current"));
    }
}

void evse_manager_consumer_API::generate_api_var_dc_mode() {
    if (not r_ps_dc.empty()) {
        r_ps_dc[0]->subscribe_mode(forward_api_var("dc_mode"));
    }
}

void evse_manager_consumer_API::generate_api_var_dc_capabilities() {
    if (not r_ps_dc.empty()) {
        r_ps_dc[0]->subscribe_capabilities(forward_api_var("dc_capabilities"));
    }
}

void evse_manager_consumer_API::generate_api_var_random_delay_countdown() {
    if (not r_random_delay.empty()) {
        r_random_delay[0]->subscribe_countdown(forward_api_var("random_delay_countdown"));
    }
}

void evse_manager_consumer_API::generate_api_var_session_info() {
    this->session_info.handle()->set_publish_callback(
        [this](const everest::lib::API::V1_0::types::evse_manager::SessionInfo& external) {
            static const auto topic = helper.get_topics().everest_to_extern("session_info");
            try {
                auto&& payload = serialize(external);
                mqtt_v.publish(topic, payload);
            } catch (const std::exception& e) {
                EVLOG_warning << "Variable: '" << topic << "' failed with -> " << e.what();
            } catch (...) {
                EVLOG_warning << "Invalid data: Cannot convert internal to external or serialize it.\n" << topic;
            }
        });

    this->r_evse_manager->subscribe_session_event([this](types::evse_manager::SessionEvent const& session_event) {
        session_info.handle()->update_state(session_event);
    });

    this->r_evse_manager->subscribe_powermeter([this](types::powermeter::Powermeter const& powermeter) {
        session_info.handle()->update_powermeter(powermeter);
    });

    this->r_evse_manager->subscribe_selected_protocol(
        [this](std::string const& protocol) { session_info.handle()->update_selected_protocol(protocol); });
}

} // namespace module
