using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using System.Threading.Tasks;

namespace LHDN.CRM.Models.ViewModels
{
    public abstract class DataTableViewModel
    {
        public int Draw { get; set; }
        public int Start { get; set; }
        public int Length { get; set; }
        public DTSearch Search { get; set; }
        public List<DTColumn> Columns { get; set; }
        public List<DTOrder> Order { get; set; }

        public string SortExpression
        {
            get
            {
                string strSortExpr = "";
                Regex r = new Regex("^[a-zA-Z]+$");
                if (Order != null)
                {
                    if (Order.Count == 0)
                    {
                        strSortExpr = string.Empty;
                    }                     
                    else
                     if(r.IsMatch(Columns[Order[0].Column].Data))
                    {
                        strSortExpr = Columns[Order[0].Column].Data;
                    }
                    else
                    {
                        strSortExpr = string.Empty;
                    }
                        
                }

                if (!string.IsNullOrEmpty(strSortExpr))
                {
                    if(r.IsMatch(Order[0].Dir))
                    {
                        strSortExpr += " " + Order[0].Dir;
                    }
                    else
                    {
                        strSortExpr = string.Empty;
                    }
                }

          
                return strSortExpr;
            }
        }

        public string SortDirection
        {
            get
            {
                string strSortDir = "";
                Regex r = new Regex("^[a-zA-Z]+$");
                if (!string.IsNullOrEmpty(strSortDir))
                    if (r.IsMatch(Order[0].Dir))
                    {
                        strSortDir = Order[0].Dir;
                    }
                    else
                    {
                        strSortDir = string.Empty;
                    }
          
                return strSortDir;
            }
        }

        public virtual int PageIndex
        {
            get
            {
                if (Length == 0) Length = 1;
                int intPageIndex = 0;
                intPageIndex = (Start / Length) + 1;
                return intPageIndex;
            }
        }
    }

    public class DTResultViewModel<T> where T : class
    {
        public int draw { get; set; }
        public int recordsTotal { get; set; }
        public int recordsFiltered { get; set; }
        public List<T> data { get; set; }
        public string valReturned { get; set; }
        public decimal decTotalAmt { get; set; }
        public List<WorkFlowIDs> WorkflowList { get; set; }
    }

    public class DTColumn
    {
        public string Data { get; set; }
        public string Name { get; set; }
        public bool Searchable { get; set; }
        public bool Orderable { get; set; }
        public DTSearch Search { get; set; }
    }

    public class DTSearch
    {
        public bool Regex { get; set; }
        public string Value { get; set; }
    }

    public class DTOrder
    {
        public int Column { get; set; }
        public string Dir { get; set; }
    }

}
