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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use std::path::PathBuf;

use anyhow::{Context, Result};
use owo_colors::OwoColorize;

use super::Todo;
use crate::{
    config::{Config, Language},
    Cli, Command,
};

/// Function that runs the entire program. It pattern matches agains the
/// command option and performs the appropiate function for each subcommand.
pub fn tasker_run(config: &Config, args: &Cli, mut todo: Todo) -> Result<()> {
    match &args.command {
        Command::Config(cfg) => {
            Config::write_config(cfg, todo).context("Failed to change configuration.")?;

            match cfg.language {
                Language::English => {
                    println!("{}", "Configuration updated!".green());
                }
                Language::Spanish => {
                    println!("{}", "¡Configuración actualizada!".green());
                }
            }
        }

        Command::Create(task) => {
            todo.add_task(task.task.clone())
                .context("Failed to create task.")?;

            match config.language {
                Language::English => {
                    println!("Task: {} created", task.task.purple());
                }
                Language::Spanish => {
                    println!("Tarea: {} creada", task.task.purple());
                }
            }
        }

        Command::Complete(task) => {
            let mut completed_task = todo.tasks.get_mut(task.id).context("No such task exists")?;
            completed_task.done = true;

            let completed_task = completed_task.name.clone();

            todo.save().context("Failed to save tasks.yml file")?;

            match config.language {
                Language::English => {
                    println!("Task: {} completed", completed_task.green());
                }
                Language::Spanish => {
                    println!("Tarea: {} completada", completed_task.green());
                }
            }
        }

        Command::Delete(task) => {
            let deleted_task = todo.tasks.remove(task.id).name;

            todo.save().context("Failed to save tasks.yml file")?;

            match config.language {
                Language::English => {
                    println!("Task: {} deleted", deleted_task.red());
                }
                Language::Spanish => {
                    println!("Tarea: {} eliminada", deleted_task.red());
                }
            }
        }

        Command::Edit(task) => {
            let mut edited_task = todo.tasks.get_mut(task.id).context("No such task exists")?;
            edited_task.name = task.task.clone();

            todo.save().context("Failed to save tasks.yml file")?;

            match config.language {
                Language::English => {
                    println!("Task: {} edited", task.task.magenta());
                }
                Language::Spanish => {
                    println!("Tarea: {} editada", task.task.magenta());
                }
            }
        }

        Command::List => match config.language {
            Language::English => {
                println!(
                    "Good day, {}.\nHere's what you got for today!\n\n{}",
                    config.name.magenta(),
                    todo
                );
            }
            Language::Spanish => {
                println!(
                    "Buen día, {}.\n¡Esto es lo que tienes para hoy!\n\n{}",
                    config.name.magenta(),
                    todo
                )
            }
        },

        Command::Clean => {
            todo.clean_tasks().context("Failed to clean tasks.")?;

            match config.language {
                Language::English => {
                    println!("{}", "Cleaned completed tasks!".green());
                }
                Language::Spanish => {
                    println!("{}", "¡Se limpiaron las tareas completadas!".green());
                }
            }
        }

        Command::Path => {
            println!(
                "{}",
                confy::get_configuration_file_path("tasker", "tasker_cli")
                    .context("Failed to get configuration path")?
                    .to_str()
                    .context("Failed to parse configuration path")?
            );
        }
    }

    Ok(())
}

/// Returns path to the program folder. This folder is where all tasks are
/// saved.
pub fn app_directory() -> Option<PathBuf> {
    dirs::home_dir().map(|home| home.join(".tasker"))
}

/// Creates .tasker directory in user folder.
///
/// # Errors
///
/// The function returns an error if the client is not using Windows, MacOS or
/// Linux.
pub fn create_app_directory() -> Result<()> {
    let directory = app_directory().context("Not using a supported OS")?;

    std::fs::create_dir_all(directory).context("Error creating .tasker directory")?;

    Ok(())
}