﻿using System;
using System.Collections.Generic;

namespace AsmodeeDigital.CrossPromo.Plugin.Domain
{
    public class AnalyticsScreenDisplayItem : AnalyticsItem
    {
        public string ScreenPrevious { get; set; }

        public string ScreenCurrent { get; set; }

        public uint ScreenCount { get; set; }

        public string ScreenPreviousNavAction { get; set; }

        public float TimeScreenPreviousSec { get; set; }

        public Product Product { get; set; }

        public string Context { get; set; }

        public uint ScreenCountContext { get; set; }

        public AnalyticsScreenDisplayItem(Dictionary<string, object> headers)
            : base(headers)
        {
            MetricName = "screen_display";
            ScreenCurrent = "not_applicable";
        }

        public override string ToString()
        {
            string str = string.Format(
@"
- Previous screen : {0}
- Current screen : {1}
- Screen count : {2}
- Navigation action : {3}
- Time on previous screen : {4}",
                ScreenPrevious,
                ScreenCurrent,
                ScreenCount,
                ScreenPreviousNavAction,
                TimeScreenPreviousSec
            );

            if (Product != null) {
                str += string.Format("\n- Product name : {0}", Product.name);
            }

            if (Context != null) {
                str += string.Format(@"
- Context : {0}
- Screen count in context : {1}",
                    Context,
                    ScreenCountContext
                );
            }

            str += string.Format("\n{0}", base.ToString());

            return str;
        }

        public override IDictionary<string, object> ToDictionary()
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();

            foreach (string key in Headers.Keys) {
                dic[key] = Headers[key];
            }

            dic.Add("screen_previous", ScreenPrevious);
            dic.Add("screen_current", ScreenCurrent);
            dic.Add("screen_count", ScreenCount); // Should be handled by the app itself
            dic.Add("screen_previous_nav_action", ScreenPreviousNavAction);
            dic.Add("time_screen_previous_sec", TimeScreenPreviousSec);

            if (Product != null) {
                dic.Add("product_name", Product.name);
            }

            if (Context != null) {
                dic.Add("context", Context);
                dic.Add("screen_count_context", ScreenCountContext);
            }

            return dic;
        }
    }
}

