// Copyright 2022 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <esp_log.h>
#include <string.h>

#include <app/clusters/ota-requestor/BDXDownloader.h>
#include <app/clusters/ota-requestor/DefaultOTARequestor.h>
#include <app/clusters/ota-requestor/ExtendedOTARequestorDriver.h>
#include <app/clusters/ota-requestor/DefaultOTARequestorStorage.h>
#ifdef CONFIG_CHIP_ENABLE_EXTERNAL_PLATFORM
#ifndef EXTERNAL_ESP32OTAIMAGEPROCESSORIMPL_HEADER
#error "Please define EXTERNAL_ESP32OTAIMAGEPROCESSORIMPL_HEADER in your external platform gn/cmake file"
#endif // !EXTERNAL_ESP32OTAIMAGEPROCESSORIMPL_HEADER
#include EXTERNAL_ESP32OTAIMAGEPROCESSORIMPL_HEADER
#else // CONFIG_CHIP_ENABLE_EXTERNAL_PLATFORM
#include <platform/ESP32/OTAImageProcessorImpl.h>
#endif // !CONFIG_CHIP_ENABLE_EXTERNAL_PLATFORM

#include <esp_matter.h>
#include <esp_matter_ota.h>
#include <zap-generated/endpoint_config.h>

using chip::BDXDownloader;
using chip::DefaultOTARequestor;
using chip::DefaultOTARequestorStorage;
using chip::OTAImageProcessorImpl;
using chip::Server;
using chip::DeviceLayer::ExtendedOTARequestorDriver;

using namespace esp_matter;

#if CONFIG_ENABLE_OTA_REQUESTOR
DefaultOTARequestor gRequestorCore;
DefaultOTARequestorStorage gRequestorStorage;
ExtendedOTARequestorDriver gRequestorUser;
BDXDownloader gDownloader;
OTAImageProcessorImpl gImageProcessor;

static esp_matter_ota_requestor_impl_t s_ota_requestor_impl = {
    .driver = &gRequestorUser,
    .image_processor = &gImageProcessor,
};
#endif // CONFIG_ENABLE_OTA_REQUESTOR

esp_err_t esp_matter_ota_requestor_init(void)
{
#if (CONFIG_ENABLE_OTA_REQUESTOR && (FIXED_ENDPOINT_COUNT == 0))
    endpoint::ota_requestor::config_t config;
    node_t *root_node = esp_matter::node::get();
    endpoint_t *root_node_endpoint = esp_matter::endpoint::get(root_node, 0);

    if (!root_node || !root_node_endpoint) {
        return ESP_FAIL;
    }

    return endpoint::ota_requestor::add(root_node_endpoint, &config);
#else
    return ESP_ERR_NOT_SUPPORTED;
#endif
}

#if CONFIG_ENABLE_OTA_REQUESTOR
static esp_err_t esp_matter_ota_override_impl(const esp_matter_ota_requestor_impl_t *impl)
{
    VerifyOrReturnError(impl != nullptr, ESP_ERR_INVALID_ARG);

    if (impl->driver != nullptr) {
        s_ota_requestor_impl.driver = impl->driver;
    }
    if (impl->image_processor != nullptr) {
        s_ota_requestor_impl.image_processor = impl->image_processor;
    }

    s_ota_requestor_impl.user_consent = impl->user_consent;

    return ESP_OK;
}
#endif // CONFIG_ENABLE_OTA_REQUESTOR

void esp_matter_ota_requestor_start(void)
{
#if CONFIG_ENABLE_OTA_REQUESTOR
    VerifyOrReturn(chip::GetRequestorInstance() == nullptr);

    chip::SetRequestorInstance(&gRequestorCore);
    gRequestorStorage.Init(Server::GetInstance().GetPersistentStorage());

    if (gRequestorCore.Init(Server::GetInstance(), gRequestorStorage, *s_ota_requestor_impl.driver, gDownloader) != CHIP_NO_ERROR) {
        ESP_LOGE("OTARequestor", "Failed to init OTARequestor core");
        return;
    }

    gImageProcessor.SetOTADownloader(&gDownloader);

    gDownloader.SetImageProcessorDelegate(s_ota_requestor_impl.image_processor);

    s_ota_requestor_impl.driver->SetUserConsentDelegate(s_ota_requestor_impl.user_consent);
    s_ota_requestor_impl.driver->Init(&gRequestorCore, s_ota_requestor_impl.image_processor);
#endif
}

#ifdef CONFIG_ENABLE_ENCRYPTED_OTA
esp_err_t esp_matter_ota_requestor_encrypted_init(const char *key, uint16_t size)
{
    VerifyOrReturnError(key != nullptr, ESP_ERR_INVALID_ARG);
    VerifyOrReturnError(gImageProcessor.InitEncryptedOTA(chip::CharSpan{key, size}) == CHIP_NO_ERROR, ESP_ERR_INVALID_STATE);
    return ESP_OK;
}
#endif // CONFIG_ENABLE_ENCRYPTED_OTA

esp_err_t esp_matter_ota_requestor_set_config(const esp_matter_ota_config_t  &config)
{
#if CONFIG_ENABLE_OTA_REQUESTOR
    if (config.periodic_query_timeout) {
        gRequestorUser.SetPeriodicQueryTimeout(config.periodic_query_timeout);
    }
    if (config.watchdog_timeout) {
        gRequestorUser.SetWatchdogTimeout(config.watchdog_timeout);
    }
    if (config.impl != nullptr) {
        esp_matter_ota_override_impl(config.impl);
    }

    return ESP_OK;
#else
    return ESP_ERR_NOT_SUPPORTED;
#endif // CONFIG_ENABLE_OTA_REQUESTOR
}
