/*
 * SPDX-FileCopyrightText: 2020~2020 CSSlayer <wengxt@gmail.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 */
#ifndef _FCITX5_MODULES_IMSELECTOR_IMSELECTOR_H_
#define _FCITX5_MODULES_IMSELECTOR_IMSELECTOR_H_

#include <cstddef>
#include <memory>
#include <vector>
#include "fcitx-config/configuration.h"
#include "fcitx-config/iniparser.h"
#include "fcitx-config/option.h"
#include "fcitx-config/rawconfig.h"
#include "fcitx-utils/handlertable.h"
#include "fcitx-utils/i18n.h"
#include "fcitx-utils/key.h"
#include "fcitx/addoninstance.h"
#include "fcitx/inputpanel.h"
#include "fcitx/instance.h"
#include "imselectortempmode.h"

namespace fcitx {

FCITX_CONFIGURATION(
    IMSelectorConfig,
    KeyListOption triggerKey{
        this,
        "TriggerKey",
        _("Trigger Key"),
        {},
        KeyListConstrain(KeyConstrainFlag::AllowModifierLess)};
    KeyListOption triggerKeyLocal{
        this,
        "TriggerKeyLocal",
        _("Trigger Key for only current input context"),
        {},
        KeyListConstrain(KeyConstrainFlag::AllowModifierLess)};
    KeyListOptionWithAnnotation<ToolTipAnnotation> switchKey{
        this,
        "SwitchKey",
        _("Hotkey for switching to the N-th input method"),
        {},
        KeyListConstrain(KeyConstrainFlag::AllowModifierLess),
        {},
        ToolTipAnnotation(
            _("The n-th hotkey in the list selects the n-th input method."))};
    KeyListOptionWithAnnotation<ToolTipAnnotation> switchKeyLocal{
        this,
        "SwitchKeyLocal",
        _("Hotkey for switching to the N-th input "
          "method for only current input context"),
        {},
        KeyListConstrain(KeyConstrainFlag::AllowModifierLess),
        {},
        ToolTipAnnotation(
            _("The n-th hotkey in the list selects the n-th input method."))};);

class IMSelector final : public AddonInstance {
public:
    IMSelector(Instance *instance);

    const IMSelectorConfig &config() const { return config_; }
    Instance *instance() { return instance_; }
    const KeyList &selectionKeys() const { return selectionKeys_; }

    void reset(InputContext *inputContext);
    bool selectInputMethod(InputContext *inputContext, size_t index,
                           bool local);
    const Configuration *getConfig() const override { return &config_; }
    void setConfig(const RawConfig &config) override {
        config_.load(config, true);
        safeSaveAsIni(config_, "conf/imselector.conf");
    }
    void reloadConfig() override;

private:
    std::vector<std::unique_ptr<fcitx::HandlerTableEntry<fcitx::EventHandler>>>
        eventHandlers_;
    Instance *instance_;
    IMSelectorConfig config_;
    KeyList selectionKeys_;
    IMSelectorTempMode tempMode_;
};
} // namespace fcitx

#endif // _FCITX5_MODULES_IMSELECTOR_IMSELECTOR_H_
