using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Hotel_Course_Project
{
    /// <summary>
    /// Логика взаимодействия для OrderChangeOrAddPage.xaml
    /// </summary>
    public partial class OrderChangeOrAddPage : Page
    {
        Order _order;
        Staff _hostel;
        public OrderChangeOrAddPage(Order order, Staff hostel)
        {
            InitializeComponent();
            DataContext = order;
            _order = order;
            _hostel = hostel;
            if (DataContext == null)
            {
                SOrderDateStart.Text = DateTime.Now.Date.ToString();
                SOrderDateEnd.DisplayDateStart = DateTime.Now.AddDays(1);
                SOrderDateEnd.DisplayDateEnd = DateTime.Now.AddMonths(12);
                PaySP.Visibility = Visibility.Collapsed;
                AddOrChangeBtn.Visibility = Visibility.Collapsed;
            }
            else
            {
                SClientBtn.Visibility = Visibility.Collapsed;
                SRoomBtn.Visibility = Visibility.Collapsed;
                SOrderDateEnd.IsEnabled = false;
                PayMakeBtn.Visibility = Visibility.Collapsed;
                AddOrChangeBtn.Content = "Отменить";
                AddOrChangeBtn.Content = "Назад";
            }
        }

        private void SClientBtn_Click(object sender, RoutedEventArgs e)
        {
            ClientAddOnOrderWindow clientAddOnOrder = new ClientAddOnOrderWindow();
            clientAddOnOrder.ShowDialog();
            if (clientAddOnOrder.DialogResult == true)
            {
                SClient.Text = "" + Helper.SelectedClientOnOrder.LName + " " + Helper.SelectedClientOnOrder.FName[0] + ".";
                if (Helper.SelectedClientOnOrder.MName != null)
                {
                    SClient.Text += Helper.SelectedClientOnOrder.MName[0] + ".";
                }
                SClientBtn.Content = "Изменить";
                SClientBtn.HorizontalAlignment = HorizontalAlignment.Right;
                SClientBtn.VerticalAlignment = VerticalAlignment.Center;
            }
            else
            {
                SClientBtn.Content = "Добавить";
                SClient.Text = String.Empty;
                SClientBtn.HorizontalAlignment = HorizontalAlignment.Stretch;
                SClientBtn.VerticalAlignment = VerticalAlignment.Stretch;
            }
        }
        private void SRoomBtn_Click(object sender, RoutedEventArgs e)
        {
            RoomAddOnOrderWindow roomAddOnOrder = new RoomAddOnOrderWindow();
            roomAddOnOrder.ShowDialog();
            if (roomAddOnOrder.DialogResult == true)
            {
                SRoom.Text = "№" + Helper.SelectedRoomOnOrder.NumRoom + " дневная оплата: " + Helper.SelectedRoomOnOrder.CostPerDay;
                SRoomBtn.Content = "Изменить";
                SRoomBtn.HorizontalAlignment = HorizontalAlignment.Right;
                SRoomBtn.VerticalAlignment = VerticalAlignment.Center;
            }
            else
            {
                SRoomBtn.Content = "Изменить";
                SRoomBtn.HorizontalAlignment = HorizontalAlignment.Stretch;
                SRoomBtn.VerticalAlignment = VerticalAlignment.Stretch;
                SRoom.Text = String.Empty;
            }
        }

        private void PayMakeBtn_Click(object sender, RoutedEventArgs e)
        {
            if (SOrderDateEnd.SelectedDate == null)
            {
                MessageBox.Show("ВЫберите конец периода съёма комнаты");
            }
            else if (SRoom.Text == string.Empty)
            {
                MessageBox.Show("Комната не выбрана");
            }
            else
            {
                DateTime dateEnd = (DateTime)SOrderDateEnd.SelectedDate;
                int dateDiff = dateEnd.Subtract(DateTime.Parse(SOrderDateStart.Text.ToString())).Days + 1;
                PayAddOnOrderWindow payAddOnOrder = new PayAddOnOrderWindow(dateDiff, (decimal)Helper.SelectedRoomOnOrder.CostPerDay);
                payAddOnOrder.ShowDialog();
                if (payAddOnOrder.DialogResult == true)
                {
                    PayMakeBtn.Visibility = Visibility.Hidden;
                    PaySP.Visibility = Visibility.Visible;
                    SPayEndSum.Text = DataBase.db.Pay.SingleOrDefault(item => item.Id == Helper.idNewPay).EndSum.ToString();
                    SPayType.Text = DataBase.db.Pay.SingleOrDefault(item => item.Id == Helper.idNewPay).PayType.Name.ToString();
                    SClientBtn.Visibility = Visibility.Collapsed;
                    SRoomBtn.Visibility = Visibility.Collapsed;
                    SOrderDateEnd.IsEnabled = false;
                    CancelOrderBtn.Content = "Отменить";
                    AddOrChangeBtn.Visibility = Visibility.Visible;
                }
                else
                {
                    PayMakeBtn.Visibility = Visibility.Visible;
                    PaySP.Visibility = Visibility.Hidden;
                    SPayEndSum.Text = string.Empty;
                    SPayType.Text = string.Empty;
                }
            }
        }

        private void AddOrChangeBtn_Click(object sender, RoutedEventArgs e)
        {
            if (DataContext == null)
            {
                Order order = new Order()
                {
                    Id_Client = Helper.SelectedClientOnOrder.Id,
                    Id_Staff = _hostel.Id,
                    Id_OrderStatus = 2,
                    Id_Pay = Helper.idNewPay,
                    DateStart = DateTime.Parse(SOrderDateStart.Text.ToString()),
                    DateEnd = (DateTime)SOrderDateEnd.SelectedDate,
                    Id_Room = Helper.SelectedRoomOnOrder.Id
                };
                DataBase.db.Order.Add(order);
                var lastId = DataBase.db.Order.OrderByDescending(item => item.Id).FirstOrDefault().Id;
                DataBase.db.SaveChanges();
                MessageBox.Show("Заказ добавлен");
                PChanger.MainFrame.GoBack();
            }
            else
            {
                PChanger.MainFrame.GoBack();
            }
        }

        private void CancelOrderBtn_Click(object sender, RoutedEventArgs e)
        {
            if (DataContext == null)
            {
                if (PayMakeBtn.Visibility == Visibility.Hidden)
                {
                    DataBase.db.Pay.Remove(Helper.pay);
                    DataBase.db.SaveChanges();
                    PChanger.MainFrame.GoBack();
                }
                else
                {
                    PChanger.MainFrame.GoBack();
                }
            }
            else
            {

                _order.Id_OrderStatus = 3;
                MessageBox.Show("Заказ отменён");
                DataBase.db.SaveChanges();
                PChanger.MainFrame.GoBack();
            }
        }

    }
}
