'use strict';

const fs = require('fs');

const Molecule = require('../minimal').Molecule;

describe('from and to SMILES', () => {
  it.each(['C', 'COCOC', 'c1cc2cccc3c4cccc5cccc(c(c1)c23)c54'])(
    'fromSmiles',
    (smiles) => {
      const mol = Molecule.fromSmiles(smiles);
      expect(Molecule.fromSmiles(mol.toSmiles()).getIDCode()).toBe(
        mol.getIDCode()
      );
    }
  );

  it('should throw on syntax error', () => {
    expect(function () {
      Molecule.fromSmiles('ABC');
    }).toThrow(/SmilesParser: unknown element label found/);
  });

  it('toIsomericSmiles', () => {
    const mol = Molecule.fromSmiles('C1CC1');
    expect(mol.toIsomericSmiles()).toBe('C1CC1');
  });
});

describe('Molecule', function () {
  it('medley', function () {
    const idcode =
      'enYXNH@MHDAELem`OCIILdhhdiheCDlieKDdefndZRVVjjfjjfjihJBbb@@@';
    let mol = Molecule.fromIDCode(idcode);

    const molfile = mol.toMolfile();
    mol = Molecule.fromMolfile(molfile);
    expect(mol.getIDCode()).toBe(idcode);

    const smiles = mol.toSmiles();
    mol = Molecule.fromSmiles(smiles);
    expect(mol.getIDCode()).toBe(idcode);
  });

  it('toSVG', function () {
    const mol = Molecule.fromSmiles('CCOCCO');
    let svg = mol.toSVG(300, 150, 'myId');
    expect(svg).toContain('width="300px" height="150px"');
    expect(svg).toContain('myId:Bond:0-1');
    expect(svg).toContain('myId:Atom:0');

    svg = mol.toSVG(300, 300, 'myId', {
      strokeWidth: 2,
      fontWeight: 'bold'
    });
    expect(svg).toContain('font-weight="bold"');
    expect(svg).toContain('stroke-width:2');
  });

  it('molfile V3', function () {
    const idcode =
      'enYXNH@MHDAELem`OCIILdhhdiheCDlieKDdefndZRVVjjfjjfjihJBbb@@@';
    let mol = Molecule.fromIDCode(idcode);

    let molfileV3 = mol.toMolfileV3();
    mol = Molecule.fromMolfile(molfileV3);
    expect(mol.getIDCode()).toBe(idcode);

    molfileV3 = mol.toMolfileV3();
    mol = Molecule.fromMolfile(molfileV3);
    expect(mol.getIDCode()).toBe(idcode);
  });

  it('fromMolfileWithAtomMap', () => {
    const molfile = fs.readFileSync(
      `${__dirname}/data/molfileWithHMap.mol`,
      'utf8'
    );
    const result = Molecule.fromMolfileWithAtomMap(molfile);
    expect(result.molecule).toBeInstanceOf(Molecule);
    expect(result.molecule.toSmiles()).toBe('CCCCC');
    expect(result.map).toHaveLength(result.molecule.getAllAtoms());
    expect(new Set(result.map).size).toBe(result.molecule.getAllAtoms());
  });

  it('addMissingChirality', () => {
    let mol = Molecule.fromSmiles('CC(Cl)CC');
    expect(mol.getChiralText()).toBe('unknown chirality');
    mol.addMissingChirality();
    expect(mol.getChiralText()).toBe('racemate');

    mol = Molecule.fromSmiles('CC(Cl)CC');
    mol.addMissingChirality(Molecule.cESRTypeOr);
    expect(mol.getChiralText()).toBe('this or other enantiomer');

    mol = Molecule.fromSmiles('CC(Cl)CC');
    mol.addMissingChirality(Molecule.cESRTypeAbs);
    expect(mol.getChiralText()).toBe('this enantiomer');
  });

  it.skip('getCanonizedIDCode', () => {
    let idcode = 'didH@@RYm^Fh@BHx@';
    let mol = Molecule.fromIDCode(idcode);
    expect(
      mol.getCanonizedIDCode(Molecule.CANONIZER_DISTINGUISH_RACEMIC_OR_GROUPS)
    ).toBe(idcode);
    mol.inventCoordinates();
    expect(
      mol.getCanonizedIDCode(Molecule.CANONIZER_DISTINGUISH_RACEMIC_OR_GROUPS)
    ).toBe(idcode);

    idcode = 'didH@@RYm^Fh@BHX@';
    mol = Molecule.fromIDCode(idcode);
    expect(
      mol.getCanonizedIDCode(Molecule.CANONIZER_DISTINGUISH_RACEMIC_OR_GROUPS)
    ).toBe(idcode);
    mol.inventCoordinates();
    expect(
      mol.getCanonizedIDCode(Molecule.CANONIZER_DISTINGUISH_RACEMIC_OR_GROUPS)
    ).toBe(idcode);
  });
});

@@ -208,11 +208,15 @@
 iles
+: %25s
 ',%0A
-
     
@@ -561,24 +561,397 @@
 %0A  %7D);%0A%0A  it
+.each(%5B%0A    %5B'C1CC1', 'C1CC1'%5D,%0A    %5B'OCC', 'CCO'%5D,%0A    %5B'%5BCH3%5D%5BCH2%5D%5BOH%5D', 'CCO'%5D,%0A    %5B'C-C-O', 'CCO'%5D,%0A    %5B'C(O)C', 'CCO'%5D%0A    // Next examples don't work rigth now. See https://github.com/Actelion/openchemlib/issues/35%0A    // %5B'OC(=O)C(Br)(Cl)N', 'NC(Cl)(Br)C(=O)O'%5D,%0A    // %5B'ClC(Br)(N)C(=O)O', 'NC(Cl)(Br)C(=O)O'%5D,%0A    // %5B'O=C(O)C(N)(Br)Cl', 'NC(Cl)(Br)C(=O)O'%5D%0A  %5D)
 ('toIsomeric
@@ -956,20 +956,37 @@
 icSmiles
+: %25s
 ', (
+input, output
 ) =%3E %7B%0A 
@@ -1020,23 +1020,21 @@
 mSmiles(
-'C1CC1'
+input
 );%0A    e
@@ -1068,23 +1068,22 @@
 )).toBe(
-'C1CC1'
+output
 );%0A  %7D);

add more isomeric tests