/*
This file is part of Telegram Desktop,
the official desktop version of Telegram messaging app, see https://telegram.org

Telegram Desktop 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 3 of the License, or
(at your option) any later version.

It is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

In addition, as a special exception, the copyright holders give permission
to link the code of portions of this program with the OpenSSL library.

Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
*/
#include "stdafx.h"
#include "boxes/downloadpathbox.h"

#include "lang.h"
#include "localstorage.h"
#include "ui/filedialog.h"
#include "ui/widgets/checkbox.h"
#include "ui/widgets/buttons.h"
#include "pspecific.h"
#include "styles/style_boxes.h"

DownloadPathBox::DownloadPathBox() : AbstractBox()
, _path(Global::DownloadPath())
, _pathBookmark(Global::DownloadPathBookmark())
, _default(this, qsl("dir_type"), 0, lang(lng_download_path_default_radio), _path.isEmpty())
, _temp(this, qsl("dir_type"), 1, lang(lng_download_path_temp_radio), _path == qsl("tmp"))
, _dir(this, qsl("dir_type"), 2, lang(lng_download_path_dir_radio), !_path.isEmpty() && _path != qsl("tmp"))
, _pathLink(this, QString(), st::boxLinkButton)
, _save(this, lang(lng_connection_save), st::defaultBoxButton)
, _cancel(this, lang(lng_cancel), st::cancelBoxButton) {
	setTitleText(lang(lng_download_path_header));

	connect(_save, SIGNAL(clicked()), this, SLOT(onSave()));
	connect(_cancel, SIGNAL(clicked()), this, SLOT(onClose()));

	connect(_default, SIGNAL(changed()), this, SLOT(onChange()));
	connect(_temp, SIGNAL(changed()), this, SLOT(onChange()));
	connect(_dir, SIGNAL(changed()), this, SLOT(onChange()));

	connect(_pathLink, SIGNAL(clicked()), this, SLOT(onEditPath()));
	if (!_path.isEmpty() && _path != qsl("tmp")) {
		setPathText(QDir::toNativeSeparators(_path));
	}
	updateControlsVisibility();

	prepare();
}

void DownloadPathBox::updateControlsVisibility() {
	_pathLink->setVisible(_dir->checked());

	int32 h = titleHeight() + st::boxOptionListPadding.top() + _default->height() + st::boxOptionListPadding.top() + _temp->height() + st::boxOptionListPadding.top() + _dir->height();
	if (_dir->checked()) h += st::downloadPathSkip + _pathLink->height();
	h += st::boxOptionListPadding.bottom() + st::boxButtonPadding.top() + _save->height() + st::boxButtonPadding.bottom();

	setMaxHeight(h);
}

void DownloadPathBox::resizeEvent(QResizeEvent *e) {
	_default->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), titleHeight() + st::boxOptionListPadding.top());
	_temp->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), _default->y() + _default->height() + st::boxOptionListPadding.top());
	_dir->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), _temp->y() + _temp->height() + st::boxOptionListPadding.top());
	int32 inputx = st::boxPadding.left() + st::boxOptionListPadding.left() + st::defaultRadiobutton.textPosition.x();
	int32 inputy = _dir->y() + _dir->height() + st::downloadPathSkip;

	_pathLink->moveToLeft(inputx, inputy);

	_save->moveToRight(st::boxButtonPadding.right(), height() - st::boxButtonPadding.bottom() - _save->height());
	_cancel->moveToRight(st::boxButtonPadding.right() + _save->width() + st::boxButtonPadding.left(), _save->y());
	AbstractBox::resizeEvent(e);
}

void DownloadPathBox::onChange() {
	if (_dir->checked()) {
		if (_path.isEmpty() || _path == qsl("tmp")) {
			(_path.isEmpty() ? _default : _temp)->setChecked(true);
			onEditPath();
			if (!_path.isEmpty() && _path != qsl("tmp")) {
				_dir->setChecked(true);
			}
		} else {
			setPathText(QDir::toNativeSeparators(_path));
		}
	} else if (_temp->checked()) {
		_path = qsl("tmp");
	} else {
		_path = QString();
	}
	updateControlsVisibility();
	update();
}

void DownloadPathBox::onEditPath() {
	filedialogInit();
	QString path, lastPath = cDialogLastPath();
	if (!Global::DownloadPath().isEmpty() && Global::DownloadPath() != qstr("tmp")) {
		cSetDialogLastPath(Global::DownloadPath().left(Global::DownloadPath().size() - (Global::DownloadPath().endsWith('/') ? 1 : 0)));
	}
	if (filedialogGetDir(path, lang(lng_download_path_choose))) {
		if (!path.isEmpty()) {
			_path = path + '/';
			_pathBookmark = psDownloadPathBookmark(_path);
			setPathText(QDir::toNativeSeparators(_path));
		}
	}
	cSetDialogLastPath(lastPath);
}

void DownloadPathBox::onSave() {
	Global::SetDownloadPath(_default->checked() ? QString() : (_temp->checked() ? qsl("tmp") : _path));
	Global::SetDownloadPathBookmark((_default->checked() || _temp->checked()) ? QByteArray() : _pathBookmark);
	Local::writeUserSettings();
	Global::RefDownloadPathChanged().notify();
	onClose();
}

void DownloadPathBox::setPathText(const QString &text) {
	int32 availw = st::boxWideWidth - st::boxPadding.left() - st::defaultRadiobutton.textPosition.x() - st::boxPadding.right();
	_pathLink->setText(st::boxTextFont->elided(text, availw));
}