using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Win32;
using MiniExcelLibs;
using NVelocity.Util.Introspection;
using SlnMesnac.Business;
using SlnMesnac.Config;
using SlnMesnac.Model.domain;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2024 WenJY 保留所有权利。
* CLR版本：4.0.30319.42000
* 机器名称：T14-GEN3-7895
* 命名空间：SlnMesnac.WPF.ViewModel
* 唯一标识：363901e3-4a2d-433c-862a-6ea822f784fb
*
* 创建者：WenJY
* 电子邮箱：
* 创建时间：2024-09-20 10:23:24
* 版本：V1.0.0
* 描述：
*
*--------------------------------------------------------------------
* 修改人：
* 时间：
* 修改说明：
*
* 版本：V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.WPF.ViewModel
{
    public partial class HistoryViewModel : ObservableObject
    {
        private TagScanBusiness tagScanBusiness;
        private AppConfig _appConfig;
        public HistoryViewModel()
        {
            tagScanBusiness = App.ServiceProvider.GetService<TagScanBusiness>();
            _appConfig = App.ServiceProvider.GetService<AppConfig>();
        }

        public string _rfidCode;
        public string RfidCode
        {
            get => _rfidCode;
            set => SetProperty(ref _rfidCode, value);
        }

        public DateTime _beginTime = DateTime.Now;
        public DateTime BeginTime
        {
            get => _beginTime;
            set => SetProperty(ref _beginTime, value);
        }

        public DateTime _endTime = DateTime.Now.AddDays(1);
        public DateTime EndTime
        {
            get => _endTime;
            set => SetProperty(ref _endTime, value);
        }

        private ObservableCollection<ScanLogModel> _scanItems = new ObservableCollection<ScanLogModel>();
        public ObservableCollection<ScanLogModel> ScanItems
        {
            get => _scanItems;
            set => SetProperty(ref _scanItems, value);
        }

        [RelayCommand]
        private void Query()
        {
            tagScanBusiness.QuerySacnInfo(_rfidCode, _beginTime, _endTime,out List<ScanLogModel> info);

            ScanItems = new ObservableCollection<ScanLogModel>(info);
        }

        [RelayCommand]
        private void Export()
        {
            try
            {
                var info = _scanItems;

                // 创建 SaveFileDialog 对象
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "Excel文件 (*.xlsx)|*.xlsx|所有文件 (*.*)|*.*";
                saveFileDialog.Title = "保存文件";
                saveFileDialog.FileName = $"扫描记录{DateTime.Now.Date:yyyy-MM-dd}.xlsx";

                if (saveFileDialog.ShowDialog() == true)
                {
                    string filePath = saveFileDialog.FileName;
                    MiniExcel.SaveAs(filePath, info);
                    MessageBox.Show($"扫描记录导出成功:{filePath}");

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"扫描记录导出异常:{ex.Message}");
            }
        }

        [RelayCommand]
        private void OpenImageFile(ScanLogModel scanLog)
        {
            if (!string.IsNullOrEmpty(scanLog.Url))
            {
                Task.Run(() =>
                {
                    lock (string.Empty)
                    {
                        ShowImageInPopup(scanLog.Url);
                    }
                });
            }
            else
            {
                MessageBox.Show("无法打开全景图像，图像路径为空");
            }
        }


        private void ShowImageInPopup(string ImagePath)
        {
            App.Current.Dispatcher.Invoke(() =>
            {
                var window = new Window
                {
                    Title = "全景图像在线预览",
                    Content = new System.Windows.Controls.Image
                    {
                        Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(ImagePath))
                    },
                    Width = 800,
                    Height = 650,
                    WindowStartupLocation = WindowStartupLocation.CenterOwner
                };
                window.ShowDialog();
            });
            
        }
    }
}
