using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Foresight.Data;

namespace IronIntel.Contractor.MapView
{
    public class JobManagement
    {
        public static JobSiteViewItem[] GetJobSite(string searchtext, int onroad, string companyid)
        {
            const string sql_1 = @"select JOBSITEID,JOBSITENAME,LATITUDE,LONGITUDE ,RADIUS,RADUIS_UOM,CONTRACTORID,COLOR,NOTES,STARTDATE,ENDDATE,POLYGON from JOBSITES where ENDDATE is null or ENDDATE>getdate() order by JOBSITENAME";
            const string sql_2 = @"select JOBSITEID,JOBSITENAME,LATITUDE,LONGITUDE ,RADIUS,RADUIS_UOM,CONTRACTORID,COLOR,NOTES,STARTDATE,ENDDATE,POLYGON from JOBSITES where (ENDDATE is null or ENDDATE>getdate()) and JOBSITENAME like {0} order by JOBSITENAME";

            FISqlConnection db = SystemParams.GetCompanyDbConnection(companyid);
            if (db == null)
            {
                return new JobSiteViewItem[0];
            }
            DataTable dt = null;
            if (string.IsNullOrWhiteSpace(searchtext))
            {
                dt = db.GetDataTableBySQL(sql_1);
            }
            else
            {
                dt = db.GetDataTableBySQL(sql_2, "%" + searchtext + "%");
            }
            List<JobSiteViewItem> list = new List<JobSiteViewItem>();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    JobSiteViewItem js = new JobSiteViewItem();
                    long JobSiteId = FIDbAccess.GetFieldInt(dr["JOBSITEID"], 0);
                    js.ID = FIDbAccess.GetFieldInt(dr["JOBSITEID"], 0);
                    js.Name = FIDbAccess.GetFieldString(dr["JOBSITENAME"], string.Empty);
                    js.Latitude = FIDbAccess.GetFieldDouble(dr["LATITUDE"], 0);
                    js.Longitude = FIDbAccess.GetFieldDouble(dr["LONGITUDE"], 0);
                    js.Radius = FIDbAccess.GetFieldDouble(dr["RADIUS"], 0);
                    js.Radius_UOM = FIDbAccess.GetFieldString(dr["RADUIS_UOM"], string.Empty);
                    if (string.IsNullOrWhiteSpace(js.Radius_UOM))
                        js.Radius_UOM = "Mile";
                    js.ContractorID = FIDbAccess.GetFieldString(dr["CONTRACTORID"], string.Empty);
                    js.ColorString = FIDbAccess.GetFieldString(dr["COLOR"], string.Empty);
                    System.Drawing.Color color = System.Drawing.Color.Orange;
                    try
                    {
                        color = System.Drawing.ColorTranslator.FromHtml(js.ColorString);
                    }
                    catch
                    {
                    }
                    js.Color = new IIColor() { Alpha = color.A, Red = color.R, Green = color.G, Blue = color.B };

                    js.Notes = FIDbAccess.GetFieldString(dr["NOTES"], string.Empty);
                    js.StartDate = FIDbAccess.GetFieldDateTime(dr["STARTDATE"], DateTime.MinValue);
                    js.EndDate = FIDbAccess.GetFieldDateTime(dr["ENDDATE"], DateTime.MinValue);
                    string polygon = FIDbAccess.GetFieldString(dr["POLYGON"], string.Empty);
                    js.Polygon = ConvertPolygonToPointItem(polygon);
                    MachineViewItem[] msiary = GetJobSiteMachines(JobSiteId, onroad, db);
                    if (msiary.Count() > 0)
                    {
                        js.Machines = msiary.OrderBy((m) => m.VIN).ToArray();
                    }
                    else
                    {
                        js.Machines = null;
                    }
                    list.Add(js);
                }
            }
            return list.ToArray();
        }

        private static MachineViewItem[] GetJobSiteMachines(long jobsiteid, int onroad, FISqlConnection db = null)
        {
            const string sql_m = @"select a.MACHINEID,b.VIN,b.MACHINENAME,b.MACHINENAME2,ONROAD from JOBSITEMACHINES a,MACHINES b where a.MACHINEID=b.MACHINEID and ISNULL(b.HIDE,0)=0 and a.JOBSITEID={0}";

            if (db == null)
                db = SystemParams.GetDbInstance();
            DataTable dt = null;
            dt = db.GetDataTableBySQL(sql_m, jobsiteid);

            if (dt.Rows.Count == 0)
            {
                return new MachineViewItem[0];
            }
            List<MachineViewItem> list = new List<MachineViewItem>();
            foreach (DataRow dr in dt.Rows)
            {
                MachineViewItem mi = new MachineViewItem();
                mi.ID = FIDbAccess.GetFieldInt64(dr["MACHINEID"], 0);
                mi.Name = FIDbAccess.GetFieldString(dr["MACHINENAME"], string.Empty);
                mi.Name2 = FIDbAccess.GetFieldString(dr["MACHINENAME2"], string.Empty);
                mi.VIN = FIDbAccess.GetFieldString(dr["VIN"], string.Empty);
                mi.Onroad = FIDbAccess.GetFieldInt(dr["ONROAD"], 0);
                if (onroad < 0 || onroad == mi.Onroad)
                    list.Add(mi);
            }
            return list.ToArray();
        }

        private static PostionItem[] ConvertPolygonToPointItem(string polygon)
        {
            if (string.IsNullOrWhiteSpace(polygon))
                return null;

            List<PostionItem> list = new List<PostionItem>();
            var polygons = polygon.Split(';');
            foreach (var py in polygons)
            {
                PostionItem pi = new PostionItem();
                var sap = py.Split(',');
                pi.Latitude = Convert.ToDouble(sap[0]);
                pi.Longitude = Convert.ToDouble(sap[1]);
                list.Add(pi);
            }
            return list.ToArray();
        }
    }
}
