using System;
using System.Data;
using System.Windows.Forms;
using System.Collections.Generic;
using chemaxon.sss;
using chemaxon.sss.search;
using chemaxon.struc;
using chemaxon.formats;

namespace JChemBaseSearch
{

    public partial class BaseSearchForm : Form
    {
        public BaseSearchForm()
            : base()
        {
            InitializeComponent();
        }

        private void BaseSearchForm_Load(object sender, EventArgs e)
        {
            List> itemList = new List>();
            itemList.Add(new KeyValuePair(SearchConstants.__Fields.SUBSTRUCTURE, "Substructure"));
            itemList.Add(new KeyValuePair(SearchConstants.__Fields.SUPERSTRUCTURE, "Superstructure"));
            itemList.Add(new KeyValuePair(SearchConstants.__Fields.FULL, "Full"));
            itemList.Add(new KeyValuePair(SearchConstants.__Fields.FULL_FRAGMENT, "Full fragment"));
            itemList.Add(new KeyValuePair(SearchConstants.__Fields.SIMILARITY, "Similarity"));
            itemList.Add(new KeyValuePair(SearchConstants.__Fields.DUPLICATE, "Duplicate"));

            searchTypeCombo.ValueMember = "Key";
            searchTypeCombo.DisplayMember = "Value";
            searchTypeCombo.DataSource = itemList;
        }

        private void searchTypeCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((int)searchTypeCombo.SelectedValue == SearchConstants.__Fields.SIMILARITY)
            {
                groupBoxSimilarityType.Enabled = true;
            }
            else
            {
                groupBoxSimilarityType.Enabled = false;
            }
        }

        private void searchDatabaseBtn_Click(object sender, EventArgs e)
        {
            Molecule queryMolecule = null;
            try
            {
                queryMolecule = MolImporter.importMol(queryMoleculeTxt.Text);
            }
            catch
            {
                MessageBox.Show("Invalid query molecule");
                return;
            }

            int searchType = (int)searchTypeCombo.SelectedValue;
            JChemSearchOptions searchOptions = new JChemSearchOptions(searchType);

            if (searchType == SearchConstants.__Fields.SIMILARITY)
            {
                searchOptions.setDissimilarityThreshold(1.0f - float.Parse(dissimilarityTreshTxt.Text));
            }
            searchOptions.setMaxResultCount(int.Parse(maxHitsTxt.Text));

            //to do: run it on a separate thread
            DataTable searchResultsTable = DataAccess.SearchStructure(queryMolecule, searchOptions);
            DataTable transformedDataTable = NetMolImporter.ImportMolecules(searchResultsTable, "cd_structure");
            searchResultsGridView.DataSource = transformedDataTable;
        }
    }

}