using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChemAxon.NET.IKVM.DB.JChemMemoryTable;
using ChemAxon.NET.IKVM.Chemistry;
using ChemAxon.NET.API;
using ChemAxon.NET.Base.Search.Options;

namespace ChemAxon.NET.UnitTest.API.Examples.Database
{
    public class JChemMemoryTableExample
    {

        public IJChemMemoryTable CreateMemoryTable()
        {
            return MainFactory.Database.OpenMemoryCache();
        }

        public void FillMemoryTable(IJChemMemoryTable memoryTable)
        {
            IJChemMolecule molecule = MainFactory.Chemistry.CreateMolecule("benzene");

            memoryTable.Add(molecule);

            IJChemMolecule[] molecules = new IJChemMolecule[]{
                MainFactory.Chemistry.CreateMolecule("methylpropane"),
                MainFactory.Chemistry.CreateMolecule("benzol")
            };

            memoryTable.Add(molecules);

            Console.WriteLine(string.Format("Count: {0}", memoryTable.Count));
        }

        public void IsMatchMolecule(IJChemMemoryTable memoryTable)
        {
            IJChemMolecule query = MainFactory.Chemistry.CreateMolecule("benzene");
            bool isMatch = memoryTable.IsMatch(query);

            Console.WriteLine(string.Format("IsMatch: {0}", isMatch));
        }

        public void FindAllMolecule(IJChemMemoryTable memoryTable)
        {
            memoryTable.Options.SearchType = JChemSearchTypes.Similarity;
            memoryTable.Options.SimilarityThreshold = 1f;
            memoryTable.Options.InvertResults = true;

            IJChemMolecule query = MainFactory.Chemistry.CreateMolecule("benzene");

            IEnumerable<IJChemMolecule> result = memoryTable.FindAll(query);

            foreach (IJChemMolecule molecule in result)
                Console.WriteLine(molecule.Formula);
        }

        public void ClearMemoryTable(IJChemMemoryTable memoryTable)
        {
            Console.WriteLine(string.Format("Count before clear: {0}", memoryTable.Count));
            memoryTable.Clear();
            Console.WriteLine(string.Format("Count aftre clear: {0}", memoryTable.Count));
        }
    }
}