using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Cashier
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public bool IsEnabledCashier = false;
        public MainWindow()
        {
            InitializeComponent();
            ManagerFrame.MainFrame = this.MainFrame;
            ManagerFrame.MainFrame.Navigate(new StartWorkPage());
        }

        private void MainFrame_ContentRendered(object sender, EventArgs e)
        {
            BtnEndWorkShift.Visibility = ManagerFrame.MainFrame.CanGoBack ? Visibility.Visible : Visibility.Collapsed;
            BtnStartWorkShift.Visibility = ManagerFrame.MainFrame.CanGoBack ? Visibility.Visible : Visibility.Collapsed;
            BtnBlockCashier.Visibility = ManagerFrame.MainFrame.CanGoBack ? Visibility.Visible : Visibility.Collapsed;
        }

        private void BtnBack_Click(object sender, RoutedEventArgs e)
        {
            ManagerFrame.MainFrame.GoBack();
        }

        private void BtnEndWorkShift_Click(object sender, RoutedEventArgs e)
        {
            EndWorkShift(Helper.IDStore.ToString(), $"End work shift ={Helper.IDCashier}");
            ManagerFrame.MainFrame.GoBack();
        }

        public async void EndWorkShift(string IDStore, string Message)
        {
            try
            {
                if (IDStore.Length == 1) IDStore = IDStore.Insert(0, "0");
                IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), Convert.ToInt32("102" + IDStore));
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Connect(iPEndPoint);
                byte[] data = Encoding.UTF8.GetBytes(Message);
                socket.Send(data);
                Helper.WorkShiftID = null;
                BtnEndWorkShift.IsEnabled = false;
                BtnStartWorkShift.IsEnabled = true;
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }
            catch (Exception)
            {
                Helper.ErrorMessage("Нет ответа от сервера");
            }

        }

        public async void StartWorkShift(string IDStore, string Message)
        {
            try
            {
                if (IDStore.Length == 1) IDStore = IDStore.Insert(0, "0");
                IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), Convert.ToInt32("102" + IDStore));
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Connect(iPEndPoint);
                byte[] data = Encoding.UTF8.GetBytes(Message);
                socket.Send(data);
                data = new byte[256];
                int byts = 0;
                StringBuilder builder = new StringBuilder();
                do
                {
                    byts = socket.Receive(data, data.Length, 0);
                    builder.Append(Encoding.UTF8.GetString(data, 0, byts));
                } while (socket.Available > 0);
                string response = builder.ToString();
                Helper.WorkShiftID = Convert.ToInt32(response);
                BtnEndWorkShift.IsEnabled = true;
                BtnStartWorkShift.IsEnabled = false;
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }
            catch (Exception)
            {
                Helper.ErrorMessage("Нет ответа от сервера");
            }

        }


        private void BtnStartWorkShift_Click(object sender, RoutedEventArgs e)
        {
            StartWorkShift(Helper.IDStore.ToString(), $"Start work shift ={Helper.IDCashier}");
        }

        private void BtnBlockCashier_Click(object sender, RoutedEventArgs e)
        {
            if (IsEnabledCashier == false)
            {
                BtnBlockCashier.Content = "Разблокировать кассу";
                MainFrame.IsEnabled = false;
                BtnStartWorkShift.IsEnabled = false;
                BtnEndWorkShift.IsEnabled = false;
                IsEnabledCashier = true;
                return;
            }
            EnabledCashierWindow enabledCashierWindow = new EnabledCashierWindow();
            if (enabledCashierWindow.ShowDialog()== true)
            {
                if (Helper.WorkShiftID == null)
                {
                    BtnStartWorkShift.IsEnabled = true;
                }
                BtnBlockCashier.Content = "Заблокировать кассу";
                MainFrame.IsEnabled = true;
                BtnEndWorkShift.IsEnabled = true;
                IsEnabledCashier = false;
            }
          
        }
    }
}
