using ProximaAnalizer.Data;
using System.Collections.ObjectModel;
using System.Windows;

namespace ProximaAnalizer.ViewModels;

internal partial class RecipeStepNaviViewModel(IEventAggregator eventAggregator, SqlSugarCustom sqlSugar, TraceData traceData) : ObservableObject
{
    [ObservableProperty]
    private DateTime _SelectedTime = DateTime.Now;

    [ObservableProperty]
    private Visibility _Masking = Visibility.Hidden;

    [ObservableProperty]
    private ObservableCollection<ProcessData> _ProcessData = [];

    partial void OnSelectedTimeChanged(DateTime value)
    {
        this.Search();
    }

    [RelayCommand]
    private void Navi(ProcessData process)
    {
        this.Masking = Visibility.Visible;
        traceData.ProcessData = process;

        Task.Factory.StartNew(() =>
        {
            try
            {
                App.Current.Dispatcher.Invoke(() =>
                {
                    eventAggregator.GetEvent<WindowSwitch>().Page = Pages.MainWindow;
                    eventAggregator.GetEvent<WindowSwitch>().Publish();
                    eventAggregator.GetEvent<RefreshRecipeData>().Publish();
                });
            }
            finally
            {
                this.Masking = Visibility.Collapsed;
            }
        });
    }

    [RelayCommand]
    private void History(ProcessData process)
    {
        this.Masking = Visibility.Visible;
        traceData.ProcessData = process;

        Task.Factory.StartNew(() =>
        {
            try
            {
                App.Current.Dispatcher.Invoke(() =>
                {
                    eventAggregator.GetEvent<WindowSwitch>().Page = Pages.HistoryData;
                    eventAggregator.GetEvent<WindowSwitch>().Publish();
                    RefreshAlarmData alarmData = eventAggregator.GetEvent<RefreshAlarmData>();
                    alarmData.Selected = null;
                    alarmData.FromPage = Pages.RecipeStepNavi;
                    alarmData.Publish();
                });
            }
            finally
            {
                this.Masking = Visibility.Collapsed;
            }
        });
    }

    [RelayCommand]
    private void DayOperator(string para)
    {
        this.SelectedTime = para switch
        {
            "+" => this.SelectedTime.AddDays(1),
            "-" => this.SelectedTime.AddDays(-1),
            _ => this.SelectedTime
        };
    }

    [RelayCommand]
    private void Search()
    {
        if (sqlSugar.Client is null)
            return;

        this.ProcessData ??= [];
        this.ProcessData.Clear();
        this.Masking = Visibility.Visible;

        Task.Factory.StartNew(() =>
        {
            try
            {
                var t = sqlSugar.Client.Queryable<ProcessData>().Where(t =>
                t.Process_Begin_Time >= SelectedTime &&
                t.Process_Begin_Time < SelectedTime.AddDays(1)).AS("process_data").ToArray();
                foreach (var item in t)
                {
                    App.Current.Dispatcher.Invoke(() =>
                    {
                        this.ProcessData.Add(item);
                    });
                    Thread.Sleep(1);
                }

            }
            finally
            {
                this.Masking = Visibility.Collapsed;
            }
        });
    }

    //[RelayCommand]
    //private void NameSearch(string name)
    //{
    //    if (sqlSugar.Client is null)
    //        return;
    //    if (string.IsNullOrEmpty(name))
    //        return;

    //    this.ProcessData ??= [];
    //    this.ProcessData.Clear();
    //    this.Masking = Visibility.Visible;
    //    Task.Factory.StartNew(() =>
    //    {
    //        try
    //        {
    //            var t = sqlSugar.Client.Queryable<ProcessData>()
    //                .Where(t => (string.IsNullOrEmpty(t.Recipe_Name) ? string.Empty : t.Recipe_Name).ToLower().Contains(name.ToLower()))
    //                .AS("process_data").ToArray()
    //                .OrderBy(t => t.Process_Begin_Time);

    //            foreach (var item in t)
    //            {
    //                App.Current.Dispatcher.Invoke(() =>
    //                {
    //                    this.ProcessData.Add(item);
    //                });
    //                Thread.Sleep(1);
    //            }

    //        }
    //        finally
    //        {
    //            this.Masking = Visibility.Collapsed;
    //        }
    //    });

    //}
}

