/* 
(C) 2022 Cadence Design Systems, Inc. (Cadence) 
All rights reserved.
TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is
provided to current licensees or subscribers of Cadence products or
SaaS offerings (each a "Customer").
Customer is hereby permitted to use, copy, and modify the Sample Code,
subject to these terms. Cadence claims no rights to Customer's
modifications. Modification of Sample Code is at Customer's sole and
exclusive risk. Sample Code may require Customer to have a then
current license or subscription to the applicable Cadence offering.
THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED.  OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT
NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall Cadence be
liable for any damages or liability in connection with the Sample Code
or its use.
*/
/****************************************************************************
* Converts an MDL query structure into an image file.
* The output file format depends on its file extension.
****************************************************************************/

#include <openeye.h>

#include <string>
#include <oeplatform.h>
#include <oesystem.h>
#include <oechem.h>
#include <oedepict.h>

#include "mdlquery2img.itf"

using namespace std;
using namespace OEPlatform;
using namespace OESystem;
using namespace OEChem;
using namespace OEDepict;

int main(int argc, char *argv[])
{
  OEInterface itf(InterfaceData);
  OEConfigureImageOptions(itf);
  OEConfigure2DMolDisplayOptions(itf);

  if(!OEParseCommandLine(itf, argc, argv))
    OEThrow.Fatal("Unable to interpret command line!");

  const string iname = itf.Get<string>("-in");
  const string oname = itf.Get<string>("-out");

  const string ext = OESystem::OEFileExtension(oname, "");
  if (!OEIsRegisteredImageFile(ext))
    OEThrow.Fatal("Unknown image type!");

  oemolistream ifs;
  if (!ifs.open(iname.c_str()))
    OEThrow.Fatal("Cannot open input file!");

  if (ifs.GetFormat() != OEFormat::MDL)
    OEThrow.Fatal("Input file is not an MDL query file!");

  oeofstream ofs;
  if (!ofs.open(oname.c_str()))
    OEThrow.Fatal("Cannot open output file!");

  OEGraphMol mol;
  if (!OEReadMDLQueryFile(ifs, mol))
    OEThrow.Fatal("Cannot read mdl query input file!");

  const bool clearcoords = false;
  const bool suppressH = false;
  OEPrepareDepiction(mol, clearcoords, suppressH);

  const double width  = OEGetImageWidth (itf);
  const double height = OEGetImageHeight(itf);
  OE2DMolDisplayOptions opts(width, height, OEScale::AutoScale);
  OESetup2DMolDisplayOptions(opts, itf);

  OE2DMolDisplay disp(mol, opts);
  OERenderMolecule(ofs, ext, disp);

  return 0;
}
