use crate::domain::usecases;

use super::{UJMTError, read};

use clap::{Arg, Command, ArgMatches, ArgAction};

pub fn init() -> Command {
    Command::new("validate")
        .about("Validate Drawio XML file for BPMN generation")
        .arg(
            Arg::new("file")
                .help("Drawio XML file")
                .action(ArgAction::Set)
        )
}

pub fn exec(args: &ArgMatches) -> Result<(), UJMTError> {

    info!("Execute Validate");

    let file = args.get_one::<String>("file").unwrap();
    let result = read(file).map_err(|error| UJMTError::FileInputError(error))?;

    debug!("Use File: \"{}\"", file);
    if validate_xml(result).is_ok() {
        Ok(())
    } else {
        Err(UJMTError::Error("Validation failed".to_string()))
    }
}

/*fn generate_from_json(text: String, out_dir: &str, out_file: &str) -> Result<(), presenter::PresenterError> { 
    presenter::to_bpmn_from_json(text, out_dir, out_file)
}*/

fn validate_xml(text: String) -> Result<(), usecases::UseCaseExecError> { 
    let mark_response = usecases::generate_mark_response_from_drawio(text.clone())?;
    //let message = warp::reply::json(&mark_response);
    info!("{:?}", mark_response);
    //let message = warp::reply::json(&mark_response);

    let json_string = serde_json::to_string(&mark_response);
    if json_string.is_ok() {
        info!("{:?}", json_string.unwrap());
    }
    Ok(())
}