using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.JsonPatch;
using EnumsNET;
using AyaNova.Util;
using AyaNova.Api.ControllerHelpers;
using AyaNova.Biz;
using AyaNova.Models;


namespace AyaNova.Biz
{

    //Turn a type and ID into a displayable name
    //this version uses a direct DataReader for performance in tight loops (search)
    internal static class BizObjectNameFetcherDirect
    {

        internal static string Name(AyaTypeId tid, System.Data.Common.DbCommand cmd)
        {
            return Name(tid.ObjectType, tid.ObjectId, cmd);
        }


        //Returns existance status of object type and id specified in database
        internal static string Name(AyaType aytype, long id, System.Data.Common.DbCommand cmd)
        {
            string TABLE = string.Empty;
            string COLUMN = "name";
            switch (aytype)
            {
                case AyaType.User:
                    TABLE = "auser";
                    break;
                case AyaType.Widget:
                    TABLE = "awidget";
                    break;

                case AyaType.FileAttachment:
                    TABLE = "afileattachment";
                    COLUMN = "displayfilename";
                    break;
                case AyaType.DataFilter:
                    TABLE = "adatafilter";
                    break;
                default:
                    throw new System.NotSupportedException($"AyaNova.BLL.BizObjectNameFetcher::Name type {aytype.ToString()} is not supported");
            }
            cmd.CommandText = $"SELECT m.{COLUMN} FROM {TABLE} AS m WHERE m.id = {id} LIMIT 1";
            using (var dr = cmd.ExecuteReader())
                return dr.Read() ? dr.GetString(0) : "UNKNOWN";
        }





        /////////////////////////////////////////////////////////////////////

    }//eoc


}//eons

