using CloudBuilder.Core.Extensions;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudBuilder.Core.Service
{
    public class AbstractSearchCriteria
    {
        public int limit { get; set; } = 20;
        public int page { get; set; }
        public int clear { get; set; }
        public string? orderby { get; set; } = string.Empty;
        public string? orderbytag { get; set; } = string.Empty;

        public SearchFilter[]? Filter { get; set; }

        public bool IsModal { get; set; } = false;

        public string? ReportName { get; set; }

        public GridColumnInfo[]? GridColumns { get; set; }

        public SelectOption[]? SelectOptions { get; set; }

        public List<string> PackSearchCriteriaDisplay(ref List<TemplateHeader> headers, ref string sqlWhere)
        {
            return PackSearchCriteriaDisplay(ref headers, ref sqlWhere, null);
        }
        public List<string> PackSearchCriteriaDisplay(ref List<TemplateHeader> headers, ref string sqlWhere, Dictionary<string, Dictionary<string, object>> mappings)
        {
            List<string> l = new List<string>();

            if (this.GridColumns != null && this.GridColumns.Length > 0)
            {
                foreach (var column in this.GridColumns)
                {
                    if (!string.IsNullOrEmpty(column.title))
                    {
                        if (column.hidden != "N") continue;
                        if (string.IsNullOrEmpty(column.dataIndex) || string.IsNullOrEmpty(column.title) || column.dataIndex == "index") continue;
                        headers.Add(new TemplateHeader()
                        {
                            DisplayName = column.title,
                            BindingName = column.dataIndex!,
                            Format = column.format!,
                            Width = TemplateHeader.WidthAutoFit
                        });
                    }
                }
            }

            if (this.Filter != null && this.Filter.Length > 0)
            {
                sqlWhere = SearchFilter.ComposeWhereClause(this.Filter);

                foreach (SearchFilter filter in this.Filter)
                {
                    if (!string.IsNullOrEmpty(filter.Column) && headers.Where(x => filter.Column.StringToProerty() == x.BindingName).Count() > 0)
                    {
                        TemplateHeader h = headers.Where(x => filter.Column.StringToProerty() == x.BindingName).FirstOrDefault();
                        string v = filter.Value;

                        switch (filter.Operator)
                        {
                            case SearchFilterOperatorPolicy.BETWEEN:
                            case SearchFilterOperatorPolicy.DATE_BETWEEN:
                            case SearchFilterOperatorPolicy.DATETIME_BETWEEN:
                                if (!string.IsNullOrEmpty(filter.Value) && string.IsNullOrEmpty(filter.Value1))
                                    v = string.Format("[ {0} ~  ]", filter.Value);
                                break;
                        }


                        if (!string.IsNullOrEmpty(filter.Value) && !string.IsNullOrEmpty(filter.Value1)) v = string.Format("[ {0} ~ {1} ]", filter.Value, filter.Value1);
                        if (string.IsNullOrEmpty(filter.Value) && !string.IsNullOrEmpty(filter.Value1)) v = string.Format("[  ~ {0}]", filter.Value1);
                        if (filter.Values != null && filter.Values.Length > 0)
                        {
                            v = QuotedString.ToDelimiteredList(filter.Values);
                            if (mappings != null && mappings.Count() > 0)
                            {
                                List<string> m = new List<string>();
                                foreach (string mv in filter.Values)
                                {
                                    m.Add(mappings[h.BindingName][mv].ToString());
                                }
                                v = QuotedString.ToDelimiteredList(m.ToArray());
                            }
                        }

                        l.Add(string.Format("{0}: {1}", h.DisplayName, v));
                    }
                }
            }

            return l;
        }
    }

    public class TemplateHeader
    {
        public const string DateFormat1 = "yyyy-MM-dd";
        public const string DateTimeFormat1 = "yyyy-MM-dd HH:mm:ss";
        public const string IntFormat1 = "#,##0;(#,##0)";
        public const string DecimalFormat1 = "#,##0.00;(#,##0.00)";
        public const string PercentageFormat1 = "0.0%";
        public const double WidthAutoFit = -1;

        public TemplateHeader()
        {
            Width = WidthAutoFit;
        }
        public string Format { get; set; }
        public string DisplayName { get; set; }
        public string BindingName { get; set; }
        public string CellTypeFormatName { get; set; }
        public string DataValueFormatName { get; set; }
        public double Width { get; set; }
        public int? DisplayIndex { get; set; }

    }
}
