using System.Collections.Generic;
using System.Linq;
using Toolbox.Docking.Api.Control;
using Toolbox.Docking.Api.Objects.Metabolism;

namespace ToolboxAddinExamples.Metabolilsm
{
    public class MetabolismAddin : ITbMetabolism
    {
        public void Dispose()
        {
        }


        public int AlternativeObserved(ITbBasket target)
        {
            return 1;
        }

        public MetabolResult GetProducts(ITbBasket target, int alternativeObserved)
        {
            if (target?.Chemical == null)
                return null;

            var products = ProduceProducts(target.Chemical.Smiles);
            return new MetabolResult(null, products, Enumerable.Repeat((float?) null, products.Count));
        }

        private static List<string> ProduceProducts(string smiles)
        {
            var result = new List<string>();
            if (smiles.Contains("O"))
            {
                var oCount = smiles.Count(ch => ch.Equals('O'));
                for (var i = 0; i < oCount; i++)
                    result.Add('O' + string.Join("", Enumerable.Repeat("C", i + 1)));
            }
            if (smiles.Contains("N"))
            {
                var oCount = smiles.Count(ch => ch.Equals('N'));
                for (var i = 0; i < oCount; i++)
                    result.Add('N' + string.Join("", Enumerable.Repeat("C", i + 1)));
            }
            if (smiles.Contains("S"))
            {
                var oCount = smiles.Count(ch => ch.Equals('S'));
                for (var i = 0; i < oCount; i++)
                    result.Add('S' + string.Join("", Enumerable.Repeat("C", i + 1)));
            }

            return result;
        }
    }
}
