#include "sharedialog.h"

#include <services/cloudservice.h>
#include <utils/gui.h>

#include <QDebug>

#include "ui_sharedialog.h"

ShareDialog::ShareDialog(const Note &note, QWidget *parent)
    : MasterDialog(parent), ui(new Ui::ShareDialog) {
    ui->setupUi(this);
    afterSetupUI();
    this->note = note;

    // update the share link checkbox and link url line edit
    updateDialog();
}

ShareDialog::~ShareDialog() { delete ui; }

/**
 * Updates the share link checkbox and link url line edit
 */
void ShareDialog::updateDialog() {
    note.refetch();
    qDebug() << __func__ << " - 'note': " << note;

    const QSignalBlocker blocker(ui->linkCheckBox);
    Q_UNUSED(blocker)
    const QSignalBlocker blocker2(ui->linkUrlLineEdit);
    Q_UNUSED(blocker2)
    const QSignalBlocker blocker3(ui->editCheckBox);
    Q_UNUSED(blocker3)

    ui->linkCheckBox->setChecked(note.isShared());
    ui->linkUrlLineEdit->setVisible(note.isShared());
    ui->linkUrlLineEdit->setText(note.getShareUrl());
    ui->editCheckBox->setVisible(note.isShared());
    ui->editCheckBox->setChecked(note.isShareEditAllowed());
}

/**
 * Shares or removes the share from the current note
 */
void ShareDialog::on_linkCheckBox_toggled(bool checked) {
    auto *cloud = CloudService::instance();

    const QSignalBlocker blocker(ui->linkCheckBox);
    Q_UNUSED(blocker)

    // the checked status will be set by the callback function updateDialog()
    ui->linkCheckBox->setChecked(!checked);

    if (checked) {
        // share the note file
        cloud->shareNote(note, this);

        Utils::Gui::information(
            this, QString(),
            tr("Keep in mind that you still have to sync your notes with your server by using "
               "the Nextcloud / ownCloud desktop sync tool to be able to share notes with others!"),
            QStringLiteral("share-sync-information"));
    } else {
        // remove the share
        cloud->removeNoteShare(note, this);
    }
}

void ShareDialog::on_editCheckBox_toggled(bool checked) {
    auto *cloud = CloudService::instance();
    unsigned int permissions = note.getSharePermissions();

    // set 2. bit
    const uint8_t bit = 1;

    if (checked) {
        permissions |= 1UL << bit;
    } else {
        permissions &= ~(1UL << bit);
    }

    note.setSharePermissions(permissions);
    note.store();

    qDebug() << __func__ << " - 'permissions': " << permissions;

    cloud->setPermissionsOnSharedNote(note, this);
}
