private void ValidateCoupon(object sender, RoutedEventArgs e)
        {
            string input       = couponTextBox.Text;
            bool   validCoupon = Coupon.IsValid(input);

            //This will update our coupon if the discount will leave the user with a better price than before
            if (validCoupon && currentCoupon != null)
            {
                Coupon compare = Coupon.DeserializeCoupons().First(x => x.Code == input);
                if (compare.Discount < currentCoupon.Discount)
                {
                    currentCoupon = compare;
                }
                else
                {
                    MessageBox.Show("Din nuvarande rabattkod har en bättre rabatt än den du nyligen matade in!");
                    return;
                }
            }

            currentCoupon = validCoupon ? Coupon.DeserializeCoupons().First(x => x.Code == input) : null;

            hasDiscount = currentCoupon != null;

            if (currentCoupon == null)
            {
                MessageBox.Show("Den inmatade rabattkoden är ej giltig, försök igen.");
                return;
            }
            else
            {
                MessageBox.Show("Du har aktiverat en giltig rabattkod.");
                UpdateCartListBox();
            }
        }
        private void Start()
        {
            System.Globalization.CultureInfo.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

            ShopUtils.CreateFiles();

            productList  = ShopUtils.DeserializeProducts(ShopUtils.GetFilePath("Products.json"));
            shoppingCart = ShopUtils.DeserializeProducts(ShopUtils.GetFilePath("Cart.json"));
            couponList   = Coupon.DeserializeCoupons();

            #region Custom brushes
            // declare a brushconverter to convert a hex color code string to a Brush color
            BrushConverter brushConverter  = new System.Windows.Media.BrushConverter();
            Brush          backgroundBrush = (Brush)brushConverter.ConvertFromString("#2F3136");
            Brush          listBoxBrush    = (Brush)brushConverter.ConvertFromString("#36393F");
            Brush          textBoxBrush    = (Brush)brushConverter.ConvertFromString("#40444B");
            Brush          expanderBrush   = (Brush)brushConverter.ConvertFromString("#202225");
            #endregion

            // Set Window properties
            Title  = "Butiken";
            Width  = 900;
            Height = 600;
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            // Changes the Window icon
            Uri iconUri = new Uri("Images/Ica.png", UriKind.RelativeOrAbsolute);
            this.Icon = BitmapFrame.Create(iconUri);

            // Scrolling
            ScrollViewer root = new ScrollViewer();
            root.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
            Content = root;

            // Main grid definition
            Grid mainGrid = new Grid();
            root.Content = mainGrid;
            mainGrid.RowDefinitions.Add(new RowDefinition());
            // First column contains the shoppingcart and product assortment.
            mainGrid.ColumnDefinitions.Add(new ColumnDefinition());
            // The second column displays the selected product and upon payment displays the receipt
            mainGrid.ColumnDefinitions.Add(new ColumnDefinition());
            mainGrid.Background = backgroundBrush;

            #region grid definiton for the Expander in leftGrid
            Grid expanderCartGrid = new Grid();
            expanderCartGrid.RowDefinitions.Add(new RowDefinition {
                Height = GridLength.Auto
            });
            expanderCartGrid.RowDefinitions.Add(new RowDefinition {
                Height = GridLength.Auto
            });
            expanderCartGrid.RowDefinitions.Add(new RowDefinition {
                Height = GridLength.Auto
            });
            expanderCartGrid.RowDefinitions.Add(new RowDefinition {
                Height = GridLength.Auto
            });
            expanderCartGrid.RowDefinitions.Add(new RowDefinition {
                Height = GridLength.Auto
            });
            expanderCartGrid.RowDefinitions.Add(new RowDefinition());
            expanderCartGrid.RowDefinitions.Add(new RowDefinition {
                Height = GridLength.Auto
            });
            expanderCartGrid.ColumnDefinitions.Add(new ColumnDefinition());
            expanderCartGrid.ColumnDefinitions.Add(new ColumnDefinition());

            TextBlock cartTextBlock = ShopUtils.CreateTextBlock("Varukorg", 18, TextAlignment.Center);
            expanderCartGrid.Children.Add(cartTextBlock);
            Grid.SetRow(cartTextBlock, 0);
            Grid.SetColumn(cartTextBlock, 0);
            Grid.SetColumnSpan(cartTextBlock, 2);

            TextBlock discountTextBlock = ShopUtils.CreateTextBlock("Mata in rabattkod nedan", 12, TextAlignment.Center);
            expanderCartGrid.Children.Add(discountTextBlock);
            Grid.SetRow(discountTextBlock, 1);
            Grid.SetColumn(discountTextBlock, 0);
            Grid.SetColumnSpan(discountTextBlock, 1);

            // A combobox to display available coupons to the user
            couponComboBox = new ComboBox
            {
                HorizontalAlignment = HorizontalAlignment.Right,
                Height                   = 18,
                Margin                   = new Thickness(0, 5, 5, 5),
                BorderThickness          = new Thickness(0),
                VerticalContentAlignment = VerticalAlignment.Top,
                Padding                  = new Thickness(5, 1, 5, 0),
            };
            expanderCartGrid.Children.Add(couponComboBox);
            Grid.SetRow(couponComboBox, 1);
            Grid.SetColumn(couponComboBox, 1);
            // add a default index at 0, can be selected to clear the couponTextBox.Text
            couponComboBox.Items.Add("Dina rabattkoder");
            couponComboBox.SelectedIndex     = 0;
            couponComboBox.SelectionChanged += AddToCouponTextBox;
            // Adds all available coupons to the couponComboBox items from the couponList which recieves predetermined coupons from file
            foreach (var coupon in couponList)
            {
                couponComboBox.Items.Add(coupon.Code + " " + (100 - Math.Round(coupon.Discount * 100, 0)) + "%");
            }

            // A textbox for the user to enter coupon codes
            couponTextBox = new TextBox
            {
                Margin          = new Thickness(5),
                Background      = textBoxBrush,
                Foreground      = Brushes.White,
                BorderThickness = new Thickness(0),
                FontWeight      = FontWeights.SemiBold
            };
            expanderCartGrid.Children.Add(couponTextBox);
            Grid.SetRow(couponTextBox, 2);
            Grid.SetColumnSpan(couponTextBox, 1);

            ShopUtils.CreateButton("Använd rabattkod", expanderCartGrid, 2, 2, 1, ValidateCoupon);
            ShopUtils.CreateButton("Rensa varukorg", expanderCartGrid, row: 3, column: 0, columnspan: 1, ClearCartClick);
            ShopUtils.CreateButton("Spara varukorg", expanderCartGrid, 3, 1, 1, SaveCartClick);
            ShopUtils.CreateButton("Ta bort en vald produkt", expanderCartGrid, row: 4, column: 0, columnspan: 1, RemoveProductClick);
            ShopUtils.CreateButton("Ta bort varje vald produkt", expanderCartGrid, 4, 1, 1, RemoveAllSelectedProductsClick);

            // the cartListBox display all products in the shoppingCart list
            cartListBox = new ListBox
            {
                Margin              = new Thickness(5),
                VerticalAlignment   = VerticalAlignment.Stretch,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                MaxHeight           = 200,
                MinHeight           = 200,
                Background          = listBoxBrush,
                Foreground          = Brushes.White,
                BorderThickness     = new Thickness(0),
                FontWeight          = FontWeights.SemiBold
            };
            expanderCartGrid.Children.Add(cartListBox);
            Grid.SetRow(cartListBox, 5);
            Grid.SetColumnSpan(cartListBox, 2);

            sumTextBlock = ShopUtils.CreateTextBlock("Varukorgens summa: 0 kr", 12, TextAlignment.Left);
            expanderCartGrid.Children.Add(sumTextBlock);
            Grid.SetRow(sumTextBlock, 6);
            Grid.SetColumn(sumTextBlock, 0);
            Grid.SetColumnSpan(sumTextBlock, 1);

            ShopUtils.CreateButton("Till kassan", expanderCartGrid, 6, 1, 1, ShowReceipt);
            #endregion

            #region leftGrid definition
            Grid leftGrid = new Grid();
            mainGrid.Children.Add(leftGrid);
            leftGrid.ColumnDefinitions.Add(new ColumnDefinition());
            leftGrid.ColumnDefinitions.Add(new ColumnDefinition());
            leftGrid.RowDefinitions.Add(new RowDefinition {
                Height = GridLength.Auto
            });
            leftGrid.RowDefinitions.Add(new RowDefinition {
                Height = GridLength.Auto
            });
            leftGrid.RowDefinitions.Add(new RowDefinition {
                Height = GridLength.Auto
            });
            leftGrid.RowDefinitions.Add(new RowDefinition {
                Height = GridLength.Auto
            });
            leftGrid.RowDefinitions.Add(new RowDefinition {
                Height = GridLength.Auto
            });
            leftGrid.RowDefinitions.Add(new RowDefinition {
                Height = GridLength.Auto
            });
            leftGrid.RowDefinitions.Add(new RowDefinition());
            Grid.SetRow(leftGrid, 0);
            Grid.SetColumn(leftGrid, 0);

            // Expander definition, when expanded the expanderCartGrid will be displayed in the leftGrid
            cartExpander = new Expander
            {
                // sets the expanders content to the expanderCartGrid defined above
                Content    = expanderCartGrid,
                Header     = "Din varukorg 0 kr",
                FontWeight = FontWeights.SemiBold,
                Foreground = Brushes.White,
                Background = expanderBrush
            };
            leftGrid.Children.Add(cartExpander);
            Grid.SetRow(cartExpander, 0);
            Grid.SetColumn(cartExpander, 0);
            // when expanded the cartExpander's columnspan increases to take up two columns and when collapsed shrinks to one column
            cartExpander.Collapsed += DecreaseCartColumnSpan;
            cartExpander.Expanded  += IncreaseCartColumnSpan;
            // Update the cartListBox in the Expander to add items from the shoppingCart list
            UpdateCartListBox();

            TextBlock products = ShopUtils.CreateTextBlock("Produkter", 18, TextAlignment.Center);
            leftGrid.Children.Add(products);
            Grid.SetRow(products, 1);
            Grid.SetColumn(products, 0);
            Grid.SetColumnSpan(products, 2);

            TextBlock searchHeading = ShopUtils.CreateTextBlock("Sök efter produkt", 12, TextAlignment.Center);
            leftGrid.Children.Add(searchHeading);
            Grid.SetRow(searchHeading, 2);
            Grid.SetColumn(searchHeading, 0);
            Grid.SetColumnSpan(searchHeading, 2);

            // A textbox definition where the user can input a search term
            searchBox = new TextBox
            {
                Margin          = new Thickness(5),
                Background      = textBoxBrush,
                Foreground      = Brushes.White,
                BorderThickness = new Thickness(0),
                FontWeight      = FontWeights.SemiBold
            };
            leftGrid.Children.Add(searchBox);
            Grid.SetRow(searchBox, 3);
            Grid.SetColumnSpan(searchBox, 2);
            // when the searchbox text changes the ShowSearch event will run
            searchBox.TextChanged += ShowSearch;

            // A combobox which displays available categories
            categoryBox = new ComboBox
            {
                Margin          = new Thickness(5),
                BorderThickness = new Thickness(0),
                FontWeight      = FontWeights.SemiBold,
                Height          = 22,
            };
            leftGrid.Children.Add(categoryBox);
            Grid.SetRow(categoryBox, 4);
            Grid.SetColumn(categoryBox, 0);
            Grid.SetColumnSpan(categoryBox, 2);
            categoryBox.Items.Add("Välj kategori");
            categoryBox.SelectedIndex = 0;
            categoryList = ShopUtils.GenerateCategories(productList);
            // adds categories to the categoryBox
            foreach (string category in categoryList)
            {
                categoryBox.Items.Add(category);
            }
            categoryBox.SelectionChanged += ShowCategory;

            ShopUtils.CreateButton("Lägg till vald produkt", leftGrid, row: 5, column: 0, columnspan: 2, AddProductToCart);

            // Listbox definition, used to display the product assortment from the searchTermList
            productListBox = new ListBox
            {
                Margin              = new Thickness(5),
                VerticalAlignment   = VerticalAlignment.Stretch,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                MaxHeight           = 425,
                Background          = listBoxBrush,
                Foreground          = Brushes.White,
                BorderThickness     = new Thickness(0),
                FontWeight          = FontWeights.SemiBold
            };
            leftGrid.Children.Add(productListBox);
            Grid.SetRow(productListBox, 6);
            Grid.SetColumn(productListBox, 0);
            Grid.SetColumnSpan(productListBox, 2);
            // set the searchTerm to empty string in order to add every product from the productList upon start
            UpdateProductListBox("");
            // selecting an item in the listbox will display productinformation in the stackpanel to the right
            productListBox.SelectionChanged += DisplaySelectedProduct;
            #endregion

            #region rightStackPanel definition
            StackPanel rightStackPanel = new StackPanel();
            mainGrid.Children.Add(rightStackPanel);
            Grid.SetRow(rightStackPanel, 0);
            Grid.SetColumn(rightStackPanel, 1);

            productHeading        = ShopUtils.CreateTextBlock("Välj produkt", 18, TextAlignment.Center, rightStackPanel);
            productHeading.Margin = new Thickness(5, 30, 5, 32);

            imageGrid = new Grid();
            rightStackPanel.Children.Add(imageGrid);

            // sets a startup image
            currentImage = ShopUtils.CreateImage("Images/Ica.png", false);
            imageGrid.Children.Add(currentImage);

            productDescriptionHeading     = ShopUtils.CreateTextBlock("", 16, TextAlignment.Center, rightStackPanel);
            productDescription            = ShopUtils.CreateTextBlock("", 12, TextAlignment.Center, rightStackPanel);
            productDescription.FontWeight = FontWeights.Thin;
            productDescription.Margin     = new Thickness(30, 5, 30, 5);
            #endregion
        }