#include "input_geometry.hpp"

QtInputGeometry::QtInputGeometry(Geometry* _geometry):QWidget(){
  geometry=_geometry;
  
  main_layout=new QVBoxLayout;
  lX_label=new QLabel("Horizontal lenght of the domain (l<sub>X</sub>)");
  nX_label=new QLabel("Number of horizontal steps (n<sub>X</sub>)");
  nZ_label=new QLabel("Number of vertical steps (n<sub>Z</sub>)");
  depth_label=new QLabel("Maximal height between h<sub>soil</sub> and h<sub>bot</sub> (depth)");
  lX_input=new QLineEdit(QString::number(geometry->lX));
  nX_input=new QLineEdit(QString::number(geometry->nX));
  nZ_input=new QLineEdit(QString::number(geometry->nZ_max));
  depth_input=new QLineEdit(QString::number(geometry->depth));

  //Labels
  lX_label->setTextFormat(Qt::RichText);
  nX_label->setTextFormat(Qt::RichText);
  nZ_label->setTextFormat(Qt::RichText);
  depth_label->setTextFormat(Qt::RichText);

  //Button
  refresh_button=new QPushButton("Refresh");

  //Main layout
  int vspace=20;
  main_layout->addWidget(lX_label);
  main_layout->addWidget(lX_input);
  main_layout->addSpacing(vspace);
  main_layout->addWidget(nX_label);
  main_layout->addWidget(nX_input);
  main_layout->addSpacing(vspace);
  main_layout->addWidget(depth_label);
  main_layout->addWidget(depth_input);
  main_layout->addSpacing(vspace);
  main_layout->addWidget(nZ_label);
  main_layout->addWidget(nZ_input);
  main_layout->addSpacing(2*vspace);
  main_layout->addWidget(refresh_button);
  main_layout->addStretch();

  setLayout(main_layout);

  //Validators
  positive_double_validator=new QDoubleValidator;
  positive_double_validator->setBottom(0);
  positive_int_validator=new QIntValidator;
  positive_int_validator->setBottom(1);
  lX_input->setValidator(positive_double_validator);
  nX_input->setValidator(positive_int_validator);
  depth_input->setValidator(positive_double_validator);
  nZ_input->setValidator(positive_int_validator);

  connect(refresh_button,&QPushButton::clicked,this,&QtInputGeometry::emitGeometryChanged);
}

QWidget*
QtInputGeometry::validate(){
  if(not lX_input->hasAcceptableInput()) return lX_input;
  if(not nX_input->hasAcceptableInput()) return nX_input;
  if(not depth_input->hasAcceptableInput()) return depth_input;
  if(not nZ_input->hasAcceptableInput()) return nZ_input;
  return nullptr;
}

void
QtInputGeometry::save(fstream& file){
  double d;
  size_t n;
  d=lX_input->text().toDouble();
  file.write((char*)&d,sizeof(double));
  n=nX_input->text().toULong();
  file.write((char*)&n,sizeof(size_t));
  d=depth_input->text().toDouble();
  file.write((char*)&d,sizeof(double));
  n=nZ_input->text().toULong();
  file.write((char*)&n,sizeof(size_t));
}

void
QtInputGeometry::load(fstream& file){
  double d;
  size_t n;
  file.read((char*)&d,sizeof(double));
  lX_input->setText(QString::number(d));
  file.read((char*)&n,sizeof(size_t));
  nX_input->setText(QString::number(n));
  file.read((char*)&d,sizeof(double));
  depth_input->setText(QString::number(d));
  file.read((char*)&n,sizeof(size_t));
  nZ_input->setText(QString::number(n));
}


