/*
 * Copyright © Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 or 3 as
 * published by the Free Software Foundation.
 *
 * 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/>.
 */

#include "basic_store.h"
#include "live_config_ini_file_common.h"

#include <miral/live_config_ini_file.h>

#include <mir/log.h>


namespace mlc = miral::live_config;

class mlc::IniFile::Self: public mlc::BasicStore
{
public:
    void load_file(std::istream& istream, std::filesystem::path const& path);
};

void mlc::IniFile::Self::load_file(std::istream& istream, std::filesystem::path const& path)
{
    do_transaction(
        [&]
        {
            parse_ini(istream, path, [this](auto const& key, auto value, auto const& path) { update_key(key, value, path); });
        });
}

mlc::IniFile::IniFile() :
    self{std::make_shared<Self>()}
{
}

mlc::IniFile::~IniFile() = default;

void mlc::IniFile::add_int_attribute(Key const& key, std::string_view description, HandleInt handler)
{
    self->add_int_attribute(key, description, std::move(handler));
}

void mlc::IniFile::add_ints_attribute(Key const& key, std::string_view description, HandleInts handler)
{
    self->add_ints_attribute(key, description, std::move(handler));
}

void mlc::IniFile::add_bool_attribute(Key const& key, std::string_view description, HandleBool handler)
{
    self->add_bool_attribute(key, description, std::move(handler));
}

void mlc::IniFile::add_float_attribute(Key const& key, std::string_view description, HandleFloat handler)
{
    self->add_float_attribute(key, description, std::move(handler));
}

void mlc::IniFile::add_floats_attribute(Key const& key, std::string_view description, HandleFloats handler)
{
    self->add_floats_attribute(key, description, std::move(handler));
}

void mlc::IniFile::add_string_attribute(Key const& key, std::string_view description, HandleString handler)
{
    self->add_string_attribute(key, description, std::move(handler));
}

void mlc::IniFile::add_strings_attribute(Key const& key, std::string_view description, HandleStrings handler)
{
    self->add_strings_attribute(key, description, std::move(handler));
}

void mlc::IniFile::add_int_attribute(Key const& key, std::string_view description, int preset, HandleInt handler)
{
    self->add_int_attribute(key, description, preset, std::move(handler));
}

void mlc::IniFile::add_ints_attribute(
    Key const& key, std::string_view description, std::span<int const> preset, HandleInts handler)
{
    self->add_ints_attribute(key, description, preset, std::move(handler));
}

void mlc::IniFile::add_bool_attribute(Key const& key, std::string_view description, bool preset, HandleBool handler)
{
    self->add_bool_attribute(key, description, preset, std::move(handler));
}

void mlc::IniFile::add_float_attribute(Key const& key, std::string_view description, float preset, HandleFloat handler)
{
    self->add_float_attribute(key, description, preset, std::move(handler));
}

void mlc::IniFile::add_floats_attribute(
    Key const& key, std::string_view description, std::span<float const> preset, HandleFloats handler)
{
    self->add_floats_attribute(key, description, preset, std::move(handler));
}

void mlc::IniFile::add_string_attribute(
    Key const& key, std::string_view description, std::string_view preset, HandleString handler)
{
    self->add_string_attribute(key, description, preset, std::move(handler));
}

void mlc::IniFile::add_strings_attribute(
    Key const& key,
    std::string_view description,
    std::span<std::string const> preset,
    HandleStrings handler)
{
    self->add_strings_attribute(key, description, preset, std::move(handler));
}

void mlc::IniFile::on_done(HandleDone handler)
{
    self->on_done(std::move(handler));
}

void mlc::IniFile::load_file(std::istream& istream, std::filesystem::path const& path)
{
    self->load_file(istream, path);
}
