using System.Windows.Forms;
using System.ComponentModel;
using ChemAxon.NET.IKVM.IO;
using ChemAxon.NET.IKVM.Chemistry;
using ChemAxon.NET.IKVM.ExtensionMethods;
using ChemAxon.NET.Windows.Forms.Grid;
using ChemAxon.NET.API;

namespace _2013_Developer_training {

    public class Import {
        public static void ImportSDFIntoGridView() {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "SD File (*.sdf)|*.sdf";
            ofd.ShowDialog();
            var fileName = ofd.FileName;
            if (!string.IsNullOrEmpty(fileName)) {

                //////////////////////////////////////////////////////////
                // Data sources - lazy-read molecules
                var enumarator = MainFactory.IO.OpenFile(fileName).Read();
                BindingList<BindableMolecule> list = new BindingList<BindableMolecule>();

                ////////////////////////////////////////////////////////////////
                // Fill Data and Aromatize in one step 
                foreach (var mol in enumarator) {
                    list.Add(MainFactory.Chemistry.CreateBindableMolecule( mol.Transformations.Aromatize().ToJChemMolecule()));
                }

                //////////////////////////////////////////////////////////
                // Visualization in Grid
                MoleculeGridView gv = new MoleculeGridView();

                //////////////////////////////////////////////////////////
                // Settings
                gv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
                gv.MoleculeCellSize = new System.Drawing.Size(150, 150);
                gv.AutoGenerateColumns = false;
                // Binding
                gv.DataSource = list;
                gv.Columns.Clear();
                /////////////////////////////////////////////////
                // Columns what to Show in
                AddGridViewColumn(gv, "BindableData", "Structure");
                AddGridViewColumn(gv, "Formula", "Formula");
                AddGridViewColumn(gv, "Mass", "Mass");

                gv.MoleculeColumns.Add("BindableData");
                gv.FormulaColumn = "Formula";

                ShowMoleculeControl(gv, "Importing with IO into grid and make aromatization within one step");
            }
        }

        private static void ShowMoleculeControl(Control c, string title) {
            var frm = new Form();
            frm.Text = title;
            frm.Controls.Add(c);
            c.Dock = DockStyle.Fill;
            frm.ShowDialog();
        }

        private static void AddGridViewColumn(DataGridView gv, string columnName,
                                        string columnDisplayName) {
            DataGridViewColumn gridColumn = new DataGridViewTextBoxColumn();
            gridColumn.Name = columnName;
            gridColumn.DataPropertyName = columnName;
            gridColumn.HeaderText = columnDisplayName;
            gv.Columns.Add(gridColumn);
        }
    }

}