#include "SettingsWindow.h"
#include <QGridLayout>
#include <QLabel>
#include <QVBoxLayout>
#include "SMSClient.h"


CSettingsWindow::CSettingsWindow()
{
    resize(600,600);

    QVBoxLayout *MainLayout = new QVBoxLayout;
    QWidget *mPagesContainer = new QWidget(this);

    mOptionsTabBar = new QTabBar();
    mOptionsTabBar->addTab("SMS");
    mOptionsTabBar->addTab("Network");
    mOptionsTabBar->resize(this->geometry().width(),mOptionsTabBar->height());
    MainLayout->addWidget(mOptionsTabBar);

    mSMSPage = new QWidget(mPagesContainer);
 //   mVoipPage->resize(this->size());

    QGridLayout *mSMSPageLayout = new QGridLayout;


    QLabel *Label;
    mVoipMSUsername = new QLineEdit;
    Label = new QLabel("Voip.ms Username: ");
    mSMSPageLayout->addWidget(Label,0,1,1,1);
    mSMSPageLayout->addWidget(mVoipMSUsername,0,2,1,2/*,Qt::AlignLeft*/);

    Label = new QLabel("Voip.ms Password: ");
    mVoipMSPassword = new QLineEdit;
    mVoipMSPassword->setEchoMode(QLineEdit::Password);
    mSMSPageLayout->addWidget(Label,1,1,1,1);
    mSMSPageLayout->addWidget(mVoipMSPassword,1,2,1,1/*,Qt::AlignLeft*/);

    mRetreiveDIDSButton = new QPushButton("Get DIDs");
    connect(mRetreiveDIDSButton,SIGNAL(clicked(bool)),this,SLOT(RetreiveDIDButtonClicked(bool)));
    mSMSPageLayout->addWidget(mRetreiveDIDSButton,1,3,1,1);


    Label = new QLabel("DID:");
    mDIDSelectionDropList = new QComboBox();
    mSMSPageLayout->addWidget(Label,2,1,1,1);
    mSMSPageLayout->addWidget(mDIDSelectionDropList,2,2,1,1);


    mVoipMSCalendar = new QCalendarWidget;
    Label = new QLabel("SMS retreival stard date:");
    mSMSPageLayout->addWidget(Label,3,1,1,1);
    mSMSPageLayout->addWidget(mVoipMSCalendar,4,1,1,4);

    mDoneButton = new QPushButton("Done");
    MainLayout->addWidget(mDoneButton);
    connect(mDoneButton,SIGNAL(clicked(bool)),this,SLOT(DoneButtonClicked(bool)));




    mSMSPage->setLayout(mSMSPageLayout);
    mSMSPage->show();

    MainLayout->addWidget(mPagesContainer);
    setLayout(MainLayout);

    connect(mOptionsTabBar,SIGNAL(currentChanged(int)),this,SLOT(TabBarClicked(int)));

}

CSettingsWindow::~CSettingsWindow()
{

}

void CSettingsWindow::TabBarClicked(int TabIndex)
{
    if(TabIndex == 0)//SMS
    {
        mSMSPage->show();


    }
    else if(TabIndex == 1)//Network
    {
        mSMSPage->hide();

    }
}

void CSettingsWindow::RetreiveDIDButtonClicked(bool checked)
{
    Q_UNUSED(checked)

    if(mVoipMSUsername->text().isEmpty() || mVoipMSPassword->text().isEmpty())
    {
        return;
    }



    qDebug("CLick!");
}

void CSettingsWindow::DIDsListFetched(QStringList DIDs)
{
    if(DIDs.size() == 0)
    {

    }
    else
    {
        mDIDSelectionDropList->clear();
        for(int i = 0; i < DIDs.size(); i++)
        {
            mDIDSelectionDropList->addItem(DIDs.at(i));
        }
    }
}

unsigned int CSettingsWindow::SetSettingsData(CSettings *SettingsData)
{
    mVoipMSPassword->setText(SettingsData->mVoipMSSettings.mPassword);
    mVoipMSUsername->setText(SettingsData->mVoipMSSettings.mUsername);
    mDIDSelectionDropList->clear();
    mDIDSelectionDropList->addItem(SettingsData->mVoipMSSettings.mDefaultDID);
    mVoipMSCalendar->setSelectedDate(SettingsData->mVoipMSSettings.mStartDate);
    return RET_OK;
}

void CSettingsWindow::DoneButtonClicked(bool checked)
{
    Q_UNUSED(checked)

    CSettings *Settings = new CSettings;
    Settings->mVoipMSSettings.mDefaultDID = mDIDSelectionDropList->currentText();
    Settings->mVoipMSSettings.mPassword = mVoipMSPassword->text();
    Settings->mVoipMSSettings.mStartDate = mVoipMSCalendar->selectedDate();
    Settings->mVoipMSSettings.mUsername = mVoipMSUsername->text();

    mProgramHandle->SaveSettings(Settings);
    mProgramHandle->SettingsWindowClosed();
    delete Settings;

    this->hide();
}
