using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using AyaNova.Biz;
namespace AyaNova.PickList
{
    internal class WidgetPickList : AyaPickList
    {
        public WidgetPickList()
        {
            DefaultListObjectType = AyaType.Widget;
            SQLFrom = "from awidget left outer join auser on (awidget.userid=auser.id)";
            AllowedRoles = BizRoles.GetRoleSet(DefaultListObjectType).Select;

            //Default template
            dynamic dTemplate = new JArray();

            dynamic cm = new JObject();
            cm.fld = "widgetname";
            dTemplate.Add(cm);

            cm = new JObject();
            cm.fld = "widgetserial";
            dTemplate.Add(cm);

            //todo: For testing only
            cm = new JObject();
            cm.fld = "widgettags";
            dTemplate.Add(cm);

            cm = new JObject();
            cm.fld = "username";
            dTemplate.Add(cm);

            base.DefaultTemplate = dTemplate.ToString(Newtonsoft.Json.Formatting.None);

            //NOTE: Due to the join, all the sql id and name fields that can conflict with the joined (in this case User) table need to be specified completely
            ColumnDefinitions = new List<AyaPickListFieldDefinition>();
            ColumnDefinitions.Add(new AyaPickListFieldDefinition
            {
                TKey = "Active",
                FieldKey = "widgetactive",
                ColumnDataType = UiFieldDataType.Bool,
                SqlValueColumnName = "awidget.active",
                IsActiveColumn = true
            });

            ColumnDefinitions.Add(new AyaPickListFieldDefinition
            {
                TKey = "WidgetName",
                FieldKey = "widgetname",
                //AyaObjectType = AyaType.Widget,
                ColumnDataType = UiFieldDataType.Text,
                SqlIdColumnName = "awidget.id",
                SqlValueColumnName = "awidget.name",
                IsRowId = true
            });

            ColumnDefinitions.Add(new AyaPickListFieldDefinition
            {
                TKey = "WidgetSerial",
                FieldKey = "widgetserial",
                ColumnDataType = UiFieldDataType.Integer,
                SqlValueColumnName = "awidget.serial"
            });

            ColumnDefinitions.Add(new AyaPickListFieldDefinition
            {
                FieldKey = "username",
                TKey = "User",
                ColumnDataType = UiFieldDataType.Text,
                SqlIdColumnName = "auser.id",
                SqlValueColumnName = "auser.name"
            });

            ColumnDefinitions.Add(new AyaPickListFieldDefinition
            {
                TKey = "Tags",
                FieldKey = "widgettags",
                ColumnDataType = UiFieldDataType.Tags,
                SqlValueColumnName = "awidget.tags"
            });
        }
    }//eoc
}//eons