/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2013  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#include "crashdialog.h"
#include <QFileDialog>
#include <QDateTime>
#include <QMessageBox>
#include <QClipboard>
#include <QProcess>
#include <QtDebug>
#include <util/util.h>
#include <util/sysinfo.h>
#include "appinfo.h"

namespace LeechCraft
{
namespace AnHero
{
namespace CrashProcess
{
	CrashDialog::CrashDialog (const AppInfo& info, QWidget *parent)
	: QDialog (parent, Qt::Window)
	, CmdLine_ (info.ExecLine_)
	{
		Ui_.setupUi (this);

		Ui_.InfoLabel_->setText (tr ("Unfortunately LeechCraft has crashed. This is the info we could collect:"));
		Ui_.TraceDisplay_->append ("=== SYSTEM INFO ===");
		Ui_.TraceDisplay_->append ("Offending signal: " + QString::number (info.Signal_));
		Ui_.TraceDisplay_->append ("App path: " + info.Path_);
		Ui_.TraceDisplay_->append ("App version: " + info.Version_);
		Ui_.TraceDisplay_->append ("Qt version (build-time): " + QString (QT_VERSION_STR));
		Ui_.TraceDisplay_->append ("Qt version (runtime): " + QString (qVersion ()));

		const auto& osInfo = Util::SysInfo::GetOSNameSplit ();
		Ui_.TraceDisplay_->append ("OS: " + osInfo.first);
		Ui_.TraceDisplay_->append ("OS version: " + osInfo.second);

		Ui_.TraceDisplay_->append ("\n\n=== BACKTRACE ===");

		Ui_.RestartBox_->setEnabled (!info.ExecLine_.isEmpty ());

		show ();
	}

	void CrashDialog::WriteTrace (const QString& filename)
	{
		QFile file (filename);
		if (!file.open (QIODevice::WriteOnly | QIODevice::Truncate))
		{
			QMessageBox::critical (this,
					"LeechCraft",
					tr ("Cannot open file: %1")
						.arg (file.errorString ()));
			return;
		}
		file.write (Ui_.TraceDisplay_->toPlainText ().toUtf8 ());
	}

	namespace
	{
		QString GetNowFilename ()
		{
			const auto& nowStr = QDateTime::currentDateTime ().toString ("yy_MM_dd-hh_mm_ss");
			return "lc_crash_" + nowStr + ".log";
		}
	}

	void CrashDialog::accept ()
	{
		auto reportsDir = Util::CreateIfNotExists ("dolozhee/crashreports");
		const auto& filename = reportsDir.absoluteFilePath (GetNowFilename ());
		WriteTrace (filename);

		QDialog::accept ();
	}

	void CrashDialog::done (int res)
	{
		if (!CmdLine_.isEmpty () &&
				Ui_.RestartBox_->checkState () == Qt::Checked)
			QProcess::startDetached ("/bin/sh", { "-c", "sleep 2; " + CmdLine_ });

		QDialog::done (res);
	}

	void CrashDialog::appendTrace (const QString& part)
	{
		Ui_.TraceDisplay_->append (part);
	}

	void CrashDialog::on_Copy__released ()
	{
		const auto& text = Ui_.TraceDisplay_->toPlainText ();
		qApp->clipboard ()->setText (text, QClipboard::Clipboard);
	}

	void CrashDialog::on_Save__released ()
	{
		const auto& filename = QFileDialog::getSaveFileName (this,
				"Save crash info",
				QDir::homePath () + "/" + GetNowFilename ());

		if (filename.isEmpty ())
			return;

		WriteTrace (filename);
	}
}
}
}
