using Foresight.Fleet.Services.Asset;
using Foresight.Fleet.Services.User;
using IronIntel.Contractor.Machines;
using IronIntel.Services.Customers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IronIntel.Contractor
{
    public class ExportExcelManager
    {
        public static byte[] ExportRentals(string sessionid, string companyid, long machineid, DateTime from, DateTime to, string searchtext, string useriid, string sortPath, bool desc)
        {
            if (string.IsNullOrEmpty(companyid))
                companyid = SystemParams.CompanyID;

            AssetRentalInfo[] rentalinfos = FleetServiceClientHelper.CreateClient<AssetQueryClient>(companyid, sessionid).GetAssetRentals(companyid, searchtext, useriid);

            List<MachineRentalInfo> ls = new List<MachineRentalInfo>();
            foreach (AssetRentalInfo ri in rentalinfos)
            {
                MachineRentalInfo mi = new MachineRentalInfo();
                Helper.CloneProperty(mi, ri);
                mi.RentalRate = (decimal)ri.RentalRate;
                ls.Add(mi);
            }

            var rentals = ls.ToArray();
            if (machineid > 0)
                rentals = rentals.Where(m => m.MachineID == machineid).ToArray();

            rentals = rentals.Where(m => m.RentalDate >= from && m.RentalDate <= to).ToArray();

            if (!string.IsNullOrEmpty(sortPath))
            {
                if (desc)
                {
                    rentals = rentals.OrderByDescending(m =>
                    {
                        return OrderHandler(m, sortPath);
                    }).ToArray();
                }
                else
                {
                    rentals = rentals.OrderBy(m =>
                    {
                        return OrderHandler(m, sortPath);
                    }).ToArray();
                }
            }

            ExportToExcel ete = new ExportToExcel();

            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn() { ColumnName = "VIN/SN" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Asset Name" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Outside/Internal" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Rental Vendor" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Rental Rate" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Term" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Term Unit" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Rental Date On" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Project Return Date" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Return Date" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Purchase Order #" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Comments" });

            foreach (var r in rentals)
            {
                var dr = dt.NewRow();
                dr[0] = r.VIN;
                dr[1] = r.ShowName;
                dr[2] = r.Outside;
                dr[3] = r.Vendor;
                dr[4] = r.RentalRate;
                dr[5] = r.Term;
                dr[6] = r.TermUnit;
                dr[7] = r.RentalDateStr;
                dr[8] = r.ProjectReturnDateStr;
                dr[9] = r.ReturnDateStr;
                dr[10] = r.PONumber;
                dr[11] = r.Comments;
                dt.Rows.Add(dr);
            }
            double[] widths = new double[] { 180d, 180d, 180d, 150d, 150d, 150d, 150d, 150d, 150d, 150d, 150d, 200d };
            byte[] data = ete.CreateExcel(dt, null, widths, null);

            return data;
        }

        public static byte[] ExportRentalChanges(string sessionid, string companyid, long machineid, long rentalid, DateTime from, DateTime to, string searchtext, string useriid, string sortPath, bool desc)
        {
            if (string.IsNullOrEmpty(companyid))
                companyid = SystemParams.CompanyID;

            AssetRentalChangeHistoryInfo[] assetrentals = FleetServiceClientHelper.CreateClient<AssetQueryClient>(companyid, sessionid).GetAssetRentalChangeHistory(companyid, Convert.ToInt64(rentalid));
            List<RentalChangeHistoryInfo> rentalHistory = new List<RentalChangeHistoryInfo>();
            foreach (AssetRentalChangeHistoryInfo ari in assetrentals)
            {
                RentalChangeHistoryInfo mri = new RentalChangeHistoryInfo();
                Helper.CloneProperty(mri, ari);
                mri.RentalRate = (decimal)ari.RentalRate;
                rentalHistory.Add(mri);
            }

            if (machineid > 0)
                rentalHistory = rentalHistory.Where(m => m.MachineID == machineid).ToList();

            if (rentalid > 0)
                rentalHistory = rentalHistory.Where(m => m.RentalID == rentalid).ToList();

            rentalHistory = rentalHistory.Where(m => m.RentalDate >= from && m.RentalDate <= to).ToList();

            if (!string.IsNullOrEmpty(sortPath))
            {
                if (desc)
                {
                    rentalHistory = rentalHistory.OrderByDescending(m =>
                    {
                        return OrderHandler(m, sortPath);
                    }).ToList();
                }
                else
                {
                    rentalHistory = rentalHistory.OrderBy(m =>
                    {
                        return OrderHandler(m, sortPath);
                    }).ToList();
                }
            }

            ExportToExcel ete = new ExportToExcel();

            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn() { ColumnName = "Rental ID" });
            dt.Columns.Add(new DataColumn() { ColumnName = "User Name" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Last Update Date" });
            //dt.Columns.Add(new DataColumn() { ColumnName = "Operate Type" });
            dt.Columns.Add(new DataColumn() { ColumnName = "VIN/SN" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Asset Name" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Outside/Internal" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Rental Vendor" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Rental Rate" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Term" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Term Unit" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Rental Date On" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Project Return Date" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Return Date" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Purchase Order #" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Comments" });

            foreach (var r in rentalHistory)
            {
                var dr = dt.NewRow();
                dr[0] = r.RentalID;
                dr[1] = r.LastUpdateUserName;
                dr[2] = r.LastUpdateDateStr;
                //dr[3] = r.OperateType;
                dr[3] = r.VIN;
                dr[4] = r.ShowName;
                dr[5] = r.Outside;
                dr[6] = r.Vendor;
                dr[7] = r.RentalRate;
                dr[8] = r.Term;
                dr[9] = r.TermUnit;
                dr[10] = r.RentalDateStr;
                dr[11] = r.ProjectReturnDateStr;
                dr[12] = r.ReturnDateStr;
                dr[13] = r.PONumber;
                dr[14] = r.Comments;
                dt.Rows.Add(dr);
            }
            double[] widths = new double[] { 120d, 150d, 150d, 180d, 180d, 180d, 150d, 150d, 150d, 150d, 150d, 150d, 150d, 150d, 200d };
            byte[] data = ete.CreateExcel(dt, null, widths, null);

            return data;
        }

        public static byte[] ExportOdometerAdjustHistory(string sessionid, string companyid, long machineid, DateTime from, DateTime to, string sortPath, bool desc)
        {
            if (string.IsNullOrEmpty(companyid))
                companyid = SystemParams.CompanyID;
            AssetOdometerAdjustInfo[] odoHistory = FleetServiceClientHelper.CreateClient<AssetDataAdjustClient>(companyid, sessionid).GetOdometerAdjustmentHistory(companyid, machineid, from, to);
            if (!string.IsNullOrEmpty(sortPath))
            {
                if (desc)
                {
                    odoHistory = odoHistory.OrderByDescending(m =>
                    {
                        return OrderHandler(m, sortPath);
                    }).ToArray();
                }
                else
                {
                    odoHistory = odoHistory.OrderBy(m =>
                    {
                        return OrderHandler(m, sortPath);
                    }).ToArray();
                }
            }

            ExportToExcel ete = new ExportToExcel();

            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn() { ColumnName = "User Name" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Adjustment Time" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Odometer Entered" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Odometer UOM" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Odometer Time" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Notes" });

            foreach (var r in odoHistory)
            {
                var dr = dt.NewRow();
                dr[0] = r.UserName;
                dr[1] = r.AdjustmentLocalTime.ToString("MM/dd/yyyy HH:mm");
                dr[2] = r.Odometer;
                dr[3] = r.UOM;
                dr[4] = r.OdometerLocalTime.ToString("MM/dd/yyyy HH:mm");
                dr[5] = r.Notes;
                dt.Rows.Add(dr);
            }
            double[] widths = new double[] { 200d, 150d, 150d, 150d, 150d, 300d };
            byte[] data = ete.CreateExcel(dt, null, widths, null);

            return data;
        }

        public static byte[] ExportEngineHoursAdjustHistory(string sessionid, string companyid, long machineid, DateTime from, DateTime to, string sortPath, bool desc)
        {
            if (string.IsNullOrEmpty(companyid))
                companyid = SystemParams.CompanyID;
            AssetEngineHoursAdjustInfo[] ehHistory = FleetServiceClientHelper.CreateClient<AssetDataAdjustClient>(companyid, sessionid).GetEngineHoursAdjustmentHistory(companyid, machineid, from, to);
            if (!string.IsNullOrEmpty(sortPath))
            {
                if (desc)
                {
                    ehHistory = ehHistory.OrderByDescending(m =>
                    {
                        return OrderHandler(m, sortPath);
                    }).ToArray();
                }
                else
                {
                    ehHistory = ehHistory.OrderBy(m =>
                    {
                        return OrderHandler(m, sortPath);
                    }).ToArray();
                }
            }

            ExportToExcel ete = new ExportToExcel();

            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn() { ColumnName = "User Name" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Adjustment Time" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Engine Hours Entered" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Engine Hours Time" });
            dt.Columns.Add(new DataColumn() { ColumnName = "Notes" });

            foreach (var r in ehHistory)
            {
                var dr = dt.NewRow();
                dr[0] = r.UserName;
                dr[1] = r.AdjustmentLocalTime.ToString("MM/dd/yyyy HH:mm");
                dr[2] = r.EngineHours;
                dr[3] = r.EngineHoursLocalTime.ToString("MM/dd/yyyy HH:mm");
                dr[4] = r.Notes;
                dt.Rows.Add(dr);
            }
            double[] widths = new double[] { 200d, 150d, 150d, 150d, 300d };
            byte[] data = ete.CreateExcel(dt, null, widths, null);

            return data;
        }


        private static object OrderHandler(object obj, string sortPath)
        {
            Type type = obj.GetType();
            System.Reflection.PropertyInfo propertyInfo = type.GetProperty(sortPath);
            if (propertyInfo != null)
                return propertyInfo.GetValue(obj, null);
            return null;
        }
    }

}
