protected static void AssignCampaignDates(SwrveBaseCampaign campaign, Dictionary <string, object> campaignData)
        {
            DateTime initDate = SwrveHelper.UnixEpoch;

            campaign.StartDate = initDate.AddMilliseconds(MiniJsonHelper.GetLong(campaignData, START_DATE_KEY));
            campaign.EndDate   = initDate.AddMilliseconds(MiniJsonHelper.GetLong(campaignData, END_DATE_KEY));
        }
        /// <summary>
        /// Load an in-app campaign from a JSON response.
        /// </summary>
        /// <param name="campaignData">
        /// JSON object with the individual campaign data.
        /// </param>
        /// <param name="initialisedTime">
        /// Time that the SDK was initialised. Used for rules checking.
        /// </param>
        /// <param name="assetPath">
        /// Path to the folder that will store all the assets.
        /// </param>
        /// <returns>
        /// Parsed in-app campaign.
        /// </returns>
        public static SwrveBaseCampaign LoadFromJSON(SwrveSDK sdk, Dictionary <string, object> campaignData, DateTime initialisedTime, SwrveQAUser qaUser)
        {
            int id = MiniJsonHelper.GetInt(campaignData, ID_KEY);
            SwrveBaseCampaign campaign = null;

            if (campaignData.ContainsKey(CONVERSATION_KEY))
            {
                campaign = SwrveConversationCampaign.LoadFromJSON(sdk, campaignData, id, initialisedTime);
            }
            else if (campaignData.ContainsKey(MESSAGES_KEY))
            {
                campaign = SwrveMessagesCampaign.LoadFromJSON(sdk, campaignData, id, initialisedTime, qaUser);
            }

            if (campaign == null)
            {
                return(null);
            }
            campaign.Id = id;

            AssignCampaignTriggers(campaign, campaignData);
            campaign.MessageCenter = campaignData.ContainsKey(MESSAGE_CENTER_KEY) && (bool)campaignData[MESSAGE_CENTER_KEY];

            if ((!campaign.MessageCenter) && (campaign.GetTriggers().Count == 0))
            {
                campaign.LogAndAddReason("Campaign [" + campaign.Id + "], has no triggers. Skipping this campaign.", qaUser);
                return(null);
            }

            AssignCampaignRules(campaign, campaignData);
            AssignCampaignDates(campaign, campaignData);
            campaign.Subject = campaignData.ContainsKey(SUBJECT_KEY) ? (string)campaignData[SUBJECT_KEY] : "";

            if (campaign.MessageCenter)
            {
                SwrveLog.Log(string.Format("message center campaign: {0}, {1}", campaign.GetType(), campaign.subject));
            }

            return(campaign);
        }
        protected static void AssignCampaignTriggers(SwrveBaseCampaign campaign, Dictionary <string, object> campaignData)
        {
            IList <object> jsonTriggers = (IList <object>)campaignData [TRIGGERS_KEY];

            for (int i = 0, j = jsonTriggers.Count; i < j; i++)
            {
                object jsonTrigger = jsonTriggers [i];
                if (jsonTrigger.GetType() == typeof(string))
                {
                    jsonTrigger = new Dictionary <string, object> {
                        { EVENT_NAME_KEY, jsonTrigger },
                        { CONDITIONS_KEY, new Dictionary <string, object>() }
                    };
                }

                try {
                    SwrveTrigger trigger = SwrveTrigger.LoadFromJson((IDictionary <string, object>)jsonTrigger);
                    campaign.GetTriggers().Add(trigger);
                } catch (Exception e) {
                    SwrveLog.LogError("Unable to parse SwrveTrigger from json " + Json.Serialize(jsonTrigger) + ", " + e);
                }
            }
        }
        protected static void AssignCampaignRules(SwrveBaseCampaign campaign, Dictionary <string, object> campaignData)
        {
            Dictionary <string, object> rules = (Dictionary <string, object>)campaignData [RULES_KEY];

            campaign.RandomOrder = ((string)rules [DISPLAY_ORDER_KEY]).Equals(RANDOM_KEY);

            if (rules.ContainsKey(DISMISS_AFTER_VIEWS_KEY))
            {
                int totalImpressions = MiniJsonHelper.GetInt(rules, DISMISS_AFTER_VIEWS_KEY);
                campaign.maxImpressions = totalImpressions;
            }

            if (rules.ContainsKey(DELAY_FIRST_MESSAGE_KEY))
            {
                campaign.delayFirstMessage       = MiniJsonHelper.GetInt(rules, DELAY_FIRST_MESSAGE_KEY);
                campaign.showMessagesAfterLaunch = campaign.swrveInitialisedTime + TimeSpan.FromSeconds(campaign.delayFirstMessage);
            }

            if (rules.ContainsKey(MIN_DELAY_BETWEEN_MESSAGES_KEY))
            {
                int minDelay = MiniJsonHelper.GetInt(rules, MIN_DELAY_BETWEEN_MESSAGES_KEY);
                campaign.minDelayBetweenMessage = minDelay;
            }
        }