use super::{
    sink_names_input_select, BinBuildEnvironment, BinDescription,
    Calculator, FetchItem, GetCalibration, Item, Iteration, Result, Scope,
    SinkBin, SinkNames, SourceBin, SourceId, SourceNames,
    SourceSinkBinDescription, WriteDotSimple, SINK_INPUT, SINK_SELECT,
};
use crate::error;
use indexmap::{IndexMap, IndexSet};
use snafu::OptionExt;

static BIN_TYPE: &'static str = "multiplex";

pub struct Bin {
    scope: Scope,

    source_input: Box<FetchItem>,
    source_select: Box<FetchItem>,

    outputs: IndexMap<String, (usize, Item)>,
}

impl SinkBin for Bin {}

impl SourceBin for Bin {
    fn get_source_data(&self, source: &SourceId) -> Result<Item> {
        let (_, item) = self.outputs.get(&source.id).context(
            error::MissingSourceName {
                scope: self.scope.clone(),
                name: source.id.to_string(),
                bin_type: BIN_TYPE.to_string(),
            },
        )?;
        Ok(item.clone())
    }
}

impl Calculator for Bin {
    fn calculate(&mut self, _iteration: &Iteration) -> Result<()> {
        let input = self.source_input.fetch_item(&self.scope)?;
        let select = self.source_select.fetch_item(&self.scope)?;

        let select = select.to_usize().ok();

        for (i, v) in self.outputs.values_mut() {
            *v = match select {
                Some(select_index) if *i == select_index => input.clone(),
                Some(_) | None => Item::Nothing,
            };
        }
        Ok(())
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Description {
    pub outputs: IndexMap<String, usize>,
}

impl BinDescription for Description {
    type Bin = Bin;

    fn check_validity(
        &self,
        _scope: &Scope,
        _get_calibration: &mut GetCalibration,
    ) -> Result<()> {
        Ok(())
    }

    fn bin_type(&self) -> &'static str {
        BIN_TYPE
    }
}

impl SinkNames for Description {
    fn sink_names(&self) -> IndexSet<String> {
        sink_names_input_select()
    }
}

impl SourceNames for Description {
    fn source_names(&self) -> Result<IndexSet<String>> {
        Ok(self.outputs.keys().cloned().collect())
    }
}

impl SourceSinkBinDescription for Description {
    fn build_bin(
        &self,
        scope: &Scope,
        env: &mut BinBuildEnvironment,
    ) -> Result<Self::Bin> {
        Ok(Bin {
            scope: scope.clone(),
            source_input: env.resolve(SINK_INPUT)?,
            source_select: env.resolve(SINK_SELECT)?,

            outputs: self
                .outputs
                .iter()
                .map(|(k, v)| (k.to_string(), (*v, Item::Nothing)))
                .collect(),
        })
    }
}

impl WriteDotSimple for Description {}

#[cfg(test)]
mod tests {
    use super::Description;
    use crate::bins::{directsource, verificationsink};
    use crate::{run_bin, Result};
    use indexmap::indexset;

    #[test]
    fn simulate() -> Result<()> {
        use crate::Item::*;

        let input = directsource::Description {
            columns: indexset!["input".to_string(), "select".to_string()],
            rows: vec![
                vec![Nothing, Nothing],
                vec![U8(49), Nothing],
                vec![Nothing, U8(1)],
                vec![U8(50), U8(0)],
                vec![U8(51), U8(1)],
                vec![U8(52), U8(2)],
                vec![U8(53), U8(3)],
                vec![U8(54), U8(4)],
            ]
            .into(),
        };
        let verification = verificationsink::Description {
            columns: indexset!["a".to_string(), "b".to_string()],
            expected: vec![
                vec![Nothing, Nothing],
                vec![Nothing, Nothing],
                vec![Nothing, Nothing],
                vec![Nothing, Nothing],
                vec![U8(51), Nothing],
                vec![Nothing, Nothing],
                vec![Nothing, Nothing],
                vec![Nothing, U8(54)],
            ]
            .into(),
        };
        let description = Description {
            outputs: vec![
                ("a".to_string(), 1usize),
                ("b".to_string(), 4usize),
            ]
            .into_iter()
            .collect(),
        };

        run_bin(&input, &description, &verification)
    }
}
