package CFX5::Simulation::SimSettings;

use strict;

use CFX5::FileTests;
use CFX5::PathManip qw(dirname basename fullpath join_file normalize_filename);
use CFX5::Config qw (%Config);
use CFX5::Output qw(:all);

use base 'CFX5::Simulation::SettingsBase';

sub _def_cclupdatelog {
  my $self = shift;
  my $cclupdatelogobj = CFX5::Output::CCLUpdateLog->new();
  $self->{settings}{cclupdatelog} = $cclupdatelogobj;
  return $cclupdatelogobj;
}
sub _ok_cclupdatelog {
  1;
}

sub _def_singleconfig {
  undef;
}
sub _ok_singleconfig {
  1;
}

sub _ok_indirect_startup {
  1;
}
sub _def_indirect_startup {
  0;
}

sub _ok_indirect_options {
  return 0 unless defined $_[1];
  1;
}
sub _def_indirect_options {
  0;
}

sub _def_norun {
  0;
}
sub _ok_norun {
  1;
}

sub _def_configmap {
  my $self=shift;
  $self->{configmap}={} unless defined $self->{configmap}; 
  return $self->{configmap};
}

sub ok_configmap { 1; }

sub _def_outobj { undef; }
sub _ok_outobj { 1; }

sub _ok_outobjccl {
  1;
}
sub _def_outobjccl {
  undef;
}


sub _ok_ownoutfile {
  1;
}
sub _def_ownoutfile {
  1;
}

sub _def_text_command { undef; }
sub _ok_text_command { 1; }

sub _def_outfile {
  my $self=shift;
  (defined $self->fullname() ? $self->fullname().'.out' : undef);
}
sub _ok_outfile {
  my $self=shift;
  my $file=shift;
  CFX5::FileTests::datafile_w($file);
}

sub _def_fullname { undef; }
sub _ok_fullname {
  my $self=shift;
  my $name=shift;
  return 0 unless defined $name;
  1;
}

sub _ok_rundir {
  my $self=shift;
  my $file=shift;

  # if we've already got an object pointing to this directory, we
  # won't change it by trying to fix this value, so give a warning if
  # somebody tries to do this.  Remember, always run with -w ...
  if ($^W) {
    if (exists $self->{settings}->{rundir_obj}) {
      warn "Resetting rundir after rundir_obj was created, job will probably be\ninconsistent";
    }
  }
  1;
}
sub _def_rundir {
  my $self=shift;
  (defined $self->fullname() ? $self->fullname() : undef);
}

sub _ok_rundir_obj {
  my $self=shift;
  my $obj=shift;

  unless (!defined($obj) 
	  or (ref($obj) and $obj->isa('CFX5::WorkDir'))) {
    internal_error("Argument to rundir_obj not a CFX5::WorkDir object");
    return 0;
  }
}
sub _def_rundir_obj {
  my $self=shift;

  $self->_DoRunDir(1)
    or return undef;

  # the above sets this, here we just return it
  $self->{settings}->{rundir_obj};
}


sub _ok_checkpoint_workdir {
  return 0 unless defined $_[1];
  1;
}
sub _def_checkpoint_workdir {
  0;
}

sub _ok_configurations {
  1;
}
sub _def_configurations {
  {};
}

sub _ok_mdeffile {
  my $self=shift;
  my $file=shift;

  return 0 unless CFX5::FileTests::datafile($file, "ANSYS CFX Solver Input File $file");
  1;
}
sub _def_mdeffile { undef; }

sub _ok_cfgdefdir {
  my $self=shift;
  my $file=shift;
  return 0 unless CFX5::FileTests::datadir($file, "Configuration definition file directory $file");
  1;
}

sub _def_cfgdefdir {
  my $self = shift; 
  my $cfgdefdir=$self->mdeffile();
  $cfgdefdir =~s!.m(def|res)$!! if defined $cfgdefdir ;
  return $cfgdefdir ; 
}

sub _ok_mresfile {
  my $self=shift;
  my $file=shift;

  return 0 unless CFX5::FileTests::datafile_w($file, "ANSYS CFX Results File $file");
  1;
}
sub _def_mresfile {
  my $self=shift;
  my $ext='.mres';
  (defined $self->fullname() ? $self->fullname().$ext : undef);
}
  
  


sub _ok_name {
  my $self=shift;
  my $name=shift;
  return 0 unless defined $name;
  1;
}
sub _def_name {
  my $self=shift;
  my $mdeffile=$self->mdeffile();
  return undef unless defined $mdeffile;
  if($mdeffile =~ m/\.mres$/i){
    $mdeffile =~ s/(_\d\d\d)?\.[^\.]+$//i;
  } else {
    $mdeffile =~ s/\.[^\.]+$//i;
  }
  fullpath(basename($mdeffile));
}

sub _ok_batch {
  1;
}
sub _def_batch {
  0;
}

sub _ok_stop_now {
  my $self=shift;
  my $v=shift;
  defined $v;
}

sub _def_stop_now {
  my $self=shift;
  return 2 if error_pending();
  return '' unless $self->have_setting('rundir_obj');
  my $rd=$self->rundir_obj();
  return ($rd->have_file('stp')) ? 1 : '';
}

sub _ok_check_only {
  1;
}
sub _def_check_only {
  0;
}

sub _ok_check_for_gui {
  1;
}
sub _def_check_for_gui {
  0;
}

sub _ok_start_aas {
  1;
}
sub _def_start_aas {
  0;
}

1;
