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

namespace Sockeye.DataList
{
    internal class CustomerDataList : DataListProcessingBase, IDataListInternalCriteria
    {
        public CustomerDataList(long translationId)
        {
            DefaultListAType = SockType.Customer;
            SQLFrom = @"FROM ACUSTOMER LEFT JOIN AHEADOFFICE ON (ACUSTOMER.HEADOFFICEID = AHEADOFFICE.ID)";

            var RoleSet = BizRoles.GetRoleSet(DefaultListAType);
            AllowedRoles = RoleSet.ReadFullRecord | RoleSet.Change;
            DefaultColumns = new List<string>() { "customername", "customerphone1", "customeremail", "customerheadoffice" };
            DefaultSortBy = new Dictionary<string, string>() { { "customername", "+" } };

            FieldDefinitions = new List<DataListFieldDefinition>();



            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "CustomerName",
                FieldKey = "customername",
                SockType = (int)SockType.Customer,
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlIdColumnName = "acustomer.id",
                SqlValueColumnName = "acustomer.name",
                IsRowId = true
            });

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

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "Active",
                FieldKey = "customeractive",
                UiFieldDataType = (int)UiFieldDataType.Bool,
                SqlValueColumnName = "acustomer.active"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "DoNotContact",
                FieldKey = "DoNotContact",
                UiFieldDataType = (int)UiFieldDataType.Bool,
                SqlValueColumnName = "acustomer.donotcontact"
            });

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

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "WebAddress",
                FieldKey = "customerwebaddress",
                UiFieldDataType = (int)UiFieldDataType.HTTP,
                SqlValueColumnName = "acustomer.webaddress"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "CustomerAlertNotes",
                FieldKey = "CustomerAlertNotes",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "acustomer.alertnotes"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "HeadOffice",
                FieldKey = "customerheadoffice",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SockType = (int)SockType.HeadOffice,
                SqlIdColumnName = "aheadoffice.id",
                SqlValueColumnName = "aheadoffice.name"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "CustomerBillHeadOffice",
                FieldKey = "customerbillheadoffice",
                UiFieldDataType = (int)UiFieldDataType.Bool,
                SqlValueColumnName = "acustomer.billheadoffice"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "CustomerTechNotes",
                FieldKey = "customertechnotes",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "acustomer.technotes"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "CustomerAccountNumber",
                FieldKey = "customeraccountnumber",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "acustomer.accountnumber"
            });





            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "CustomerPhone1",
                FieldKey = "customerphone1",
                UiFieldDataType = (int)UiFieldDataType.PhoneNumber,
                SqlValueColumnName = "acustomer.phone1"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "CustomerPhone2",
                FieldKey = "customerphone2",
                UiFieldDataType = (int)UiFieldDataType.PhoneNumber,
                SqlValueColumnName = "acustomer.phone2"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "CustomerPhone3",
                FieldKey = "customerphone3",
                UiFieldDataType = (int)UiFieldDataType.PhoneNumber,
                SqlValueColumnName = "acustomer.phone3"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "CustomerPhone4",
                FieldKey = "customerphone4",
                UiFieldDataType = (int)UiFieldDataType.PhoneNumber,
                SqlValueColumnName = "acustomer.phone4"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "CustomerPhone5",
                FieldKey = "customerphone5",
                UiFieldDataType = (int)UiFieldDataType.PhoneNumber,
                SqlValueColumnName = "acustomer.phone5"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "CustomerEmail",
                FieldKey = "customeremail",
                UiFieldDataType = (int)UiFieldDataType.EmailAddress,
                SqlValueColumnName = "acustomer.emailaddress"
            });


            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "AddressPostalDeliveryAddress",
                FieldKey = "customerpostaddress",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "acustomer.postaddress"
            });


            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "AddressPostalCity",
                FieldKey = "customerpostcity",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "acustomer.postcity"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "AddressPostalStateProv",
                FieldKey = "customerpostregion",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "acustomer.postregion"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "AddressPostalCountry",
                FieldKey = "customerpostcountry",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "acustomer.postcountry"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "AddressPostalPostal",
                FieldKey = "customerpostcode",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "acustomer.postcode"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "AddressDeliveryAddress",
                FieldKey = "customeraddress",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "acustomer.address"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "AddressCity",
                FieldKey = "customercity",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "acustomer.city"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "AddressStateProv",
                FieldKey = "customerregion",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "acustomer.region"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "AddressCountry",
                FieldKey = "customercountry",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "acustomer.country"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "AddressPostal",
                FieldKey = "customeraddresspostal",
                UiFieldDataType = (int)UiFieldDataType.Text,
                SqlValueColumnName = "acustomer.addresspostal"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "AddressLatitude",
                FieldKey = "customerlatitude",
                UiFieldDataType = (int)UiFieldDataType.Decimal,
                SqlValueColumnName = "acustomer.latitude"
            });

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                TKey = "AddressLongitude",
                FieldKey = "customerlongitude",
                UiFieldDataType = (int)UiFieldDataType.Decimal,
                SqlValueColumnName = "acustomer.longitude"
            });



            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "CustomerCustom1", FieldKey = "customercustom1", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "acustomer.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "CustomerCustom2", FieldKey = "customercustom2", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "acustomer.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "CustomerCustom3", FieldKey = "customercustom3", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "acustomer.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "CustomerCustom4", FieldKey = "customercustom4", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "acustomer.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "CustomerCustom5", FieldKey = "customercustom5", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "acustomer.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "CustomerCustom6", FieldKey = "customercustom6", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "acustomer.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "CustomerCustom7", FieldKey = "customercustom7", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "acustomer.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "CustomerCustom8", FieldKey = "customercustom8", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "acustomer.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "CustomerCustom9", FieldKey = "customercustom9", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "acustomer.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "CustomerCustom10", FieldKey = "customercustom10", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "acustomer.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "CustomerCustom11", FieldKey = "customercustom11", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "acustomer.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "CustomerCustom12", FieldKey = "customercustom12", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "acustomer.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "CustomerCustom13", FieldKey = "customercustom13", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "acustomer.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "CustomerCustom14", FieldKey = "customercustom14", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "acustomer.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "CustomerCustom15", FieldKey = "customercustom15", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "acustomer.customfields" });
            FieldDefinitions.Add(new DataListFieldDefinition { TKey = "CustomerCustom16", FieldKey = "customercustom16", IsCustomField = true, IsFilterable = false, IsSortable = false, SqlValueColumnName = "acustomer.customfields" });


            //META COLUMNS

            FieldDefinitions.Add(new DataListFieldDefinition
            {
                FieldKey = "metaheadoffice",
                UiFieldDataType = (int)UiFieldDataType.InternalId,
                SqlIdColumnName = "aheadoffice.id",
                SqlValueColumnName = "aheadoffice.id",
                IsMeta = true //"I'm So Meta Even This Acronym"
            });
        }

        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)
            {
                //for now just show all customers of headoffice
                int nType = 0;
                if (!int.TryParse(crit[1], out nType)) return ret;
                SockType forType = (SockType)nType;
                if (forType != SockType.HeadOffice) return ret;

                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.HeadOffice:
                        {
                            DataListFilterOption FilterOption = new DataListFilterOption() { Column = "metaheadoffice" };
                            FilterOption.Items.Add(new DataListColumnFilter() { value = crit[0], op = DataListFilterComparisonOperator.Equality });
                            ret.Add(FilterOption);
                        }
                        break;

                }
            }
            return ret;
        }

    }//eoc
}//eons