﻿using System;
using System.Windows;
using System.Windows.Controls;
using BLL;

namespace Polymorphism_2 {
    public partial class MainWindow : Window {

        public MainWindow () {
            InitializeComponent();
        }

        public void RadioButton_Checked(object sender, RoutedEventArgs e) {
            /* Empty the ComboBoxes so we can refill it properly and disable
             * TxtAmount and CbRegion because we have no item selected
             * */
            CbProduct.ItemsSource = null;
            CbRegion.ItemsSource = null;
            TxtAmount.IsEnabled = false;
            CbRegion.IsEnabled = false;

            /*
             * Check wich RadioButton is checked and fill CbProduct and CbRegion.
             * */
            if (RadioBook.IsChecked.Value) {
                CbProduct.ItemsSource = Book.GetBooks();
                CbRegion.ItemsSource = Country.GetCountries();
            } else if (RadioCD.IsChecked.Value) {
                CbProduct.ItemsSource = MusicCD.GetCDs();
                CbRegion.ItemsSource = Continent.GetContinents();
            }
        }

        private void CbProduct_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            /*
             * When the selected item in CbProduct changes, we update the text field underneath it 
             * to display the serial number (for CD's) or the ISBN Number (for books). 
             * 
             * To access the IsbnNumber or SerialNumber properties, we need to cast it as one of
             * the classes.
             * */
            if (CbProduct.Items.Count > 0) { // only continue if CbProduct contains at least 1 item
                try {
                    if (CbProduct.SelectedItem.GetType() == typeof(Book))
                        LblInfo.Content = "Isbn: " + ((Product)CbProduct.SelectedItem).SerialNumber;
                    else
                        LblInfo.Content = "Serial: " + ((Product)CbProduct.SelectedItem).SerialNumber;

                    TxtAmount.IsEnabled = true;
                    CbRegion.IsEnabled = true;
                } catch (Exception ex) {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            } else { // CbProduct is empty, so clear LblInfo
                LblInfo.Content = string.Empty;
            }
        }

        private void CbRegion_SelectionChanged (object sender, SelectionChangedEventArgs e) {
            if (CbRegion.SelectedIndex != -1 & CbProduct.SelectedIndex != -1)
                BtnCalcTotal.IsEnabled = true;
            else
                BtnCalcTotal.IsEnabled = false;
        }

        private void BtnCheckStock_Click(object sender, RoutedEventArgs e) {
            /*
             * The AmountInStock value is compared to the text entered in the txtAmount 
             * field using the CheckStock method of the selected product and a MessageBox 
             * will show us the result of this comparison.
             * 
             * Every exception thrown will be catched and shown to the user via a 
             * MessagBox dialog.
             * */
            try {
                if (((Product)CbProduct.SelectedItem).CheckStock(int.Parse(TxtAmount.Text)))
                    MessageBox.Show("Sufficient stock", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
                else
                    MessageBox.Show("Insufficient stock", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void TxtAmount_Changed (object sender, TextChangedEventArgs e) {
            if (TxtAmount.Text != string.Empty)
                BtnCheckStock.IsEnabled = true;
            else
                BtnCheckStock.IsEnabled = false;
        }

        private void BtnCalcTotal_Click(object sender, RoutedEventArgs e) {
            try {
                MessageBox.Show(
                    $"The total cost, incl. shipping, of {Convert.ToInt32(TxtAmount.Text)} copy/copies of {((Product)CbProduct.SelectedItem).Name} is: " +
                    $"€ {((Product)CbProduct.SelectedItem).CalculateTotal(Convert.ToInt32(TxtAmount.Text), ((Region)CbRegion.SelectedItem).ShippingCost).ToString("n2")}",
                    "Kostprijs",
                    MessageBoxButton.OK,
                    MessageBoxImage.Information);
            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void BtnReset_Click(object sender, RoutedEventArgs e) {
            // Reset all elements that were altered back to their original values
            RadioBook.IsChecked = false;
            RadioCD.IsChecked = false;
            CbRegion.IsEnabled = false;
            TxtAmount.IsEnabled = false;

            CbProduct.ItemsSource = null;
            CbRegion.ItemsSource = null;

            LblRegion.Content = string.Empty;
            LblInfo.Content = string.Empty;
            TxtAmount.Text = string.Empty;
        }

        private void BtnExit_Click(object sender, RoutedEventArgs e) {
            Application.Current.Shutdown(); // Exists the application
        }
    }
}
