using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Sockeye.Models;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;


namespace Sockeye.PickList
{
    internal static class PickListFetcher
    {
        internal static async Task<List<NameIdActiveItem>> GetResponseAsync(IAyaPickList PickList, string autoCompleteQuery,
        string tagSpecificQuery, bool includeInactive, long[] preIds, string variant, AyContext ct, ILogger log, string overrideTemplate)
        {

            //Sort out effective Template
            string Template = null;
            if (string.IsNullOrWhiteSpace(overrideTemplate))
            {
                //Attempt to fetch custom template              
                var t = await ct.PickListTemplate.FirstOrDefaultAsync(z => z.Id == ((long)PickList.DefaultListAType));
                if (t == null)
                {
                    Template = PickList.DefaultTemplate;
                }
                else
                {
                    Template = t.Template;
                }
            }
            else
            {
                Template = overrideTemplate;
            }

            //parse the template 
            var jTemplate = JArray.Parse(Template);

            //Get the field key names in a list from the template
            List<string> TemplateColumnNames = PickList.GetFieldListFromTemplate(jTemplate);

            //BUILD THE QUERY  
            var q = PickListSqlBuilder.Build(PickList, TemplateColumnNames, autoCompleteQuery, tagSpecificQuery, includeInactive, preIds, variant);

            //If want to log all queries
            //log.LogInformation(q);

            //RETURN OBJECTS  
            var ret = new List<NameIdActiveItem>();

            //QUERY THE DB
            using (var command = ct.Database.GetDbConnection().CreateCommand())
            {
                await ct.Database.OpenConnectionAsync();

                //GET DATA RETURN ROWS
                command.CommandText = q;
                try
                {
                    using (var dr = await command.ExecuteReaderAsync())
                    {
                        while (dr.Read())
                        {
                            //query is always in the same order:
                            //plId, plActive, plName
                            ret.Add(new NameIdActiveItem
                            {
                                Id = dr.GetInt64(0),
                                Active = dr.GetBoolean(1),
                                Name = dr.GetString(2)
                            });
                        }
                    }
                }
                catch (Npgsql.PostgresException e)
                {
                    //log out the exception and the query
                    //This may be called internally in *Biz classes who don't have a log of their own
                    if (log == null)
                        log = Sockeye.Util.ApplicationLogging.CreateLogger("PickListFetcher");
                    log.LogError("PickList query failed unexpectedly.  Query was:");
                    log.LogError(q);
                    log.LogError(e, "DB Exception");
                    throw new System.Exception("PickListFetcher - Query failed see log");
                }
                catch (System.Exception e)
                {
                    //ensure any other type of exception gets surfaced properly
                    //log out the exception and the query
                    log.LogError("PickListFetcher unexpected failure.  Query was:");
                    log.LogError(q);

                    log.LogError(e, "Exception");
                    throw new System.Exception("PickListFetcher - unexpected failure see log");
                }
            }
            return ret;
        }
    }//eoc
}//eons