using Admin.Core.Model;
using Aucma.Scada.UI.Common;
using Aucma.Scada.UI;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Windows;

namespace Aucma.Scada.UI.ViewModel.AssemblyPlan
{
    public partial class QuantityIssuedViewModel : ObservableObject
    {
        private AssemblyPlanBusiness assemblyPlanBusiness = new AssemblyPlanBusiness();// AssemblyPlanBusiness.Instance;
        private AppConfig appConfig = new  AppConfig();//AppConfig.Instance;
        public QuantityIssuedViewModel(ProductPlanInfo productPlanInfo)
        {
            PlanInfo = productPlanInfo;
        }

        private ProductPlanInfo _PlanInfo = new ProductPlanInfo();
        public ProductPlanInfo PlanInfo
        {
            get => _PlanInfo;
            set => SetProperty(ref _PlanInfo, value);
        }

        private string _TransmitAmount = string.Empty;
        public string TransmitAmount
        {
            get => _TransmitAmount;
            set => SetProperty(ref _TransmitAmount, value);
        }

        [RelayCommand]
        private async void PlanInfoTransmit()
        {

            var productPlanInfo = _PlanInfo;

            if(productPlanInfo != null)
            {
                var materialType = productPlanInfo.MaterialCode;
                
                bool shellResult =await assemblyPlanBusiness.JudgmentStock(productPlanInfo, Convert.ToInt32(_TransmitAmount), appConfig.shellMaterialType,appConfig.shellStoreCode);
                if (!shellResult)
                {
                    MessageBox.Show("计划下达失败,箱壳物料库存不足", "提示", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly);
                    return;
                }
                bool linerResult =await assemblyPlanBusiness.JudgmentStock(productPlanInfo, Convert.ToInt32(_TransmitAmount), appConfig.linerMaterialType, appConfig.linerStoreCode);
                if (!linerResult)
                {
                    MessageBox.Show("计划下达失败,内胆物料库存不足", "提示", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly);
                    return;
                }

                //if (!shellResult && !linerResult)
                //{
                //    MessageBox.Show("计划下达失败,箱壳、内胆物料库存不足");
                //    return;
                //}else if(!shellResult || !linerResult)
                //{
                //    if (!shellResult)
                //    {
                //        MessageBox.Show("计划下达失败,箱壳物料库存不足");
                //        return;
                //    }
                //    else if (!linerResult)
                //    {
                //        MessageBox.Show("计划下达失败,内胆物料库存不足");
                //        return;
                //    }
                //}
            }
            else
            {
                MessageBox.Show("生产计划获取失败,加载为空", "提示", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly);
                return;
            }

            bool result =await assemblyPlanBusiness.PlanTransmitByProductPlan(_PlanInfo.PlanCode, Convert.ToInt32(_TransmitAmount));
            if (result)
            {
                MessageBox.Show("执行计划维护成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly);
            }
        }
        [RelayCommand]
        private void ClearTransmitAmount()
        {
            string amount = _TransmitAmount.ToString();
            if(amount.Length > 0)
            {
                TransmitAmount = amount.Substring(0, amount.Length - 1);
            }
        }
        [RelayCommand]
        private void KeypadButton(object obj)
        {
            var info = obj as string;

            TransmitAmount += info;
        }
        [RelayCommand]
        private void CloseWindow(object parameter)
        {
            var window = parameter as Window;
            if (window != null)
            {
                window.Close();
            }

        }

    }
}