using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using C1.Data.Transactions;
using C1.LiveLinq;
using C1.Silverlight.Data.RiaServices;

namespace TransactionsSilverlight
{
    public partial class OrderWindow : ChildWindow
    {
        // This class is used as eру result item type of an order details view defined below.
        public class OrderDetailsInfo
        {
            public virtual int OrderID { get; set; }
            public virtual int ProductID { get; set; }
            public virtual decimal UnitPrice { get; set; }
            public virtual float Discount { get; set; }
            public virtual decimal Quantity { get; set; }
        }

        // All changes in this window are made in the scope of this client transaction.
        ClientTransaction _transaction;
        // The order being edited.
        Web.Order _order;
        Web.Customer _customerToAddOrderTo;

        public OrderWindow()
        {
            InitializeComponent();
        }

        public OrderWindow(Web.Order order, RiaClientCache cache, ClientTransaction transaction, Web.Customer customerToAddOrderTo)
        {
            InitializeComponent();
            _order = order;
            _customerToAddOrderTo = customerToAddOrderTo;

            // Create a child transaction. Rolling back the child transaction back will not roll back the parent transaction.
            _transaction = new ClientTransaction(transaction);

            // Create a proxy for the order that automatically opens the transaction scope
            // each time a value is assigned to a property; and use it for data binding.
            DataContext = _transaction.ScopeDataContext(order);

            // Define a live view of order details.
            var orderDetailsView = from od in order.Order_Details.AsLive(cache)
                                   select new OrderDetailsInfo
                                   {
                                       OrderID = od.OrderID,
                                       ProductID = od.ProductID,
                                       UnitPrice = od.UnitPrice,
                                       Discount = od.Discount,
                                       Quantity = od.Quantity
                                   };
            // All changes in this view are made in the transaction scope.
            orderDetailsView.SetTransaction(_transaction);
            orderDetailsGrid.ItemsSource = orderDetailsView;
        }
        private void ok_Click(object sender, RoutedEventArgs e)
        {
            // Add the order in the transaction scope and commit the transaction.

            if (_customerToAddOrderTo != null)
            {
                using (_transaction.Scope())
                {
                    _customerToAddOrderTo.Orders.Add(_order);
                }
            }
            _transaction.Commit();
            DialogResult = true;
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            // Cancel the changes made by this window.
            _transaction.Rollback();
            DialogResult = false;
        }

        private void ChildWindow_Closed(object sender, EventArgs e)
        {
            if (_transaction.State == TransactionState.Open)
                _transaction.Rollback(); // this is required because otherwise _transaction would not be immediately disposed if the window is closed by other means than PK/Cancel buttons
        }
    }
}