jobber 0.1.1-alpha

Minimalistic console work time tracker
use crate::{
    commands::{
        Cli, Result,
        edit::{
            edit_address::edit_address, edit_bank::edit_bank, edit_scheme::edit_scheme, map_empty,
        },
    },
    entities::Company,
    jobber::JobberEdit,
    print_tip,
    views::CompanyView,
};
use clap::Parser;
use color_print::cprintln;
use inquire::{Confirm, Text};

/// Edit company
#[derive(Parser, Debug)]
pub(crate) struct EditCompany {}

impl EditCompany {
    pub(crate) fn run(&self, cli: &Cli) -> Result<()> {
        let mut database = cli.open_database()?;
        loop {
            if !database.edit_company(edit_company)? {
                cprintln!("Nothing changed.");
                return Ok(());
            }
            println!();
            println!("{}", CompanyView::new(&database, cli.ansi())?);
            match Confirm::new("Is this information correct?")
                .with_default(true)
                .with_help_message("cancel change with ESC")
                .prompt_skippable()?
            {
                Some(true) => {
                    cprintln!("Successfully edited company.");
                    print_tip!(cli);
                    print_tip!(
                        cli,
                        "Use <s>jobber show company</> to see the company data."
                    );
                    print_tip!(cli, "Use <s>jobber undo</> to undo the change.");
                    cli.close_database(database)?;
                    return Ok(());
                }
                Some(false) => (),
                None => {
                    cprintln!("Database remained unchanged.");
                    return Ok(());
                }
            }
        }
    }
}

pub(crate) fn edit_company(company: &mut Company) -> crate::database::Result<bool> {
    let orig = company.clone();

    edit_address(&mut company.address)?;
    edit_bank(&mut company.bank)?;

    company.vat = map_empty(
        Text::new("Tax No. (VAT)?")
            .with_default(&orig.vat.clone().unwrap_or_default())
            .with_help_message("enter '-' to remove")
            .prompt()?,
    );

    println!();
    println!("Add optional contact information...");
    println!();
    company.email = map_empty(
        Text::new("Email Address?")
            .with_default(&orig.email.clone().unwrap_or("-".to_string()))
            .with_help_message("enter '-' to remove")
            .prompt()?,
    );
    company.phone = map_empty(
        Text::new("Phone No.?")
            .with_default(&orig.phone.clone().unwrap_or("-".to_string()))
            .with_help_message("enter '-' to remove")
            .prompt()?,
    );
    company.register = map_empty(
        Text::new("Company Registration?")
            .with_default(&orig.register.clone().unwrap_or("-".to_string()))
            .with_help_message("enter '-' to remove")
            .prompt()?,
    );

    edit_scheme(&mut company.scheme)?;

    Ok(orig != *company)
}