use std::collections::{BTreeMap, HashMap};
use anyhow::Result;
use libhaystack::val::Value;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::base::block::BlockState;
use crate::base::program::{Program, data::LinkData};
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct BlockInputData {
pub kind: String,
pub val: Value,
#[serde(default)]
pub is_connected: bool,
}
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct BlockOutputData {
pub kind: String,
pub val: Value,
}
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct BlockDefinition {
pub id: String,
pub name: String,
pub library: String,
pub inputs: BTreeMap<String, BlockInputData>,
pub outputs: BTreeMap<String, BlockOutputData>,
#[serde(default)]
pub state: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub fault_reason: Option<String>,
}
#[derive(Debug, Clone)]
pub enum ChangeSource {
Input(String, Value),
Output(String, Value),
}
#[derive(Debug, Clone)]
pub struct WatchMessage {
pub block_id: Uuid,
pub changes: HashMap<String, ChangeSource>,
pub state: BlockState,
}
#[derive(Debug, Clone)]
pub enum EngineMessage<WatchEventSender: Clone> {
AddBlockReq(Uuid, String, Option<String>, Option<String>),
AddBlockRes(Result<Uuid, String>),
RemoveBlockReq(Uuid, Uuid),
RemoveBlockRes(Result<Uuid, String>),
WatchBlockSubReq(Uuid, WatchEventSender),
WatchBlockSubRes(Result<Uuid, &'static str>),
WriteBlockOutputReq(Uuid, Uuid, String, Value),
WriteBlockOutputRes(Result<Value, String>),
WriteBlockInputReq(Uuid, Uuid, String, Value),
WriteBlockInputRes(Result<Option<Value>, String>),
WatchBlockUnsubReq(Uuid),
WatchBlockUnsubRes(Result<Uuid, &'static str>),
GetCurrentProgramReq(Uuid),
GetCurrentProgramRes(Result<Program, String>),
LoadProgramReq(Uuid, Program),
LoadProgramRes(Result<(), String>),
InspectBlockReq(Uuid, Uuid),
InspectBlockRes(Result<BlockDefinition, String>),
EvaluateBlockReq(Uuid, String, Vec<Value>, Option<String>),
EvaluateBlockRes(Result<Vec<Value>, String>),
ConnectBlocksReq(Uuid, LinkData),
ConnectBlocksRes(Result<LinkData, String>),
RemoveLinkReq(Uuid, Uuid),
RemoveLinkRes(Result<bool, String>),
Shutdown,
Pause,
Resume,
Reset,
}