using System.Collections.Generic;
using System.Linq;
using Sockeye.Biz;
using Sockeye.Models;

namespace Sockeye.DataList
{
    internal class SubscriptionServerDataList : DataListProcessingBase, IDataListInternalCriteria
    {
        public SubscriptionServerDataList(long translationId)
        {
            DefaultListAType = SockType.SubscriptionServer;
            SQLFrom = @"FROM asubscriptionserver LEFT JOIN acustomer ON (asubscriptionserver.customerid = acustomer.id)";

            var RoleSet = BizRoles.GetRoleSet(DefaultListAType);
            AllowedRoles = RoleSet.ReadFullRecord | RoleSet.Change;
            DefaultColumns = new List<string>() { "SubServerSubExpire", "SubServerName", "Customer", "SubServerTrial", "SubServerLastHealthStatus", "SubServerLastHealthCheck", "SubServerDatacenter", "ServerState" };
            DefaultSortBy = new Dictionary<string, string>() { { "SubServerSubExpire", "+" } };
            FieldDefinitions = new List<DataListFieldDefinition>();

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "SubServerName",
                FieldKey = "SubServerName",
                SockType = (int)SockType.SubscriptionServer,
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlIdColumnName = "asubscriptionserver.id",
                SqlValueColumnName = "asubscriptionserver.name",
                IsRowId = true
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "ServerState",
                FieldKey = "ServerState",
                UiFieldDataType = (int)UiFieldDataType.Enum,
                EnumType = Sockeye.Util.StringUtil.TrimTypeName(typeof(ServerState).ToString()),
                SqlValueColumnName = "asubscriptionserver.serverstate"
            });


            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "Created",
                FieldKey = "Created",
                UiFieldDataType = (int)UiFieldDataType.DateTime,
                SqlValueColumnName = "asubscriptionserver.created"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "SubServerNotes",
                FieldKey = "SubServerNotes",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "asubscriptionserver.notes"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "SubServerDatacenter",
                FieldKey = "SubServerDatacenter",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "asubscriptionserver.datacenter"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "SubServerTimeZone",
                FieldKey = "SubServerTimeZone",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "asubscriptionserver.timezone"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "DatabaseID",
                FieldKey = "DatabaseID",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "asubscriptionserver.dbid"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "SubServerLastUpdated",
                FieldKey = "SubServerLastUpdated",
                UiFieldDataType = (int)UiFieldDataType.DateTime,
                SqlValueColumnName = "asubscriptionserver.lastupdated"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "SubServerSubExpire",
                FieldKey = "SubServerSubExpire",
                UiFieldDataType = (int)UiFieldDataType.DateTime,
                SqlValueColumnName = "asubscriptionserver.subscriptionexpire"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "SubServerLastHealthStatus",
                FieldKey = "SubServerLastHealthStatus",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "asubscriptionserver.lasthealthstatus"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "SubServerLastHealthCheck",
                FieldKey = "SubServerLastHealthCheck",
                UiFieldDataType = (int)UiFieldDataType.DateTime,
                SqlValueColumnName = "asubscriptionserver.lasthealthcheck"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "SubServerTrial",
                FieldKey = "SubServerTrial",
                UiFieldDataType = (int)UiFieldDataType.Bool,
                SqlValueColumnName = "asubscriptionserver.trial"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "SubServerTrialContact",
                FieldKey = "SubServerTrialContact",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "asubscriptionserver.trialcontact"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "SubServerTrialEmail",
                FieldKey = "SubServerTrialEmail",
                UiFieldDataType = (int)UiFieldDataType.EmailAddress,
                SqlValueColumnName = "asubscriptionserver.trialemail"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "SubServerTrialCompany",
                FieldKey = "SubServerTrialCompany",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "asubscriptionserver.trialcompany"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "SubServerOperatingSystem",
                FieldKey = "SubServerOperatingSystem",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "asubscriptionserver.operatingsystem"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "SubServerCustomerDomain",
                FieldKey = "SubServerCustomerDomain",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "asubscriptionserver.customersubdomain"
            });


            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "Customer",
                FieldKey = "Customer",
                SockType = (int)SockType.Customer,
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlIdColumnName = "asubscriptionserver.customerid",
                SqlValueColumnName = "acustomer.name"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "Tags",
                FieldKey = "subscriptionservertags",
                UiFieldDataType = (int)UiFieldDataType.Tags,
                SqlValueColumnName = "asubscriptionserver.tags"
            });

            //META column 
            FieldDefinitions.Add(new DataListFieldDefinition
            {
                FieldKey = "metacustomer",
                UiFieldDataType = (int)UiFieldDataType.InternalId,
                SqlIdColumnName = "asubscriptionserver.customerid",
                SqlValueColumnName = "asubscriptionserver.customerid",
                IsMeta = true
            });

        }

        public List<DataListFilterOption> DataListInternalCriteria(long currentUserId, AuthorizationRoles userRoles, string clientCriteria)
        {
            List<DataListFilterOption> ret = new List<DataListFilterOption>();

            //ClientCriteria format for this list is "OBJECTID,AYATYPE" 
            var crit = (clientCriteria ?? "").Split(',').Select(z => z.Trim()).ToArray();
            if (crit.Length > 1)
            {
                //will be filtered from different types, show all records from Customer and nothing else at this time 
                int nType = 0;
                if (!int.TryParse(crit[1], out nType)) return ret;
                SockType forType = (SockType)nType;
                if (forType != SockType.Customer) return ret;//only supports customer  for now see workorderdatalist for alts

                long lId = 0;
                if (!long.TryParse(crit[0], out lId)) return ret;
                if (lId == 0) return ret;

                //Have valid type, have an id, so filter away
                switch (forType)
                {
                    case SockType.Customer:
                        {
                            DataListFilterOption FilterOption = new DataListFilterOption() { Column = "metacustomer" };
                            FilterOption.Items.Add(new DataListColumnFilter() { value = crit[0], op = DataListFilterComparisonOperator.Equality });
                            ret.Add(FilterOption);
                        }
                        break;
                        // case AyaType.Project:
                        //     {
                        //         DataListFilterOption FilterOption = new DataListFilterOption() { Column = "metaproject" };
                        //         FilterOption.Items.Add(new DataListColumnFilter() { value = crit[0], op = DataListFilterComparisonOperator.Equality });
                        //         ret.Add(FilterOption);
                        //     }
                        //     break;
                }
            }
            return ret;
        }

    }//eoc
}//eons