using BBWYB.Client.APIServices;
using BBWYB.Client.Models;
using BBWYB.Client.Views.Order;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;

namespace BBWYB.Client.ViewModels
{
    public class EditPriceViewModel : BaseVM
    {
        private OrderService orderService;

        public IList<OrderSkuEditPrice> OrderSkuList { get; set; }
        private Order order;
        private decimal freightAmount;
        private bool isResponseFreightChanged;

        public ICommand DistributFreightCommand { get; set; }
        public ICommand SaveCommand { get; set; }

        public decimal FreightAmount { get => freightAmount; set { SetProperty(ref freightAmount, value); } }

        public EditPriceViewModel(OrderService orderService)
        {
            OrderSkuList = new ObservableCollection<OrderSkuEditPrice>();
            DistributFreightCommand = new RelayCommand(DistributFreight);
            SaveCommand = new RelayCommand(Save);
            this.orderService = orderService;
        }

        public void SetData(Order order)
        {
            this.order = order;
            foreach (var orderSku in order.ItemList)
            {
                OrderSkuList.Add(new OrderSkuEditPrice()
                {
                    Id = orderSku.Id,
                    Logo = orderSku.Logo,
                    Title = orderSku.Title,
                    SkuId = orderSku.SkuId,
                    Price = orderSku.Price,
                    NewPrice = orderSku.Price ?? 0,
                    ItemTotal = orderSku.ItemTotal,
                    OnFreightChanged = OnFreightChanged
                });
            }
        }

        private void DistributFreight()
        {
            if (FreightAmount <= 0)
            {
                MessageBox.Show("运费不正确", "提示");
                return;
            }
            isResponseFreightChanged = false;
            var totalItemCount = OrderSkuList.Sum(s => s.ItemTotal);
            foreach (var orderSku in OrderSkuList)
            {
                var quantityRatio = 1M * orderSku.ItemTotal / totalItemCount;
                var currentFreightAmount = FreightAmount * quantityRatio;
                orderSku.FreightAmount = currentFreightAmount;
                orderSku.IsResponseFreightChanged = true;
            }
            isResponseFreightChanged = true;
        }

        private void OnFreightChanged()
        {
            if (!isResponseFreightChanged)
                return;

            isResponseFreightChanged = false;
            var waitDistributFreight = FreightAmount - OrderSkuList.Where(osku => !osku.IsResponseFreightChanged).Sum(osku => osku.FreightAmount);
            var waitDistributSkuList = OrderSkuList.Where(osku => osku.IsResponseFreightChanged);
            var totalItemCount = waitDistributSkuList.Sum(osku => osku.ItemTotal);
            foreach (var orderSku in waitDistributSkuList)
            {
                var quantityRatio = 1M * orderSku.ItemTotal / totalItemCount;
                var currentFreightAmount = waitDistributFreight * quantityRatio;
                orderSku.FreightAmount = currentFreightAmount;
                orderSku.IsResponseFreightChanged = true;
            }
            isResponseFreightChanged = true;
        }

        private void Save()
        {
            if (FreightAmount <= 0)
            {
                MessageBox.Show("运费不正确", "提示");
                return;
            }
            if (OrderSkuList.Any(osku => osku.FreightAmount <= 0 || osku.NewPrice <= 0))
            {
                MessageBox.Show("运费或单价不正确", "提示");
                return;
            }
            if (Math.Abs(FreightAmount - OrderSkuList.Sum(osku => osku.FreightAmount)) > 1)
            {
                MessageBox.Show("运费超出误差范围", "提示");
                return;
            }

            var response = orderService.EditPrice(this.order.Id, OrderSkuList);
            if (!response.Success)
            {
                MessageBox.Show(response.Msg, "提示");
                return;
            }
            WeakReferenceMessenger.Default.Send(new Message_EditPrice_Close(null));

        }
    }
}