namespace Stove.StoveSDK
{
    using System;
    using System.Collections.Generic;
    using MiniJSON;

    /**
     * View's UI configuration information.
     * @param DisplayType View's display type.
     * @param bottomCloseButtonType View's bottom close button type.
     * @param EnableNavigation Enable navigation.
     * @param EnableTopCloseButton Enable top close button.
     * @see DisplayType
     * @see BottomCloseButtonType
     * @since 2.0.0
     */
    public class ViewConfiguration
    {
        /**
         * View's display type.
         * @see DisplayType
         */
        public DisplayType DisplayType { get; }

        /**
         * View's bottom close button type.
         * @see BottomCloseButtonType
         * @since 2.0.0
         */
        public BottomCloseButtonType BottomCloseButtonType { get; }

        /**
         * Enable navigation.
         * @since 2.0.0
         */
        public bool EnableNavigation { get; }

        /**
         * Enable top close button.
         * @since 2.0.0
         */
        public bool EnableTopCloseButton { get; }

        /**
         * View's navigation
         * @see ViewNavigation
         * @since 2.1.0
         */
        public ViewNavigation ViewNavigation { get; }

        /**
         * View's top close button Image type.
         * @see TopCloseButtonType
         * @since 2.1.0
         */
        public TopCloseButtonType TopCloseButtonType { get; }

        /**
         * Close Button Image
         * @see CloseButtonImage
         * @since 2.1.0
         */
        public CloseButtonImage CloseButtonImage { get; }

       /**
        * Do not show again for 1~n days. (If -1, it means never show again.)
        * @see disallowedDay
        * @since 2.6.0
        */
        public int DisallowedDay { get; }

        public ViewConfiguration(DisplayType displayType, BottomCloseButtonType bottomCloseButtonType, bool enableNavigation, bool enableTopCloseButton, ViewNavigation viewNavigation, TopCloseButtonType topCloseButtonType, CloseButtonImage closeButtonImage, int disallowedDay)
        {
            DisplayType = displayType;
            BottomCloseButtonType = bottomCloseButtonType;
            EnableNavigation = enableNavigation;
            EnableTopCloseButton = enableTopCloseButton;
            ViewNavigation = viewNavigation;
            TopCloseButtonType = topCloseButtonType;
            CloseButtonImage = closeButtonImage;
            DisallowedDay = disallowedDay;
        }

        public ViewConfiguration()
        {
            DisplayType = DisplayType.Partial;
            BottomCloseButtonType = BottomCloseButtonType.None;
            EnableNavigation = true;
            EnableTopCloseButton = true;
            ViewNavigation = new ViewNavigation();
            TopCloseButtonType = TopCloseButtonType.Template1;
            CloseButtonImage = null;
            DisallowedDay = -1;
        }

        /**
         * Dictionary deserialization.
         * @since 2.0.0
         */
        public ViewConfiguration(Dictionary<string, object> dictionary)
        {
            DisplayType = (DisplayType) Convert.ToInt16(dictionary["displayType"]);
            BottomCloseButtonType = (BottomCloseButtonType) Convert.ToInt16(dictionary["bottomCloseButtonType"]);
            EnableNavigation = Convert.ToBoolean(dictionary["enableNavigation"]);
            EnableTopCloseButton = Convert.ToBoolean(dictionary["enableTopCloseButton"]);

            if(dictionary.ContainsKey("navigation"))
			{
                Dictionary<string, object> navigation = dictionary["navigation"] as Dictionary<string, object>;
                ViewNavigation = new ViewNavigation(navigation);
            }

            if (dictionary.ContainsKey("topCloseButtonType"))
			{
                TopCloseButtonType = (TopCloseButtonType)Convert.ToInt16(dictionary["topCloseButtonType"]);
            }
            else
			{
                TopCloseButtonType = TopCloseButtonType.Template1;
            }

            if (dictionary.ContainsKey("closeButtonImage"))
			{
                Dictionary<string, object> closeButtonImage = dictionary["closeButtonImage"] as Dictionary<string, object>;
                CloseButtonImage = new CloseButtonImage(closeButtonImage);
            }

            if (dictionary.ContainsKey("disallowedDay"))
			{
                DisallowedDay = Convert.ToInt32(dictionary["disallowedDay"]);
            }

        }

        /**
         * Dictionary serialization.
         * @since 2.0.0
         */
        public Dictionary<string, object> ToDictionary()
        {
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            dictionary.Add("displayType", (int) DisplayType);
            dictionary.Add("bottomCloseButtonType", (int) BottomCloseButtonType);
            dictionary.Add("enableNavigation", EnableNavigation);
            dictionary.Add("enableTopCloseButton", EnableTopCloseButton);

            if (ViewNavigation != null)
            {
                dictionary.Add("navigation", ViewNavigation.ToDictionary());
            }

            dictionary.Add("topCloseButtonType", (int)TopCloseButtonType);

            if (CloseButtonImage != null)
            {
                dictionary.Add("closeButtonImage", CloseButtonImage.ToDictionary());
            }

            if (DisallowedDay != null)
            {
               dictionary.Add("disallowedDay", DisallowedDay);
            }

            return dictionary;
        }

        /**
         * JSON serialization.
         * @since 2.0.0
         */
        public string ToJSONString()
        {
            Dictionary<string, object> dictionary = ToDictionary();

            return Json.Serialize(dictionary);
        }

        /**
         * Recommended full screen type on the stove.
         * @return View configuration.
         * @since 2.0.0
         */
        public static ViewConfiguration Full()
        {
            return new ViewConfiguration(DisplayType.Full, BottomCloseButtonType.Image, true, false, new ViewNavigation(), TopCloseButtonType.Template1, null, -1);
        }

        /**
         * Recommended partial screen type on the stove.
         * @return View configuration.
         * @since 2.0.0
         */
        public static ViewConfiguration Partial()
        {
            return new ViewConfiguration(DisplayType.Partial, BottomCloseButtonType.None, true, true, new ViewNavigation(), TopCloseButtonType.Template1, null, -1);
        }
    }
}
