/// <summary>
        /// 
        ///     Remove markets from snapshot/update when: 
        ///     current market state is inactive AND the previous state was inactive too (with no change in status or name)
        /// 
        ///     NB: If there is a change in market's name or status then will not be removed
        /// 
        /// </summary>
        public IMarketRuleResultIntent Apply(Fixture fixture, IMarketStateCollection oldState, IMarketStateCollection newState)
        {
            var result = new MarketRuleResultIntent();

            var inactiveMarkets = fixture.Markets.Where(
                m => (oldState != null && oldState.HasMarket(m.Id) && oldState[m.Id].IsEqualTo(newState[m.Id]))
                        && (!m.IsActive && !oldState[m.Id].IsActive));

            foreach (var market in inactiveMarkets.ToList())
            {
                result.MarkAsRemovable(market);
                _Logger.DebugFormat("market rule={0} => {1} of fixtureId={2} is marked as removable", Name, market, fixture.Id);
            }

            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);
                }
            }
        }
        public void PostRuleProcessingTest()
        {

            // This test sets up a dummy market rule that removes
            // all the markets whose Id start with "REMOVE".
            // The test, after calling ApplyRules() on the MarketRuleManager
            // checks if the property "HasBeenProcessed" is set to true
            // for all the markets whose Id don't start with REMOVE.
            //
            // The HasBeenProcessed property is set to true only
            // when a market has been passed at least once to the
            // plugin, or in other words, the outcome of the 
            // MarketRuleManager hasn't remove it from the Fixture object.
            // This property is set on IUpdatableMarketState.ApplyPostRulesProcessing()

            Mock<IMarketRule> aRule = new Mock<IMarketRule>();
            aRule.Setup(x => x.Apply(It.IsAny<Fixture>(), It.IsAny<IMarketStateCollection>(), It.IsAny<IMarketStateCollection>()))
                .Returns((Fixture f, IMarketStateCollection nS, IMarketStateCollection oS) => 
                { 
                    MarketRuleResultIntent intent = new MarketRuleResultIntent();
                    foreach(var mkt in f.Markets)
                    {
                        if(mkt.Id.StartsWith("REMOVE"))
                            intent.MarkAsRemovable(mkt);
                    }

                    return intent;
                }
            );

            var settings = new Mock<ISettings>();
            var plugin = new Mock<IAdapterPlugin>();
            var stateprovider = new StateManager(settings.Object, plugin.Object);

            List<IMarketRule> rules = new List<IMarketRule> { aRule.Object };

            Fixture fixture = new Fixture { Id = "ABC", MatchStatus = "30"};

            Market testMkt = new Market { Id = "1"};
            fixture.Markets.Add(testMkt);

            testMkt = new Market { Id = "REMOVE-1" };
            fixture.Markets.Add(testMkt);

            testMkt = new Market { Id = "2" };
            fixture.Markets.Add(testMkt);

            testMkt = new Market { Id = "REMOVE-2" };
            fixture.Markets.Add(testMkt);

            MarketRulesManager manager = new MarketRulesManager(fixture.Id, stateprovider, rules);

            manager.ApplyRules(fixture);

            manager.CurrentState["1"].Should().NotBeNull();
            manager.CurrentState["1"].HasBeenProcessed.Should().BeTrue();

            manager.CurrentState["2"].Should().NotBeNull();
            manager.CurrentState["2"].HasBeenProcessed.Should().BeTrue();

            manager.CurrentState["REMOVE-1"].Should().NotBeNull();
            manager.CurrentState["REMOVE-1"].HasBeenProcessed.Should().BeFalse();

            manager.CurrentState["REMOVE-2"].Should().NotBeNull();
            manager.CurrentState["REMOVE-2"].HasBeenProcessed.Should().BeFalse();

            manager.CommitChanges();


            manager.CurrentState["1"].Should().NotBeNull();
            manager.CurrentState["1"].HasBeenProcessed.Should().BeTrue();

            manager.CurrentState["2"].Should().NotBeNull();
            manager.CurrentState["2"].HasBeenProcessed.Should().BeTrue();

            manager.CurrentState["REMOVE-1"].Should().NotBeNull();
            manager.CurrentState["REMOVE-1"].HasBeenProcessed.Should().BeFalse();

            manager.CurrentState["REMOVE-2"].Should().NotBeNull();
            manager.CurrentState["REMOVE-2"].HasBeenProcessed.Should().BeFalse();
        }
 /// <summary>
 /// This execution of the delta rule can only remove markets from the fixture.
 /// This is different from ApplyDeltaRule_RemovingSelections where selections can
 /// be removed from markets.
 /// 
 /// A market is then removed from a fixture if either its tags and all its selections
 /// haven't changed since last time.
 /// 
 /// If only a selection has changed, then entire market, with the remaining selections,
 /// remains untouched by the delta rule.
 /// </summary>
 /// <param name="fixture"></param>
 /// <param name="mkt"></param>
 /// <param name="state"></param>
 /// <param name="intent"></param>
 private void ApplyDeltaRule_RemovingMarkets(Fixture fixture, Market mkt, IMarketState state, MarketRuleResultIntent intent)
 {
     if (state.IsEquivalentTo(mkt, true, true))
     {
         intent.MarkAsRemovable(mkt);
         _logger.DebugFormat("market rule={0} => {1} of {2} marked as removable", Name, mkt, fixture);
     }
 }