public IMarketRuleResultIntent Apply(Fixture fixture, IMarketStateCollection oldState, IMarketStateCollection newState)
        {
         
            var result = new MarketRuleResultIntent();

            if (!fixture.IsMatchOver || oldState == null)
                return result;

            var markets = fixture.Markets.ToDictionary(m => m.Id);

            // get list of markets which are either no longer in snapshot or are in the snapshot and are not resulted
            // markets which were already priced (activated) should be ignored
            var marketsNotPresentInTheSnapshot = new List<IMarketState>();
            foreach (var mkt in newState.Markets)
            {
                IMarketState mkt_state = newState[mkt];
                if (!mkt_state.IsResulted && (!markets.ContainsKey(mkt_state.Id) || !markets[mkt_state.Id].IsResulted))
                    marketsNotPresentInTheSnapshot.Add(mkt_state);
            }
            
            foreach (var mkt_state in marketsNotPresentInTheSnapshot)
            {
                if (mkt_state.HasBeenActive)
                {
                    _logger.DebugFormat("market rule={0} => marketId={1} of {2} was priced during the fixture lifetime but has NOT been settled on match over.", 
                        Name, mkt_state.Id, fixture);
                    continue;
                }

                if (!mkt_state.HasBeenProcessed)
                {
                    _logger.DebugFormat("market rule={0} => marketId={1} of {2} was never passed to the plugin.", Name, mkt_state.Id, fixture);
                    continue;
                }

                var market = fixture.Markets.FirstOrDefault(m => m.Id == mkt_state.Id);
                if (market == null)
                {
                    _logger.DebugFormat("market rule={0} => marketId={1} of {2} is marked to be voided", Name, mkt_state.Id, fixture);

                    result.AddMarket(CreateSettledMarket(mkt_state), new MarketRuleAddIntent(MarketRuleAddIntent.OperationType.SETTLE_SELECTIONS));
                }
                else
                {
                    _logger.DebugFormat("market rule={0} => marketId={1} of {2} that was in the snapshot but wasn't resulted is marked to be voided", 
                        Name, market.Id, fixture);

                    Action<Market> action = x => x.Selections.ForEach(s => s.Status = SelectionStatus.Void);
                    MarketRuleEditIntent edit = new MarketRuleEditIntent(action, MarketRuleEditIntent.OperationType.CHANGE_SELECTIONS);
                    result.EditMarket(market, edit);
                }

            }

            return result;
        }
        public IMarketRuleResultIntent Apply(Fixture fixture, IMarketStateCollection oldState, IMarketStateCollection newState)
        {
            var result = new MarketRuleResultIntent();

            if (_includedSports.Contains(newState.Sport.ToLower()))
            {
                
                foreach (var mkt in fixture.Markets)
                {
                    string type = string.Format("{0}.{1}", newState.Sport, mkt.Type).ToLower();

                    if (_excludedMarketTypes.Contains(type))
                    {
                        _Logger.DebugFormat("market rule={0} => {1} of {2} is excluded from rule due its type={3}",
                            Name, mkt, fixture, mkt.Type);

                        continue;
                    }

                    var oldMarketState = oldState != null ? oldState[mkt.Id] : null;
                    var newMarketState = newState[mkt.Id];

                    if (oldMarketState == null)
                    {
                        //must be a snapshot then
                        if (newMarketState.IsActive)
                        {
                            //create market
                            _Logger.DebugFormat("market rule={0} => {1} of {2} is created", Name, mkt, fixture);
                            result.MarkAsUnRemovable(mkt);
                        }
                        else
                        {
                            //dont create market
                            _Logger.DebugFormat("market rule={0} => {1} of {2} is not created", Name, mkt, fixture);
                            result.MarkAsRemovable(mkt);
                        }
                    }
                    else
                    {
                        if (oldMarketState.HasBeenActive) continue;
                        if (newMarketState.IsActive)
                        {
                            //create
                            Action<Market> editMarketAction = (m =>
                            {
                                if (newMarketState.TagsCount == 0)
                                    return;

                                foreach (var tagKey in newMarketState.TagKeys)
                                {
                                    m.AddOrUpdateTagValue(tagKey, newMarketState.GetTagValue(tagKey));
                                }

                                foreach (var sel in m.Selections)
                                {
                                    var selState = newMarketState[sel.Id];
                                    foreach (var tagKey in selState.TagKeys)
                                    {
                                        sel.AddOrUpdateTagValue(tagKey, selState.GetTagValue(tagKey));
                                    }
                                }
                            });

                            var mri = new MarketRuleEditIntent(editMarketAction, MarketRuleEditIntent.OperationType.CHANGE_DATA);
                            _Logger.DebugFormat("market rule={0} => {1} of {2} is created", Name, mkt, fixture);
                            result.EditMarket(mkt, mri);
                        }
                        else
                        {
                            //dont create market
                            _Logger.DebugFormat("market rule={0} => {1} of {2} is not created", Name, mkt, fixture);
                            result.MarkAsRemovable(mkt);
                        }
                    }
                }                
            }

            return result;
        }
        /// <summary>
        /// This execution of the delta rule CAN remove single selections
        /// from a market if they haven't changes since last time.
        /// </summary>
        /// <param name="fixture"></param>
        /// <param name="mkt"></param>
        /// <param name="state"></param>
        /// <param name="intent"></param>
        private void ApplyDeltaRule_RemovingSelections(Fixture fixture, Market mkt, IMarketState state, MarketRuleResultIntent intent)
        {

            List<Selection> seln_changed = new List<Selection>();
            foreach (var seln in mkt.Selections)
            {
                // if we don't have the selection state, we can't
                // determine what has changed within the selection,
                // so we assume that everything has changed
                if (state.HasSelection(seln.Id) && state[seln.Id].IsEquivalentTo(seln, true))
                    continue;

                _logger.DebugFormat("market rule={0} => {1} of {2} has changed", Name, seln, mkt);
                seln_changed.Add(seln);
            }


            if (seln_changed.Count == 0)
            {
                if (state.IsEquivalentTo(mkt, true, false))
                {
                    _logger.DebugFormat("market rule={0} => {1} of {2} marked as removable", Name, mkt, fixture);
                    intent.MarkAsRemovable(mkt);
                }
                else
                {
                    Action<Market> action = x => x.Selections.Clear();
                    MarketRuleEditIntent edit = new MarketRuleEditIntent(action, MarketRuleEditIntent.OperationType.REMOVE_SELECTIONS);
                    intent.EditMarket(mkt, edit);
                }
            }
            else
            {
                if (seln_changed.Count() != mkt.Selections.Count())
                {
                    Action<Market> action = x => x.Selections.RemoveAll(y => !seln_changed.Contains(y));
                    MarketRuleEditIntent edit = new MarketRuleEditIntent(action, MarketRuleEditIntent.OperationType.REMOVE_SELECTIONS);
                    intent.EditMarket(mkt, edit);
                }
            }
        }