/*
 * pluma-backup-recovery-dialog.c
 * This file is part of pluma
 *
 * Copyright (C) 2024 - MATE Developers
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib/gi18n.h>
#include <gtk/gtk.h>

#include "pluma-backup-recovery-dialog.h"
#include "pluma-utils.h"

struct _PlumaBackupRecoveryDialogPrivate
{
    GtkWidget *message_label;
    GtkWidget *backup_info_label;
    GtkWidget *original_info_label;
    
    gchar *filename;
    gchar *original_uri;
    GFileInfo *backup_info;
    GFileInfo *original_info;
};

G_DEFINE_TYPE_WITH_PRIVATE (PlumaBackupRecoveryDialog, pluma_backup_recovery_dialog, GTK_TYPE_DIALOG)

static void
pluma_backup_recovery_dialog_finalize (GObject *object)
{
    PlumaBackupRecoveryDialog *dialog = PLUMA_BACKUP_RECOVERY_DIALOG (object);
    
    g_free (dialog->priv->filename);
    g_free (dialog->priv->original_uri);
    
    if (dialog->priv->backup_info)
        g_object_unref (dialog->priv->backup_info);
    
    if (dialog->priv->original_info)
        g_object_unref (dialog->priv->original_info);
    
    G_OBJECT_CLASS (pluma_backup_recovery_dialog_parent_class)->finalize (object);
}

static gchar *
format_file_time (GFileInfo *info)
{
    GDateTime *datetime;
    gchar *formatted_time;
    guint64 mtime;
    
    if (!info || !g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_TIME_MODIFIED))
        return g_strdup (_("未知"));
    
    mtime = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
    datetime = g_date_time_new_from_unix_local (mtime);
    
    if (datetime)
    {
        formatted_time = g_date_time_format (datetime, "%Y-%m-%d %H:%M:%S");
        g_date_time_unref (datetime);
    }
    else
    {
        formatted_time = g_strdup (_("无效时间"));
    }
    
    return formatted_time;
}

static gchar *
format_file_size (GFileInfo *info)
{
    goffset size;
    
    if (!info || !g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
        return g_strdup (_("未知"));
    
    size = g_file_info_get_size (info);
    return g_format_size (size);
}

static void
setup_dialog_content (PlumaBackupRecoveryDialog *dialog)
{
    GtkWidget *content_area;
    GtkWidget *main_vbox;
    GtkWidget *hbox;
    GtkWidget *image;
    GtkWidget *vbox;
    GtkWidget *info_frame;
    GtkWidget *info_vbox;
    gchar *markup;
    gchar *backup_time_str;
    gchar *original_time_str;
    gchar *backup_size_str;
    gchar *original_size_str;
    
    content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
    
    main_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
    gtk_widget_set_margin_start (main_vbox, 12);
    gtk_widget_set_margin_end (main_vbox, 12);
    gtk_widget_set_margin_top (main_vbox, 12);
    gtk_widget_set_margin_bottom (main_vbox, 12);
    gtk_container_add (GTK_CONTAINER (content_area), main_vbox);
    
    /* 主要消息行 */
    hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
    gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
    
    /* 警告图标 */
    image = gtk_image_new_from_icon_name ("dialog-warning", GTK_ICON_SIZE_MENU);
    gtk_widget_set_valign (image, GTK_ALIGN_START);
    gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
    
    /* 消息文本 */
    vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
    gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
    
    dialog->priv->message_label = gtk_label_new (NULL);
    markup = g_markup_printf_escaped ("<span size=\"larger\" weight=\"bold\">发现备份文件</span>\n\n"
                                       "文件 \"%s\" 有一个备份文件，可能是上次保存时意外中断造成的。\n"
                                       "您希望恢复备份文件吗？",
                                       dialog->priv->filename);
    gtk_label_set_markup (GTK_LABEL (dialog->priv->message_label), markup);
    gtk_label_set_line_wrap (GTK_LABEL (dialog->priv->message_label), TRUE);
    gtk_label_set_max_width_chars (GTK_LABEL (dialog->priv->message_label), 80);
    gtk_widget_set_halign (dialog->priv->message_label, GTK_ALIGN_START);
    gtk_box_pack_start (GTK_BOX (vbox), dialog->priv->message_label, FALSE, FALSE, 0);
    g_free (markup);
    
    /* 文件信息框架 */
    info_frame = gtk_frame_new (_("文件信息"));
    gtk_box_pack_start (GTK_BOX (main_vbox), info_frame, FALSE, FALSE, 0);
    
    info_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
    gtk_widget_set_margin_start (info_vbox, 12);
    gtk_widget_set_margin_end (info_vbox, 12);
    gtk_widget_set_margin_top (info_vbox, 6);
    gtk_widget_set_margin_bottom (info_vbox, 6);
    gtk_container_add (GTK_CONTAINER (info_frame), info_vbox);
    
    /* 备份文件信息 */
    backup_time_str = format_file_time (dialog->priv->backup_info);
    backup_size_str = format_file_size (dialog->priv->backup_info);
    
    dialog->priv->backup_info_label = gtk_label_new (NULL);
    markup = g_markup_printf_escaped ("<b>备份文件:</b>\n时间: %s\n大小: %s",
                                       backup_time_str, backup_size_str);
    gtk_label_set_markup (GTK_LABEL (dialog->priv->backup_info_label), markup);
    gtk_widget_set_halign (dialog->priv->backup_info_label, GTK_ALIGN_START);
    gtk_box_pack_start (GTK_BOX (info_vbox), dialog->priv->backup_info_label, FALSE, FALSE, 0);
    g_free (markup);
    g_free (backup_time_str);
    g_free (backup_size_str);
    
    /* 原文件信息 */
    if (dialog->priv->original_info)
    {
        original_time_str = format_file_time (dialog->priv->original_info);
        original_size_str = format_file_size (dialog->priv->original_info);
        
        dialog->priv->original_info_label = gtk_label_new (NULL);
        markup = g_markup_printf_escaped ("<b>原文件:</b>\n时间: %s\n大小: %s",
                                           original_time_str, original_size_str);
        gtk_label_set_markup (GTK_LABEL (dialog->priv->original_info_label), markup);
        gtk_widget_set_halign (dialog->priv->original_info_label, GTK_ALIGN_START);
        gtk_box_pack_start (GTK_BOX (info_vbox), dialog->priv->original_info_label, FALSE, FALSE, 0);
        g_free (markup);
        g_free (original_time_str);
        g_free (original_size_str);
    }
    
    gtk_widget_show_all (main_vbox);
}

