using System;
using System.Collections.Generic;
using System.Linq;
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.Shapes;
using Database.Models;
using Database.Repository;
using Pos.Service;

namespace Pos
{
    /// <summary>
    /// Interaction logic for PrintReceiptWindow.xaml
    /// </summary>
    public partial class PrintReceiptWindow : Window
    {
        private List<ListReceiptModel> _receiptList = new List<ListReceiptModel>();
        private bool _init = false;
        private int _saleId = 0;

        public PrintReceiptWindow()
        {
            InitializeComponent();

            StartDatePicker.DisplayDate = DateTime.Now.AddDays(-7);
            StartDatePicker.SelectedDate = DateTime.Now.AddDays(-7);
            EndDatePicker.DisplayDate = DateTime.Now;
            EndDatePicker.SelectedDate = DateTime.Now;
        }

        private void Search_OnClick(object sender, RoutedEventArgs e)
        {
            DateTime startDate = (DateTime)StartDatePicker.SelectedDate;
            DateTime endDate = (DateTime)EndDatePicker.SelectedDate;
            SaleRepository saleRepository = new SaleRepository();
            List<SaleEntity> saleEntities = saleRepository.GetByDateRange(startDate, endDate);
            foreach (SaleEntity entity in saleEntities)
            {
                List<SaleLineEntity> saleLineBySaleId = saleRepository.GetSaleLineBySaleId(entity.Id);
                decimal total = 0;
                foreach (SaleLineEntity lineEntity in saleLineBySaleId)
                {
                    total += lineEntity.Total;
                }

                ListReceiptModel receiptModel = new ListReceiptModel
                {
                    Id = entity.Id,
                    Content = $"{entity.Time:dd-MM-yyyy HH:mm} - Pris: {total:0.00}"
                };
                _receiptList.Add(receiptModel);
            }

            _init = true;
            ListReciept.ItemsSource = _receiptList;
            _init = false;
        }

        
        public class ListReceiptModel
        {
            public int Id { get; set; }
            public string Content { get; set; }

            public override string ToString()
            {
                return Content;
            }
        }

        private void ListReciept_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_init)
                return;

            ListReceiptModel data = (ListReceiptModel) e.AddedItems[0];
            _saleId = data.Id;
            SaleRepository saleRepository = new SaleRepository();
            List<SaleLineEntity> saleLineBySaleId = saleRepository.GetSaleLineBySaleId(data.Id);
            StringBuilder sb = new StringBuilder();
            decimal total = 0;
            foreach (SaleLineEntity saleLine in saleLineBySaleId)
            {
                total += saleLine.Total;
                sb.AppendLine(
                    $"{saleLine.Product} - {saleLine.Pieces} stk. a' {saleLine.Price.ToString("0.00")} = {saleLine.Total.ToString("0.00")}");
            }

            sb.AppendLine($"Total: {total.ToString("0.00")} kr.");
            PrintReceipt.Visibility = Visibility.Visible;
            SaleLines.Clear();
            SaleLines.Text = sb.ToString();
        }

        private void PrintReceipt_OnClick(object sender, RoutedEventArgs e)
        {
            SaleService saleService = new SaleService();
            saleService.PrintReceipt(_saleId);
        }
    }
}
