public static ProfitEntry CreateProfitEntry(
            Entity.CommodityType commodityType, Entity.MarketEntry localMarketEntry, Entity.MarketEntry remoteMarketEntry
            )
        {
            var profitEntry = new ProfitEntry()
            {
                CommodityGroup      = commodityType.CommodityGroup,
                CommodityType       = commodityType,
                LocalStation        = localMarketEntry.SpaceStation,
                RemoteStation       = remoteMarketEntry.SpaceStation,
                LocalSystem         = localMarketEntry.SpaceStation.SolarSystem,
                RemoteSystem        = remoteMarketEntry.SpaceStation.SolarSystem,
                BuyFromMarketPrice  = localMarketEntry.BuyFromStationPrice,
                SellToMarketPrice   = remoteMarketEntry.SellToStationPrice,
                Supply              = localMarketEntry.Supply,
                Demand              = remoteMarketEntry.Demand,
                LastBuyPriceUpdate  = localMarketEntry.LastUpdate,
                LastSellPriceUpdate = remoteMarketEntry.LastUpdate
            };

            if (profitEntry.BuyFromMarketPrice.HasValue && profitEntry.SellToMarketPrice.HasValue)
            {
                profitEntry.Profit = profitEntry.SellToMarketPrice - profitEntry.BuyFromMarketPrice;
                profitEntry.ProfitPerInvestment = (int)((double)profitEntry.Profit / (double)profitEntry.BuyFromMarketPrice * 100.0);
            }

            return(profitEntry);
        }
        public MarketEntryLine(Entity.CommodityType commodityType)
        {
            InitializeComponent();

            SetupToolTips();

            UpdateHighestBuyer(commodityType);
        }
        /// <summary>
        /// Runs over the list of market entries for the given commodity type and returns the entry with the highest buy price or the lowest
        /// sell price.
        /// </summary>
        /// <param name="commodityType">The commodity type to handle.</param>
        /// <param name="findSeller">True if the best seller shall be found, false if the best buyer shall be found.</param>
        /// <returns>The space station with the best price.</returns>
        public static Entity.MarketEntry FindBestMarketEntry(Entity.CommodityType commodityType, bool findSeller)
        {
            Func <Entity.MarketEntry, int?> sortFunction     = entry => findSeller ? entry.BuyFromStationPrice : entry.SellToStationPrice;
            Func <Entity.MarketEntry, bool> hasValueFunction = entry => findSeller ? entry.BuyFromStationPrice.HasValue : entry.SellToStationPrice.HasValue;

            var results = commodityType.MarketEntries.Where(hasValueFunction).OrderBy(sortFunction);

            if (results.Count() == 0)
            {
                return(null);
            }

            return(findSeller ? results.First() : results.Last());
        }
        public void UpdateHighestBuyer(Entity.CommodityType commodityType, Entity.SpaceStation currentStation = null)
        {
            IEnumerable <Entity.MarketEntry> marketEntries = commodityType.MarketEntries;

            if (currentStation != null)
            {
                // Do not inspect current station
                marketEntries = marketEntries.Where(x => x.SpaceStation != currentStation);
            }

            var maximumBuyPrice = marketEntries.Max(x => x.SellToStationPrice);

            if (!maximumBuyPrice.HasValue)
            {
                HighestBuyerLabel.Text = String.Empty;
            }
            else
            {
                var highestEntry = marketEntries.Where(x => x.SellToStationPrice == maximumBuyPrice);
                HighestBuyerLabel.Text = highestEntry.First().SellToStationPrice.ToString() + ": " +
                                         highestEntry.First().SpaceStation.Name + " (" + highestEntry.First().SpaceStation.SolarSystem.Name + ")";
            }
        }