#include "mainwindow.h" // Notepad class header file that was generated by the wizard
#include "ui_mainwindow.h"// UI header file that was generated by the uic tool
#include "login.h"
#include "universalFunctions.h"
#include <QFileDialog>
#include <QMessageBox>
#include <iostream>
#include <QJsonObject>
#include <QJsonDocument>
#include <QTextStream>
#include <QJsonArray>

MainWindow::MainWindow(QWidget *parent) // class definition, constructor
    : QMainWindow(parent) // calls qmainwindow parent constructor we have,
    // base class for the MainWindow class:
    , ui(new Ui::MainWindow) // create ui class and assign to ui member
    // creates the UI class instance and assigns it to the ui member:
{
    ui->setupUi(this); // setup user interface
    defineConnections();
}

void MainWindow::defineConnections() const
{
    connect(ui -> loginButton, &QPushButton::clicked, this, &MainWindow::goToLogin);
    connect(ui -> pushButton, &QPushButton::clicked, this, &MainWindow::saveRegisteredUser);
    connect(ui -> actionExit, &QAction::triggered, this, &MainWindow::exit);
    connect(ui -> actionLogin, &QAction::triggered, this, &MainWindow::goToLogin);
}

void MainWindow::exit() const
{
    QApplication::quit();
}

MainWindow::~MainWindow()
{
    delete ui; // in the destructor, we delete the ui
}

QJsonObject MainWindow::createUserObject(const QString &mail, const QString &password, const QString &userId) const
{
    QJsonObject user;
    user["userId"] = userId;
    // userId - unique user id - text obtained from the user during user registration
    user["email"] = mail;
    // email - e-mail address of the user
    user["password"] = password;
    // password - password provided by the user
    return user;
}

QJsonObject MainWindow::insertUserObject(QJsonObject &users, const QString &id)
{
    QString mail = ui->inputMail->text();
    QString password = ui->inputPassword->text();
    QJsonObject user = createUserObject(mail, password, id);
    users.insert(id, user);
    return users;
}

QJsonObject MainWindow::createBlogObject(const QString &id, const QString &ownerId) const
{
    QJsonObject blog;
    blog["blogId"] = id;
    QString title = ui -> inputBlogTitle -> text();
    blog["title"] = title;
    blog["ownerId"] = ownerId;
    QJsonArray items;
    blog["items"] = items;
    return blog;
}


QJsonObject MainWindow::insertBlogObject(QJsonObject &blogs, const QString &blogId, const QString &ownerId)
{
    QJsonObject blog = createBlogObject(blogId, ownerId);
    blogs.insert(blogId, blog);
    return blogs;
}


void MainWindow::saveFiles(QJsonObject &users, QJsonObject &blogs)
{
    saveJsonFile(blogs, "blogs.json");
    saveJsonFile(users, "user.json");
}

void MainWindow::registerUser(QJsonObject &users, QJsonObject &blogs, const QString &id, const QString &blogId)
{
    users = insertUserObject(users, id);
    blogs = insertBlogObject(blogs, blogId, id);
    saveFiles(users, blogs);
}

void MainWindow::saveRegisteredUser()
{
    QJsonObject blogs = readJsonFile("blogs.json");
    QJsonObject users = readJsonFile("user.json");

    QString id = ui->inputId->text();
    if (stringEmpty(id, "THIS USER ID IS EMPTY!")) return;
    if (idExists(id, users, "THIS USER ID IS ALREADY TAKEN!", true)) return;

    QString blogId = ui -> inputBlogID->text();
    if (stringEmpty(blogId, "THIS BLOG ID IS EMPTY!")) return;
    if (idExists(blogId, blogs, "THIS BLOG ID IS ALREADY TAKEN!", true)) return;

    registerUser(users, blogs, id, blogId);
}

void MainWindow::goToLogin()
{
    login *l = new login();
    l -> show();
    hide();
}




