#!/usr/bin/perl

use lib qw(/usr/lib/libDrakX);
# i18n: IMPORTANT: to get correct namespace (transfugdrake instead of libDrakX)
BEGIN { unshift @::textdomains, 'transfugdrake' }



use standalone;
use wizards;
use interactive;
use common;
use transfugdrake;

my %distrib = distrib();
my $windows_disk;
my @linux_users;
my @windows_users;

my @windows_items;
my $linux_user;
my $windows_user;
my $files_migration_type;
my $bookmarks_migration_type;
my $mail_migration_type;
my $background_migration_type;

my @step_items = (
    "files", [ "My Documents", "My Music", "My Pictures" ],
    "bookmarks", [ "Internet Explorer", "Mozilla Firefox" ],
    "mail", [ "Outlook Express" ],
    "background", [ "Wallpaper" ],
);
my @steps;

my $icon = 'migrationtools-52';
$ugtk3::wm_icon = $icon;

my $in = 'interactive'->vnew('su');
$in->{pop_wait_messages} = 0;
my $wiz = wizards->new({
    name => N("Migration wizard"),
    pages => {
        welcome => {
            name => N("This wizard will help you to import Windows' documents and settings into your %s system.", $distrib{system}) . "\n" . N("It allows two different migration methods: you can either import all documents and settings by copying them from Windows to your home directory, or share them between both operating systems."),
            post => sub {
                my $_w = $in->wait_message(N("Please wait"), N("Detecting disks..."));
                find_disk();
                @windows_users or return 'no_windows';
                $linux_user = @linux_users == 1 && $linux_users[0];
                $windows_user = @windows_users == 1 && $windows_users[0];
                $linux_user && $windows_user ? first_step() : 'users';
            },
        },
        users => {
            name => N("Multiple users have been detected, please select a user in the list below."),
            data => [
                {
                    label => N("Windows user"),
                    type => 'combo',
                    val => \$windows_user,
                    list => \@windows_users,
                },
                {
                    label => N("Linux user"),
                    type => 'combo',
                    val => \$linux_user,
                    list => \@linux_users,
                },
            ],
            post => sub { first_step() },
        },
        files => {
            name => N("Migrate your Windows documents to your home directory. Documents can be imported by copying them, or they can be shared with the other operating system"),
            data => [
                {
                    type => 'list',
                    val => \$files_migration_type,
                    list => [ "import", if_(0, "share"), "skip" ],
                    format => sub {
                        +{
                            import => N("Import documents (recommended)"),
                            share => N("Share documents"),
                            skip => N("Skip step"),
                        }->{$_[0]} if $_[0];
                    },
                },
            ],
            post => sub {
                if ($files_migration_type eq "import") {
                    my $_w = $in->wait_message('', N("Migration of documents in progress"));
                    step_import('files');
                }
                next_step();
            },
        },
        bookmarks => {
            name => N("Migrate your browser bookmarks"),
            data => [
                {
                    type => 'list',
                    val => \$bookmarks_migration_type,
                    list => [ "import", if_(0 && member('Mozilla Firefox', @windows_items), "share"), "skip" ],
                    format => sub {
                        +{
                            import => N("Import bookmarks (recommended)"),
                            share => N("Share bookmarks"),
                            skip => N("Skip step"),
                        }->{$_[0]} if $_[0];
                    },
                },
            ],
            post => sub {
                if ($bookmarks_migration_type eq "import") {
                    my $_w = $in->wait_message('', N("Migration of bookmarks in progress"));
                    step_import('bookmarks');
                }
                next_step();
            },
        },
        mail => {
            name => N("Migrate your mail settings"),
            data => [
                { type => 'list',
                  val => \$mail_migration_type,
                  list => [ "import", if_(0 && member('Mozilla Thunderbird', @windows_items), "share"), "skip" ],
                  format => sub {
                      +{
                          import => N("Import mail (recommended)"),
                          share => N("Share mail"),
                          skip => N("Skip step"),
                      }->{$_[0]} if $_[0],
                  },
              },
            ],
            post => sub {
                if ($mail_migration_type eq 'import') {
                    my $_w = $in->wait_message('', N("Migration of mail in progress"));
                    step_import('mail');
                }
                next_step();
            },
        },
        background => {
            name => N("Migrate your desktop background"),
            data => [
                { type => 'list',
                  val => \$background_migration_type,
                  list => [ "skip", "import" ],
                  format => sub {
                      +{
                          "skip" => N("Use Mageia background"),
                          "import" => N("Import background"),
                      }->{$_[0]} if $_[0];
                  },
              },
            ],
            post => sub {
                if ($background_migration_type eq 'import') {
                    my $_w = $in->wait_message('', N("Migration of background in progress"));
                   step_import('background');
                }
                next_step();
            },
        },
        end => {
            name => N("Congratulations, your migration is now complete!"),
            end => 1,
        },
        no_windows => {
            name => N("No Windows installation has been detected."),
            end => 1,
        },
        nothing => {
            name => N("No documents and settings have been detected."),
            end => 1,
        },
    },
});

$wiz->safe_process($in);

sub find_disk {
    $windows_disk = transfugdrake::get_windows_disk();
    @linux_users = uniq(list_users());
    @windows_users = transfugdrake::list_windows_users($windows_disk) if $windows_disk;
}

sub first_step() {
    #- make sure XDG directories are created
    system("su - $linux_user -c /usr/bin/xdg-user-dirs-update");

    @windows_items = transfugdrake::list_windows_items($windows_disk, $windows_user);
    @steps = map { $_->[0] } grep { intersection(\@windows_items, $_->[1]) } group_by2(@step_items);
    @steps ? next_step() : 'nothing';
}

sub next_step() { shift(@steps) || 'end' }

sub step_import {
    my ($stepname) = @_;
    my $step = find { $_->[0] eq $stepname } group_by2(@step_items);
    my @targets = intersection(\@windows_items, $step->[1]);
    transfugdrake::import_target($windows_disk, $windows_user, $linux_user, $_) foreach @targets;
}
