/***************************************************************************
                             kbrokeragepage.cpp
                             -------------------
    begin                : Tue Sep 25 2006
    copyright            : (C) 2007 Thomas Baumgart
    email                : Thomas Baumgart <ipwizard@users.sourceforge.net>
                           (C) 2017 by Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "kbrokeragepage.h"
#include "kbrokeragepage_p.h"

// ----------------------------------------------------------------------------
// QT Includes

#include <QCheckBox>
#include <QLabel>

// ----------------------------------------------------------------------------
// KDE Includes

#include <KLineEdit>

// ----------------------------------------------------------------------------
// Project Includes

#include "ui_kbrokeragepage.h"

#include "kmymoneycurrencyselector.h"
#include "knewaccountwizard.h"
#include "knewaccountwizard_p.h"
#include "kaccounttypepage.h"
#include "kaccounttypepage_p.h"
#include "khierarchypage.h"
#include "kinstitutionpage.h"
#include "kinstitutionpage_p.h"
#include "mymoneyfile.h"
#include "mymoneysecurity.h"
#include "wizardpage.h"

namespace NewAccountWizard
{
  BrokeragePage::BrokeragePage(Wizard* wizard) :
    QWidget(wizard),
    WizardPage<Wizard>(*new BrokeragePagePrivate(wizard), StepBroker, this, wizard)
  {
    Q_D(BrokeragePage);
    d->ui->setupUi(this);
    connect(MyMoneyFile::instance(), &MyMoneyFile::dataChanged, this, &BrokeragePage::slotLoadWidgets);
  }

  BrokeragePage::~BrokeragePage()
  {
  }

  void BrokeragePage::slotLoadWidgets()
  {
    Q_D(BrokeragePage);
    d->ui->m_brokerageCurrency->update(QString("x"));
  }

  void BrokeragePage::enterPage()
  {
    Q_D(BrokeragePage);
    // assign the currency of the investment account to the
    // brokerage account if nothing else has ever been selected
    if (d->ui->m_brokerageCurrency->security().id().isEmpty()) {
        d->ui->m_brokerageCurrency->setSecurity(d->m_wizard->d_func()->m_accountTypePage->d_func()->ui->m_currencyComboBox->security());
      }

    // check if the institution relevant fields should be enabled or not
    bool enabled = d->m_wizard->d_func()->m_institutionPage->d_func()->ui->m_accountNumber->isEnabled();
    d->ui->m_accountNumberLabel->setEnabled(enabled);
    d->ui->m_accountNumber->setEnabled(enabled);
    d->ui->m_ibanLabel->setEnabled(enabled);
    d->ui->m_iban->setEnabled(enabled);
  }

  QWidget* BrokeragePage::initialFocusWidget() const
  {
    Q_D(const BrokeragePage);
    return d->ui->m_createBrokerageButton;
  }

  KMyMoneyWizardPage* BrokeragePage::nextPage() const
  {
    Q_D(const BrokeragePage);
    return d->m_wizard->d_func()->m_hierarchyPage;
  }
}
