/*
 * Copyright (C) 2014-2026 CZ.NIC
 *
 * This program 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.
 *
 * This program 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations including
 * the two.
 */

#pragma once

#include <QDialog>
#include <QList>
#include <QString>
#include <QStringList>

#include "src/models/backup_selection_model.h"
#include "src/models/sort_filter_proxy_model.h"

class AcntId; /* Forward declaration. */
class MainWindow; /* Forward declaration. */

namespace Ui {
	class DlgConvertImportFromMobileApp;
}

/*!
 * @brief Dialogue for message database conversions and import.
 */
class DlgConvertImportFromMobileApp : public QDialog {
	Q_OBJECT

public:
	/*!
	 * @brief Destructor.
	 */
	~DlgConvertImportFromMobileApp(void);

	/*!
	 * @brief View the dialogue.
	 *
	 * @param[in] mw Pointer to main window.
	 * @param[in] parent Parent widget.
	 */
	static
	void view(MainWindow *mw, QWidget *parent = Q_NULLPTR);

protected:
	/*!
	 * @brief Check window geometry and set table columns width.
	 *
	 * @param[in] event Widget show event.
	 */
	virtual
	void showEvent(QShowEvent *event) Q_DECL_OVERRIDE;

private slots:
	/*!
	 * @brief Change selection according to all checkbox state.
	 *
	 * @param[in] state Selection state.
	 */
	void applyAllSelection(int state);

	/*!
	 * @brief Filter account list.
	 *
	 * @param[in] text Sought text.
	 */
	void filterAccount(const QString &text);

	/*!
	 * @brief Update dialogue when account selection changes.
	 */
	void modifiedAccountSelection(void);

	/*!
	 * @brief Open zip archive.
	 */
	void openZipPackage(void);

	/*!
	 * @brief Unzip and import messages.
	 */
	void unzipAndImportMsgs(void);

private:
	/*!
	 * @brief Constructor.
	 *
	 * @param[in] mw Pointer to main window.
	 * @param[in] parent Parent widget.
	 */
	DlgConvertImportFromMobileApp(MainWindow *mw,
	    QWidget *parent = Q_NULLPTR);

	/*!
	 * @brief Unzip selected account files from archive.
	 *
	 * @param[in] zipPath Path to zip archive.
	 * @param[in] unZipPath Path to unzipped files.
	 * @param[in] selectedAcnts Account list.
	 * @return List of paths to unzip message databases.
	 */
	QStringList unZipSelectedAcnts(const QString &zipPath,
	    const QString &unZipPath, const QList<AcntId> &selectedAcnts);

	/*!
	 * @brief Import messages or create accounts from databases.
	 *
	 * @param[in] dbPathList List of message database paths.
	 * @param[in] unZipPath Path to unzipped files.
	 * @return True if success.
	 */
	bool importAccountsFromDbs(const QStringList &dbPathList,
	    const QString &unZipPath);

	/*!
	 * @brief Create account from databases.
	 *
	 * @param[in] acntId Account id.
	 * @param[in] dbPathList List of message database paths.
	 * @param[in] unZipPath Path to unzipped files.
	 * @param[out] errTxt Error text.
	 * @return True if success.
	 */
	bool createAccountFromDb(const AcntId &acntId,
	    const QStringList &dbPathList, const QString &unZipPath,
	    QString &errTxt);

	Ui::DlgConvertImportFromMobileApp *m_ui; /*!< UI generated from UI file. */

	MainWindow *m_mw; /*!< Pointer to main window. */

	QString m_zipPath; /*!< ZIP file path. */

	SortFilterProxyModel m_accountListProxyModel; /*!<
	                                               * Used for account
	                                               * sorting and filtering.
	                                               */
	QString m_dfltFilerLineStyleSheet; /*!< Used to remember default line edit style. */
	BackupSelectionModel m_accountSelectionModel; /*!< Choose account data to scan. */
};
