#include "Nickname.h"

#include <iostream>

#include <QApplication>
#include <QPushButton>
#include <QLineEdit>
#include <QFormLayout>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QComboBox>

Nickname::Nickname(QWidget *parent) : QDialog(parent) {
    userConection = 0;
}

int Nickname::run() {
    QFormLayout *layoutPrincipal = new QFormLayout;
    layoutIp = new QVBoxLayout;
    QHBoxLayout *layoutBotones = new QHBoxLayout;
    botonAceptar = new QPushButton(trUtf8("Aceptar"));
    botonAceptar->setStyleSheet(QString::fromUtf8("background-color: rgb(0, 85, 127);\n"
        "color: rgb(255, 255, 255);"));
    botonCancelar = new QPushButton(trUtf8("Cancelar"));
    botonCancelar->setStyleSheet(QString::fromUtf8("background-color: rgb(0, 85, 127);\n"
        "color: rgb(255, 255, 255);"));
    campoUsuario = new QLineEdit();
    serverIp = new QLineEdit();
    serverOLocal = new QComboBox();
    serverOLocal->addItem("Local");
    serverOLocal->addItem("Server");
    this->setWindowTitle("Inicio Juego Trivia");

    serverIp->setDisabled(true);

    layoutBotones->addStretch();
    layoutBotones->addWidget(botonAceptar);
    layoutBotones->addWidget(botonCancelar);

    layoutIp->addWidget(serverIp);

    layoutPrincipal->addRow(trUtf8("Nickname"), campoUsuario);
    layoutPrincipal->addRow(trUtf8("Connection"), serverOLocal);
    layoutPrincipal->addRow(trUtf8("Ip"), layoutIp);
    layoutPrincipal->addRow(layoutBotones);

    setLayout(layoutPrincipal);

    connect(botonAceptar, SIGNAL(clicked()),
    this, SLOT(verific()));

    connect(botonCancelar, SIGNAL(clicked()),
            this, SLOT(close()));

    connect(serverOLocal, SIGNAL(currentTextChanged(QString)),
    this, SLOT(changeToServer()));
    this->setStyleSheet(QString::fromUtf8("background-color: rgb(170, 170, 127);\n"
                                          "color: rgb(0, 0, 0);\n"
                                          "font: 8pt \"Trebuchet MS\";"));
    this->show();
    QDialog::exec();
    return userConection;
}

void Nickname::changeToServer() {
    if(serverOLocal->currentText() == "Server"){
        serverIp->setEnabled(true);
        //std::cout << "Server" << std::endl;
    }
    else {
        serverIp->setDisabled(true);
        serverIp->setText("");
        //std::cout << "Local" << std::endl;
    }
}

void Nickname::verific() {
    if(serverOLocal->currentText() == "Local") {
        userConection = 1;
        QMessageBox::information(this, "", trUtf8("Conexion Local"));
        //QApplication::quit();
        QDialog::close();
    }
    else {
        userConection = 2;
        QMessageBox::information(this, "", trUtf8("Conexion Servidor"));
        //QApplication::quit();
        QDialog::close();
    }
}
