using MeterVision.Patch;
using System;
using System.Windows;
//using MeterVision.Controls; // 引入 UserControl 所在的命名空间

namespace MeterVision.Dlg
{
    public partial class PatchPrgDialog : Window
    {
        //public event EventHandler<StopProcessEventArgs> OnStopProcess;
        private UCPatchGrid imageProcessingControl; // 引用主窗体中的 UserControl
        private bool isProcessing = false;

        // 构造函数，接收 UserControl 以便启动/停止处理
        public PatchPrgDialog(UCPatchGrid control)
        {
            InitializeComponent();
            imageProcessingControl = control;

            // 订阅 UserControl 中的事件
            //imageProcessingControl.OnProcessingProgressChanged += ImageProcessingControl_OnProcessingProgressChanged;
            //imageProcessingControl.OnProcessingCompleted += ImageProcessingControl_OnProcessingCompleted;
            //imageProcessingControl.OnProcessingStopped += ImageProcessingControl_OnProcessingStopped;
        }

        // 显示对话框并启动处理
        public void StartProcessing()
        {
            progressText.Text = "开始处理...";
            progressBar.Value = 0;
            progressBar.Maximum = 100;
            stopButton.IsEnabled = true;
            isProcessing = true;

            Owner = Application.Current.MainWindow;
            WindowStartupLocation = WindowStartupLocation.CenterOwner;
            //this.ShowDialog();  // 显示对话框
            this.Show();
            //imageProcessingControl.StartProcessing(); // 启动图片处理
        }

        // 更新进度条和进度文本
        private void ImageProcessingControl_OnProcessingProgressChanged(int currentImage, int totalImages)
        {
            double progress = (double)currentImage / totalImages * 100;
            progressBar.Value = progress;
            progressText.Text = $"正在处理：{currentImage}/{totalImages}";
        }

        // 处理完成后的事件
        private void ImageProcessingControl_OnProcessingCompleted()
        {
            progressText.Text = "处理完成！";
            stopButton.IsEnabled = false; // 禁用停止按钮
            isProcessing = false;
            this.Close();  // 关闭对话框
        }

        // 处理停止后的事件
        private void ImageProcessingControl_OnProcessingStopped()
        {
            progressText.Text = "处理已停止。";
            stopButton.IsEnabled = false; // 禁用停止按钮
            isProcessing = false;
            this.Close();  // 关闭对话框
        }

        // 点击停止按钮时，停止处理
        private void StopButton_Click(object sender, RoutedEventArgs e)
        {
            if (isProcessing)
            {
                //imageProcessingControl.StopProcessing(); // 停止处理
            }
        }

        //public void UpdateProgress(int currentNum,int totalNum)
        //{
        //    Dispatcher.Invoke(() =>
        //    {
        //                    double progress = (double)currentNum / totalNum * 100;
        //    progressBar.Value = progress;
        //    progressText.Text = $"正在处理：{currentNum}/{totalNum}";
        //    });

        //}

        // 处理完成后的事件

        // 点击停止按钮时，停止处理
        //private void StopButton_Click(object sender, RoutedEventArgs e)
        //{
        //    stopButton.IsEnabled = false;   //防止连续点击
        //    OnStopProcess?.Invoke(this, new StopProcessEventArgs());
        //}
    }

    //按下停止按钮的事件
    public class StopProcessEventArgs : EventArgs
    {
        public StopProcessEventArgs()
        {

        }
    }
    //----------------------------------------------------------------------
}
