using Admin.Core.IService;
using Admin.Core.Model.Model_New;
using Admin.Core.Service;
using Aucma.Core.OldBoxFoam;
using Aucma.Core.OldBoxFoam.Models;
using Aucma.Core.OldBoxFoam.Views;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;

namespace Aucma.Core.OldBoxFoam.ViewModels
{

   
    public partial class RoadKindPageViewModel : ObservableObject
    {
        protected readonly IBoxFoamPlanServices? _boxFoamPlanServices;
        protected IOldBoxFoamTypeServices? _boxFoamTypeServices;
        public RoadKindPageViewModel()
        {
            // 设置型号
            SubmitCommand = new RelayCommand<string>(obj => SubmitCommandExecute(obj));
            _boxFoamPlanServices = App.ServiceProvider.GetService<IBoxFoamPlanServices>();
            _boxFoamTypeServices = App.ServiceProvider.GetService<IOldBoxFoamTypeServices>();
            SelectTypeViewModel.RefreshPageEvent += InitData;
            InitData();
        }

        /// <summary>
        /// 设置型号
        /// </summary>
        public RelayCommand<string> SubmitCommand { get; set; }

        private void SubmitCommandExecute(string spaceCode)
        {

            SelectType type = new SelectType(spaceCode,"");
            type.ShowDialog();

        }

        public async void InitData()
        {
            //Shapes.Clear();
            List<OldBoxFoamType> list =  await  _boxFoamTypeServices.QueryAsync();
            if (list == null) return;
            if(list.Count > 0)
            {
                list = list.OrderBy(x => x.ObjId).ToList();
                int totalItems = list.Count;
                int half = totalItems / 2;

                List<OldBoxFoamType> spaceInfoLeft = list.Take(half).ToList();
                List<OldBoxFoamType> spaceInfoRight = list.Skip(half).ToList();

                System.Windows.Application.Current.Dispatcher.Invoke((Action)(() =>
                {
                    Shapes.Clear();
                    if (spaceInfoLeft == null) return;
                    for (int i = 0; i < spaceInfoLeft.Count; i++)
                    {
                        OldBoxFoamTypeModel model = new OldBoxFoamTypeModel();
                        model.MaterialCode = spaceInfoLeft[i].MaterialCode;

                        string pattern = @",(.*)";
                        Regex regex = new Regex(pattern);
                        Match match = regex.Match(spaceInfoLeft[i].MaterialName);

                        if (match.Success)
                        {
                            string result = match.Groups[1].Value;
                            model.MaterialName = result;
                        }
                        else
                        {
                            model.MaterialName = spaceInfoLeft[i].MaterialName;
                        }
                        model.ObjId = spaceInfoLeft[i].ObjId;
                        model.Status = "1";
                        model.Local = $"东线{i + 1}";
                        Shapes.Add(model);
                    }
                    if (spaceInfoRight == null) return;
                    for (int i = 0; i < spaceInfoRight.Count; i++)
                    {
                        OldBoxFoamTypeModel model = new OldBoxFoamTypeModel();
                        model.MaterialCode = spaceInfoRight[i].MaterialCode;

                        string pattern = @",(.*)";
                        Regex regex = new Regex(pattern);
                        Match match = regex.Match(spaceInfoRight[i].MaterialName);

                        if (match.Success)
                        {
                            string result = match.Groups[1].Value;
                            model.MaterialName = result;
                        }
                        else
                        {
                            model.MaterialName = spaceInfoRight[i].MaterialName;
                        }
                        model.ObjId = spaceInfoRight[i].ObjId;
                        model.Status = "1";
                        model.Local = $"西线{i + 1}";
                        Shapes.Add(model);
                    }
                }));
            }
            //foreach(OldBoxFoamType item in list)
            //{
            //    OldBoxFoamTypeModel model= new OldBoxFoamTypeModel();
            //    model.MaterialCode = item.MaterialCode;
                
            //    string pattern = @",(.*)";
            //    Regex regex = new Regex(pattern);
            //    Match match = regex.Match(item.MaterialName);

            //    if (match.Success)
            //    {
            //        string result = match.Groups[1].Value;
            //        model.MaterialName = result;
            //    }
            //    else
            //    {
            //        model.MaterialName = item.MaterialName;
            //    }
            //    model.ObjId = item.ObjId;
            //    model.Status = "1";
            //    Shapes.Add(model);
            //}
        }

        #region 初始化
        private ObservableCollection<OldBoxFoamTypeModel> _shapes = new ObservableCollection<OldBoxFoamTypeModel>();
        public ObservableCollection<OldBoxFoamTypeModel> Shapes
        {
            get => _shapes;
            set => SetProperty(ref _shapes, value);
        }
        #endregion



    }
}