namespace AyaNova.Biz
{

    //Turn a type and ID into a displayable name
    //this version uses a direct DataReader for performance in tight loops (search)
    //Used by search and eventlog processor
    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 ayaType, long id, System.Data.Common.DbCommand cmd)
        {
            if (ayaType == AyaType.NoType || id == 0)
            {
                return "-";
            }


            string TABLE = string.Empty;
            string COLUMN = "name";
            //CoreBizObject add here BUT ONLY ADD IF AYATYPE NAME DIFFERS FROM TABLE NAME OR NO NAME FIELD AS PRIMARY NAME-LIKE COLUMN

            switch (ayaType)
            {
                //Oddballs only, otherwise let default handle it
                case AyaType.OpsNotificationSettings:
                    return "-";
                case AyaType.Global:
                    return "Global";
                case AyaType.Backup:
                    return "-";
                case AyaType.GlobalOps:
                    return "GlobalOps";
                case AyaType.FileAttachment:
                    TABLE = "afileattachment";
                    COLUMN = "displayfilename";
                    break;
                case AyaType.DataListView:
                    TABLE = "adatalistview";
                    break;
                case AyaType.FormCustom:
                    TABLE = "aformcustom";
                    COLUMN = "formkey";
                    break;
                case AyaType.WorkOrder:
                    cmd.CommandText = $"SELECT m.serial FROM aworkorder AS m WHERE m.id = {id} LIMIT 1";
                    using (var dr = cmd.ExecuteReader())
                        return dr.Read() ? dr.GetInt64(0).ToString() : "-";
                //Not sure what to return here, defaulting to nothing for now
                case AyaType.WorkOrderItem:
                case AyaType.WorkOrderItemExpense:
                case AyaType.WorkOrderItemLabor:
                case AyaType.WorkOrderItemLoan:
                case AyaType.WorkOrderItemPart:
                case AyaType.WorkOrderItemPartRequest:
                case AyaType.WorkOrderItemScheduledUser:
                case AyaType.WorkOrderItemTask:
                case AyaType.WorkOrderItemTravel:
                case AyaType.WorkOrderItemUnit:
                    return "-";
                default:
                    TABLE = "a" + ayaType.ToString().ToLowerInvariant();
                    break;

            }
            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) : "-";
        }





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

    }//eoc


}//eons

