1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Vento, a CLI inventory for your files.
 * Copyright (C) 2022 Lux Aliaga
 *
 * 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 <https://www.gnu.org/licenses/>.
 *
 */

use crate::{
    common,
    error::{throw_error, ErrorType},
    inv, item,
};
use anyhow::Result;
use colored::Colorize;
use std::fs;
use std::path::{Path, PathBuf};

/// Undoes the last action made by Vento using the history file located on the Vento directory
pub fn undo() -> Result<()> {
    let lastpath: PathBuf = [
        common::env_config()?.vento_dir,
        Path::new("last").to_path_buf(),
    ]
    .iter()
    .collect();

    let lastfile = fs::read_to_string(lastpath)?;

    let mut contents = vec![];

    for line in lastfile.lines() {
        contents.push(line);
    }

    if contents.len() != 4 {
        throw_error(ErrorType::InvalidHistoryLength)?;
    }

    match contents[3] {
        "take" => {
            let destpath = Path::new(contents[0]).to_path_buf();
            item::drop(&String::from(contents[1]), contents[2], destpath, false)?;
        }
        "drop" => {
            let path = vec![contents[0], contents[1]].join("/");
            item::take(&path, contents[2], false)?;
        }
        "switch" => {
            inv::switch(false)?;
        }
        _ => throw_error(ErrorType::IllegalAction)?,
    }

    println!(
        "✅ {}{}{}",
        match contents[3] {
            "take" => "Take",
            "drop" => "Drop",
            "switch" => "Switch",
            _ => "Unknown",
        }
        .bold(),
        " action undone".green(),
        match contents[3] {
            "take" => format!(
                "{}{}{}{}{}{}{}",
                " (".green(),
                contents[1].bold(),
                ", from ".green(),
                contents[0],
                " to ".green(),
                match contents[2] {
                    "active" => contents[2].green(),
                    "inactive" => contents[2].blue(),
                    _ => contents[2].red(),
                }
                .bold(),
                " slot)".green(),
            ),
            "drop" => format!(
                "{}{}{}{}{}{}{}",
                " (".green(),
                contents[1].bold(),
                ", from ".green(),
                match contents[2] {
                    "active" => contents[2].green(),
                    "inactive" => contents[2].blue(),
                    _ => contents[2].red(),
                }
                .bold(),
                " slot to ".green(),
                contents[0],
                ")".green(),
            ),
            _ => String::from(""),
        }
    );

    Ok(())
}