// Dynamole - Compute molecular interactions
// Copyright (C) 2018-2026 Eric Bechet - bechet@cadxfem.org
//
// See the LICENSE file for license information and contributions.
// Please report all bugs and problems to <bechet@cadxfem.org>.
#include "dynamoleconfig.h"
#ifdef DYNAMOLE_GRAPHICAL_INTERFACE
#include "ndisplay.h"
#endif //MOSHADE_GRAPHICAL_INTERFACE

#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <cstring>
#ifdef OPENMP_FOUND
#include <omp.h>
#endif // OPENMP_FOUND

#include "mole.h"

struct cmdline_info
{
  bool verbose; // print a lot of info
  bool quiet; // prints only the results
  int nprocs; // number of threads in parallel mode
  std::string file; // filename of geometry (descriptive .mol)
};



bool parse_cmdline(int argc, char *argv[], cmdline_info &data);
void help(void);
bool load_geometry(const cmdline_info &data);
inline int mo_stricmp ( const char *s1, const char*s2 );

int main(int argc, char *argv[])
{
  // load sample data
  cmdline_info cmdline;
  interactions group;
  
  group.add_molecule(molecule(ele_charge,2.8*debye,0*5.66*4*pi*perm0*1e-30));
  group.add_molecule(molecule(ele_charge,2.8*debye,0*5.66*4*pi*perm0*1e-30));
  for (int i=1;i<1000;++i)
  {
    group.get_molecule(0).pos=npoint(0,0,0);
    group.get_molecule(1).pos=npoint(1e-12*i,0,0);
    npoint3 f,df[3];
//    group.interforce(group.get_molecule(0),group.get_molecule(1),f,df);
    group.force(group.get_molecule(1),f,df);
    std::cout << i*1e-12 << "   " ; f.print(std::cout);
    for (int i=0;i<3;++i) df[i].print(std::cout);
  }
  
  
  
  bool ok=parse_cmdline(argc,argv,cmdline);
  if (ok)
  {
  }
  else
    help();
}

bool parse_cmdline(int argc, char *argv[], cmdline_info &data)
{
  data.verbose=false;
  data.quiet=false;
  data.nprocs=0;
  if (argc>=2)
  {
    data.file=std::string(argv[1]);
    for (int i=2;i<argc;++i)
    {
      if (!mo_stricmp(argv[i],"verbose"))
        data.verbose=true;
      if (!mo_stricmp(argv[i],"quiet"))
        data.quiet=true;
      if (!mo_stricmp(argv[i],"nproc"))
      {
        data.nprocs=atof(argv[++i]);
      }
    }
    return true;
  }
  else return false; // help !
}

bool load_geometry(const cmdline_info &data,molecular_group &group)
{
  
}

void help(void)
{
  std::cerr << "Dynamole - compute equilibrium states for molecules." << std::endl;
  std::cerr << "This is free software (c) 2018-2026 Eric Bechet - eric@bechet.ca" << std::endl;
  std::cerr << "See the LICENCE file for license information." << std::endl;
  std::cerr << "Usage : dynamole file [nproc val] [verbose]" << std::endl;
  std::cerr << "where \"file\" is a .mol description file" << std::endl;
  std::cerr << std::endl;
}



void display_geo(molecular_group &group)
{
#ifdef DYNAMOLE_GRAPHICAL_INTERFACE
  data_container data;
  ndisplay disp;
  properties p=data.getproptriangles();
  p.c.A=50;
  p.edgeon=true;
  p.edgecolor=p.c;
  data.setproptriangles(p);
  p=data.getproplines();
  p.c=color(255,255,255);
  data.setproplines(p);
  p=data.getproppoints();
  p.c=color(255,255,255);
  p.pointsize=3;
  data.setproppoints(p);
#endif //DYNAMOLE_GRAPHICAL_INTERFACE
}

inline int mo_stricmp ( const char *s1, const char*s2 )
{
    int    c1, c2;
    int    cmp = 0;

    if (s1 && s2)
    for (;;)
    {
        c1 = *s1++;
        c2 = *s2++;
        if (c1 && c2)
        {
            c1 = tolower(c1)&0xFF; // 8 bits
            c2 = tolower(c2)&0xFF; // only
            if (c1 < c2)
            {
                cmp = -1;
                break;
            }
            else if (c1 > c2)
            {
                cmp = 1;
                break;
            }
        }
        else
        {
            if (c1)
                cmp = 1;
            else if (c2)
                cmp = -1;
            break;
        }
    }
    return cmp;
}
