use crate::chopper::error::Result;
use crate::cli::util::YesNoAuto;
use crate::source::csv_timestamp_config::TimestampConfig;
use crate::util::csv::util::parse_into_delimiter;

#[derive(Debug, Clone)]
pub struct CSVInputConfig {
    pub delimiter: Option<u8>,
    pub has_header: YesNoAuto,
    pub merge_extra_column_data: bool,
    pub fill_missing_columns_with_empty: bool,
    pub hide_timestamp_column: bool,
    pub timestamp_config: TimestampConfig,
}

impl CSVInputConfig {
    pub fn new(timestamp_config: TimestampConfig) -> Self {
        CSVInputConfig {
            delimiter: None,
            has_header: YesNoAuto::Auto,
            merge_extra_column_data: true,
            fill_missing_columns_with_empty: true,
            hide_timestamp_column: false,
            timestamp_config,
        }
    }

    pub fn with_delimiter(mut self, delimiter: Option<impl AsRef<str>>) -> Result<Self> {
        self.delimiter = match delimiter {
            None => None,
            Some(x) => Some(parse_into_delimiter(x.as_ref())?),
        };
        Ok(self)
    }

    pub fn with_header(mut self, has_header: YesNoAuto) -> Self {
        self.has_header = has_header;
        self
    }

    pub fn merge_extra_column_data(mut self, merge_extra_column_data: bool) -> Self {
        self.merge_extra_column_data = merge_extra_column_data;
        self
    }

    pub fn fill_missing_columns_with_empty(
        mut self,
        fill_missing_columns_with_empty: bool,
    ) -> Self {
        self.fill_missing_columns_with_empty = fill_missing_columns_with_empty;
        self
    }

    pub fn hide_timestamp_column(mut self, hide_timestamp_column: bool) -> Self {
        self.hide_timestamp_column = hide_timestamp_column;
        self
    }
}
