using System; using System.Linq; using System.Linq.Expressions; using System.Windows.Media.Imaging; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; using PRSDesktop.Forms; namespace PRSDesktop { internal class DynamicMapColumn : DynamicImageColumn where T : Entity, new() { private readonly string Latitude = ""; private readonly string Longitude = ""; private readonly BitmapImage milestone = Resources.milestone.AsBitmapImage(); private readonly string Timestamp = ""; public DynamicMapColumn(DynamicGrid grid, Expression> property) : base(r => null) { Latitude = CoreUtils.GetFullPropertyName(property, ".") + ".Latitude"; Longitude = CoreUtils.GetFullPropertyName(property, ".") + ".Longitude"; Timestamp = CoreUtils.GetFullPropertyName(property, ".") + ".Timestamp"; grid.HiddenColumns.Add(CoreUtils.GetPropertyExpression(Latitude)); grid.HiddenColumns.Add(CoreUtils.GetPropertyExpression(Longitude)); grid.HiddenColumns.Add(CoreUtils.GetPropertyExpression(Timestamp)); Property = property; Image = MapImage; Action = MapClick; Filters = new[] { "(Blank)", "(Not Blank)" }; FilterRecord = MapFilter; } private bool MapFilter(CoreRow row, string[] filter) { if (filter.Contains("(Blank)") && !HasLocation(row)) return true; if (filter.Contains("(Not Blank)") && HasLocation(row)) return true; return false; } public Expression> Property { get; } private bool HasLocation(CoreRow row) { if (row == null) return true; var lat = row.Get(Latitude); var lng = row.Get(Longitude); return lat != 0.0F && lng != 0.0F; } private BitmapImage MapImage(CoreRow row) { return HasLocation(row) ? milestone : null; } private bool MapClick(CoreRow row) { if (HasLocation(row)) { var form = new MapForm(row.Get(Latitude), row.Get(Longitude), row.Get(Timestamp)); form.ShowDialog(); } return false; } } }