use database_model::{FieldRevision, RowRevision};
use flowy_database::entities::FieldType;
use flowy_database::services::field::{
  ChecklistTypeOptionPB, DateCellChangeset, MultiSelectTypeOptionPB, SelectOptionPB,
  SingleSelectTypeOptionPB,
};
use flowy_database::services::row::RowRevisionBuilder;
use std::sync::Arc;

pub struct DatabaseRowTestBuilder {
  field_revs: Vec<Arc<FieldRevision>>,
  inner_builder: RowRevisionBuilder,
}

impl DatabaseRowTestBuilder {
  pub fn new(block_id: String, field_revs: Vec<Arc<FieldRevision>>) -> Self {
    let inner_builder = RowRevisionBuilder::new(&block_id, field_revs.clone());
    Self {
      field_revs,
      inner_builder,
    }
  }

  pub fn insert_text_cell(&mut self, data: &str) -> String {
    let text_field = self.field_rev_with_type(&FieldType::RichText);
    self
      .inner_builder
      .insert_text_cell(&text_field.id, data.to_string());

    text_field.id.clone()
  }

  pub fn insert_number_cell(&mut self, data: &str) -> String {
    let number_field = self.field_rev_with_type(&FieldType::Number);
    self
      .inner_builder
      .insert_text_cell(&number_field.id, data.to_string());
    number_field.id.clone()
  }

  pub fn insert_date_cell(&mut self, data: &str) -> String {
    let value = serde_json::to_string(&DateCellChangeset {
      date: Some(data.to_string()),
      time: None,
      is_utc: true,
      include_time: Some(false),
    })
    .unwrap();
    let date_field = self.field_rev_with_type(&FieldType::DateTime);
    self.inner_builder.insert_text_cell(&date_field.id, value);
    date_field.id.clone()
  }

  pub fn insert_checkbox_cell(&mut self, data: &str) -> String {
    let checkbox_field = self.field_rev_with_type(&FieldType::Checkbox);
    self
      .inner_builder
      .insert_text_cell(&checkbox_field.id, data.to_string());

    checkbox_field.id.clone()
  }

  pub fn insert_url_cell(&mut self, content: &str) -> String {
    let url_field = self.field_rev_with_type(&FieldType::URL);
    self
      .inner_builder
      .insert_url_cell(&url_field.id, content.to_string());
    url_field.id.clone()
  }

  pub fn insert_single_select_cell<F>(&mut self, f: F) -> String
  where
    F: Fn(Vec<SelectOptionPB>) -> SelectOptionPB,
  {
    let single_select_field = self.field_rev_with_type(&FieldType::SingleSelect);
    let type_option = SingleSelectTypeOptionPB::from(&single_select_field);
    let option = f(type_option.options);
    self
      .inner_builder
      .insert_select_option_cell(&single_select_field.id, vec![option.id]);

    single_select_field.id.clone()
  }

  pub fn insert_multi_select_cell<F>(&mut self, f: F) -> String
  where
    F: Fn(Vec<SelectOptionPB>) -> Vec<SelectOptionPB>,
  {
    let multi_select_field = self.field_rev_with_type(&FieldType::MultiSelect);
    let type_option = MultiSelectTypeOptionPB::from(&multi_select_field);
    let options = f(type_option.options);
    let ops_ids = options
      .iter()
      .map(|option| option.id.clone())
      .collect::<Vec<_>>();
    self
      .inner_builder
      .insert_select_option_cell(&multi_select_field.id, ops_ids);

    multi_select_field.id.clone()
  }

  pub fn insert_checklist_cell<F>(&mut self, f: F) -> String
  where
    F: Fn(Vec<SelectOptionPB>) -> Vec<SelectOptionPB>,
  {
    let checklist_field = self.field_rev_with_type(&FieldType::Checklist);
    let type_option = ChecklistTypeOptionPB::from(&checklist_field);
    let options = f(type_option.options);
    let ops_ids = options
      .iter()
      .map(|option| option.id.clone())
      .collect::<Vec<_>>();
    self
      .inner_builder
      .insert_select_option_cell(&checklist_field.id, ops_ids);

    checklist_field.id.clone()
  }
  pub fn field_rev_with_type(&self, field_type: &FieldType) -> FieldRevision {
    self
      .field_revs
      .iter()
      .find(|field_rev| {
        let t_field_type: FieldType = field_rev.ty.into();
        &t_field_type == field_type
      })
      .unwrap()
      .as_ref()
      .clone()
  }

  pub fn build(self) -> RowRevision {
    self.inner_builder.build()
  }
}

impl std::ops::Deref for DatabaseRowTestBuilder {
  type Target = RowRevisionBuilder;

  fn deref(&self) -> &Self::Target {
    &self.inner_builder
  }
}

impl std::ops::DerefMut for DatabaseRowTestBuilder {
  fn deref_mut(&mut self) -> &mut Self::Target {
    &mut self.inner_builder
  }
}
