using Xamarin.Forms;

namespace Pixiview.UI.Theme
{
    public class ThemeBase : ResourceDictionary
    {
        public const string TitleButton = nameof(TitleButton);
        public const string TitleLabel = nameof(TitleLabel);
        public const string FontIconUser = nameof(FontIconUser);
        public const string FontIconSparkles = nameof(FontIconSparkles);
        public const string FontIconOrder = nameof(FontIconOrder);
        public const string FontIconRefresh = nameof(FontIconRefresh);
        public const string FontIconLove = nameof(FontIconLove);
        public const string FontIconNotLove = nameof(FontIconNotLove);
        public const string FontIconOption = nameof(FontIconOption);
        public const string FontIconDownload = nameof(FontIconDownload);
        public const string FontIconFavorite = nameof(FontIconFavorite);

        public const string StatusBarStyle = nameof(StatusBarStyle);
        public const string WindowColor = nameof(WindowColor);
        public const string TextColor = nameof(TextColor);
        public const string SubTextColor = nameof(SubTextColor);
        public const string MainColor = nameof(MainColor);
        public const string MainTextColor = nameof(MainTextColor);
        public const string SubColor = nameof(SubColor);
        public const string MaskColor = nameof(MaskColor);
        public const string NavColor = nameof(NavColor);
        public const string NavSelectedColor = nameof(NavSelectedColor);

        public const string IconLightFontFamily = nameof(IconLightFontFamily);
        public const string IconRegularFontFamily = nameof(IconRegularFontFamily);
        public const string IconSolidFontFamily = nameof(IconSolidFontFamily);
        public const string IconLeft = nameof(IconLeft);
        public const string FontSizeTitle = nameof(FontSizeTitle);
        public const string FontSizeTitleIcon = nameof(FontSizeTitleIcon);
        //public const string Horizon10 = nameof(Horizon10);
        public const string ScreenBottomPadding = nameof(ScreenBottomPadding);
        public const string NavigationBarHeight = nameof(NavigationBarHeight);
        public const string IconRefresh = nameof(IconRefresh);
        public const string IconOption = nameof(IconOption);
        public const string IconDownload = nameof(IconDownload);

        protected void InitResources()
        {
            Add(FontSizeTitle, StyleDefinition.FontSizeTitle);
            Add(FontSizeTitleIcon, StyleDefinition.FontSizeTitleIcon);
            //Add(Horizon10, StyleDefinition.Horizon10);
            Add(ScreenBottomPadding, StyleDefinition.ScreenBottomPadding);
            Add(IconRefresh, StyleDefinition.IconRefresh);
            Add(IconOption, StyleDefinition.IconOption);
            Add(IconDownload, StyleDefinition.IconDownload);

            if (App.ExtraResources != null)
            {
                foreach (var kv in App.ExtraResources)
                {
                    Add(kv.Key, kv.Value);
                }
            }

            var mainColor = (Color)this[MainTextColor];
            var solidFontFamily = (string)this[IconSolidFontFamily];

            Add(TitleLabel, new Style(typeof(Label))
            {
                Setters =
                {
                    new Setter { Property = View.VerticalOptionsProperty, Value = LayoutOptions.Center },
                    new Setter { Property = View.HorizontalOptionsProperty, Value = LayoutOptions.Fill },
                    new Setter { Property = Label.HorizontalTextAlignmentProperty, Value = TextAlignment.Center },
                    new Setter { Property = Label.FontSizeProperty, Value = StyleDefinition.FontSizeTitle },
                    new Setter { Property = Label.TextColorProperty, Value = mainColor }
                }
            });

            Add(TitleButton, new Style(typeof(Button))
            {
                Setters =
                {
                    new Setter { Property = Button.BorderWidthProperty, Value = 0.0 },
                    new Setter { Property = VisualElement.BackgroundColorProperty, Value = Color.Transparent },
                    new Setter { Property = Button.FontFamilyProperty, Value = solidFontFamily },
                    new Setter { Property = Button.FontSizeProperty, Value = StyleDefinition.FontSizeTitleIcon },
                    new Setter { Property = Button.TextColorProperty, Value = mainColor }
                }
            });


            Add(FontIconUser, GetSolidIcon(StyleDefinition.IconUser, solidFontFamily, mainColor));
            Add(FontIconSparkles, GetSolidIcon(StyleDefinition.IconSparkles, solidFontFamily, mainColor));
            Add(FontIconOrder, GetSolidIcon(StyleDefinition.IconOrder, solidFontFamily, mainColor));
            Add(FontIconRefresh, GetSolidIcon(StyleDefinition.IconRefresh, solidFontFamily, mainColor));
            Add(FontIconLove, GetSolidIcon(StyleDefinition.IconLove, solidFontFamily, StyleDefinition.ColorRedBackground));
            Add(FontIconNotLove, GetSolidIcon(StyleDefinition.IconLove, (string)this[IconRegularFontFamily], mainColor));
            Add(FontIconOption, GetSolidIcon(StyleDefinition.IconOption, solidFontFamily, mainColor));
            Add(FontIconDownload, GetSolidIcon(StyleDefinition.IconDownload, solidFontFamily, mainColor));
            Add(FontIconFavorite, GetSolidIcon(StyleDefinition.IconFavorite, solidFontFamily, mainColor));
        }

        private FontImageSource GetSolidIcon(string icon, string family, Color color)
        {
            return new FontImageSource
            {
                FontFamily = family,
                Glyph = icon,
                Size = StyleDefinition.FontSizeTitle,
                Color = color
            };
        }
    }
}
