/***************************************************************************
 *   Copyright (C) 2005-2022 by the Quassel Project                        *
 *   devel@quassel-irc.org                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) version 3.                                           *
 *                                                                         *
 *   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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include "aliasessettingspage.h"

#include <QHeaderView>
#include <QItemSelectionModel>

#include "icon.h"

AliasesSettingsPage::AliasesSettingsPage(QWidget* parent)
    : SettingsPage(tr("IRC"), tr("Aliases"), parent)
{
    ui.setupUi(this);
    ui.newAliasButton->setIcon(icon::get("list-add"));
    ui.deleteAliasButton->setIcon(icon::get("edit-delete"));

    ui.aliasesView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui.aliasesView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui.aliasesView->setAlternatingRowColors(true);
    ui.aliasesView->setTabKeyNavigation(false);
    ui.aliasesView->setModel(&_aliasesModel);
    // ui.aliasesView->setSortingEnabled(true);
    ui.aliasesView->verticalHeader()->hide();
    ui.aliasesView->horizontalHeader()->setStretchLastSection(true);

    connect(ui.newAliasButton, &QAbstractButton::clicked, &_aliasesModel, &AliasesModel::newAlias);
    connect(ui.deleteAliasButton, &QAbstractButton::clicked, this, &AliasesSettingsPage::deleteSelectedAlias);
    connect(&_aliasesModel, &AliasesModel::configChanged, this, &AliasesSettingsPage::setChangedState);
    connect(&_aliasesModel, &AliasesModel::modelReady, this, &AliasesSettingsPage::enableDialog);

    enableDialog(_aliasesModel.isReady());
}

void AliasesSettingsPage::load()
{
    if (_aliasesModel.hasConfigChanged())
        _aliasesModel.revert();
}

void AliasesSettingsPage::defaults()
{
    _aliasesModel.loadDefaults();
}

void AliasesSettingsPage::save()
{
    if (_aliasesModel.hasConfigChanged())
        _aliasesModel.commit();
}

void AliasesSettingsPage::enableDialog(bool enabled)
{
    ui.newAliasButton->setEnabled(enabled);
    ui.deleteAliasButton->setEnabled(enabled);
    setEnabled(enabled);
}

void AliasesSettingsPage::deleteSelectedAlias()
{
    if (!ui.aliasesView->selectionModel()->hasSelection())
        return;

    _aliasesModel.removeAlias(ui.aliasesView->selectionModel()->selectedIndexes()[0].row());
}
