// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "buildinfo.h"
#include "kit.h"
#include "task.h"

#include <utils/guard.h>

#include <QWidget>

QT_BEGIN_NAMESPACE
class QCheckBox;
class QGridLayout;
class QLabel;
class QPushButton;
class QSpacerItem;
QT_END_NAMESPACE

namespace Utils {
class DetailsWidget;
class MacroExpander;
class PathChooser;
} // namespace Utils

namespace ProjectExplorer {
class BuildInfo;

namespace Internal {

class TargetSetupWidget : public QWidget
{
    Q_OBJECT

public:
    TargetSetupWidget(Kit *k, const Utils::FilePath &projectPath);

    Kit *kit() const;
    void clearKit();

    bool isKitSelected() const;
    void setKitSelected(bool b);
    bool isValid() const { return m_isValid; }

    void addBuildInfos(const QList<BuildInfo> &infos, bool isImport);

    const QList<BuildInfo> selectedBuildInfoList() const;
    void setProjectPath(const Utils::FilePath &projectPath);
    void expandWidget();
    void update(const TasksGenerator &generator);

signals:
    void selectedToggled() const;
    void validToggled() const;

private:
    struct BuildInfoStore {
        ~BuildInfoStore();
        BuildInfoStore() = default;
        BuildInfoStore(const BuildInfoStore &other) = delete;
        BuildInfoStore(BuildInfoStore &&other);
        BuildInfoStore &operator=(const BuildInfoStore &other) = delete;
        BuildInfoStore &operator=(BuildInfoStore &&other);

        Utils::FilePath expandedBuildDir(const Kit *kit) const;

        BuildInfo buildInfo;
        QCheckBox *checkbox = nullptr;
        QLabel *label = nullptr;
        QLabel *issuesLabel = nullptr;
        Utils::PathChooser *pathChooser = nullptr;
        Utils::MacroExpander *expander = nullptr;

        bool isEnabled = false;
        bool hasIssues = false;
        bool customBuildDir = false;
        bool isImported = false;
    };

    static const QList<BuildInfo> buildInfoList(const Kit *k, const Utils::FilePath &projectPath);

    bool hasSelectableBuildConfigurations() const;

    void setValid(bool valid);
    void checkBoxToggled(QCheckBox *checkBox, bool b);
    void pathChanged(Utils::PathChooser *pathChooser);
    void targetCheckBoxToggled(bool b);
    void manageKit();

    void reportIssues(int index);
    QPair<Task::TaskType, QString> findIssues(const BuildInfoStore &store);
    void clear();
    void updateDefaultBuildDirectories();

    bool m_isValid = false;
    Kit *m_kit;
    Utils::FilePath m_projectPath;
    bool m_haveImported = false;
    Utils::DetailsWidget *m_detailsWidget;
    QPushButton *m_manageButton;
    QGridLayout *m_newBuildsLayout;

    std::vector<BuildInfoStore> m_infoStore;

    Utils::Guard m_ignoreChanges;
    int m_selected = 0; // Number of selected "buildconfigurations"
};

} // namespace Internal
} // namespace ProjectExplorer