static void
pluma_backup_recovery_dialog_init (PlumaBackupRecoveryDialog *dialog)
{
    dialog->priv = pluma_backup_recovery_dialog_get_instance_private (dialog);
    
    gtk_window_set_title (GTK_WINDOW (dialog), _("备份恢复"));
    gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
    gtk_dialog_set_default_response (GTK_DIALOG (dialog), PLUMA_BACKUP_RECOVERY_RESPONSE_RECOVER);
    
    /* 添加按钮 */
    pluma_dialog_add_button (GTK_DIALOG (dialog), _("取消"), "process-stop", GTK_RESPONSE_CANCEL);
    pluma_dialog_add_button (GTK_DIALOG (dialog), _("使用原文件"), "document-open", PLUMA_BACKUP_RECOVERY_RESPONSE_IGNORE);
    pluma_dialog_add_button (GTK_DIALOG (dialog), _("恢复备份"), "edit-undo", PLUMA_BACKUP_RECOVERY_RESPONSE_RECOVER);
}

static void
pluma_backup_recovery_dialog_class_init (PlumaBackupRecoveryDialogClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
    
    object_class->finalize = pluma_backup_recovery_dialog_finalize;
}

PlumaBackupRecoveryDialog *
pluma_backup_recovery_dialog_new (GtkWindow   *parent,
                                  const gchar *filename,
                                  const gchar *original_uri,
                                  GFileInfo   *backup_info,
                                  GFileInfo   *original_info)
{
    PlumaBackupRecoveryDialog *dialog;
    
    g_return_val_if_fail (filename != NULL, NULL);
    g_return_val_if_fail (original_uri != NULL, NULL);
    g_return_val_if_fail (backup_info != NULL, NULL);
    
    dialog = g_object_new (PLUMA_TYPE_BACKUP_RECOVERY_DIALOG, NULL);
    
    if (parent != NULL)
    {
        gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
        gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
        gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
    }
    
    dialog->priv->filename = g_strdup (filename);
    dialog->priv->original_uri = g_strdup (original_uri);
    dialog->priv->backup_info = g_object_ref (backup_info);
    
    if (original_info)
        dialog->priv->original_info = g_object_ref (original_info);
    
    setup_dialog_content (dialog);
    
    return dialog;
}

PlumaBackupRecoveryResponse
pluma_backup_recovery_dialog_run (PlumaBackupRecoveryDialog *dialog)
{
    gint response;
    
    g_return_val_if_fail (PLUMA_IS_BACKUP_RECOVERY_DIALOG (dialog), PLUMA_BACKUP_RECOVERY_RESPONSE_CANCEL);
    
    response = gtk_dialog_run (GTK_DIALOG (dialog));
    
    switch (response)
    {
        case PLUMA_BACKUP_RECOVERY_RESPONSE_RECOVER:
            return PLUMA_BACKUP_RECOVERY_RESPONSE_RECOVER;
        case PLUMA_BACKUP_RECOVERY_RESPONSE_IGNORE:
            return PLUMA_BACKUP_RECOVERY_RESPONSE_IGNORE;
        default:
            return PLUMA_BACKUP_RECOVERY_RESPONSE_CANCEL;
    }
} 