use data::tables::ProcedureConfigID;

use super::{ProcedureAction, ProcedureBase, ProcedureError, ProcedureState, ProcedureType};

pub struct ProcedureSelectRole {
    id: ProcedureConfigID,
    state: ProcedureState,
}

impl ProcedureSelectRole {
    pub fn new(id: ProcedureConfigID) -> Self {
        Self {
            id,
            state: ProcedureState::Init,
        }
    }
}

impl ProcedureBase for ProcedureSelectRole {
    fn id(&self) -> ProcedureConfigID {
        self.id
    }

    fn procedure_type(&self) -> ProcedureType {
        ProcedureType::SelectRole
    }

    fn on_action(&mut self, action: ProcedureAction) -> Result<ProcedureState, ProcedureError> {
        match action {
            ProcedureAction::SelectRole => {
                (self.state == ProcedureState::Init)
                    .then_some(())
                    .ok_or(ProcedureError::InvalidState(action, self.state))?;

                self.state = ProcedureState::Finish;
            }
            ProcedureAction::PerformTrigger => (),
            ProcedureAction::PerformEnd => (),
            _ => {
                return Err(ProcedureError::UnsupportedAction(
                    action,
                    self.procedure_type(),
                ))
            }
        }

        Ok(self.state)
    }

    fn is_finished(&self) -> bool {
        self.state == ProcedureState::Finish
    }
}
