using MeterVision.Config;
using MeterVision.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MeterVision.Dlg
{
    /// <summary>
    /// EditStandValueDlg.xaml 的交互逻辑
    /// </summary>
    public partial class EditStandValueDlg : Window
    {
        public StandValueModel mStandValueModel { get; private set; }

        private string CurImagePath = string.Empty;

        public event Action<StandValueModel> OnEditStandValue; 

        public EditStandValueDlg(StandValueModel standValueModel)
        {
            InitializeComponent();

            int width = CfginiItem.GetConfigItem().DialogStandSize_Width;
            int height = CfginiItem.GetConfigItem().DialogStandSize_Height;
            if (width != 0 && height != 0)
            {
                this.Width = width;
                this.Height = height;
            }

            mStandValueModel = standValueModel;
            InitUIValue(standValueModel);
            CurImagePath = "";
            if (standValueModel.ModelType == 2)
            {
                CurImagePath = mStandValueModel.SrcImage;   //让界面线显示结果图片
            }            
            LoadImage();
        }

        private void InitUIValue(StandValueModel standValueModel)
        {
            //ModelType = 1 模板标准值修改, = 2任务标准值修改；
            pnlLeft.Visibility = pnlRight.Visibility = pnlFinalValue.Visibility = 
                (standValueModel.ModelType == 1 ? Visibility.Collapsed : Visibility.Visible);
            txtFinalValue.Text = standValueModel.FinalValue;
            txtStandValue.Text = standValueModel.StandValue;            
        }

        private void LoadImage()
        {
            if (CurImagePath.Equals(mStandValueModel.SrcImage))
            {
                ucImage.ImageSource = mStandValueModel.DstImage;
                ucImage.ImageName = "结果图片";
                CurImagePath = mStandValueModel.DstImage;
            }
            else
            {
                ucImage.ImageSource = mStandValueModel.SrcImage;
                ucImage.ImageName = "原始图片";
                CurImagePath = mStandValueModel.SrcImage;
            }
        }

        private void BtnRight_Click(object sender, RoutedEventArgs e)
        {
            LoadImage();
        }

        private void BtnLeft_Click(object sender, RoutedEventArgs e)
        {
            LoadImage();
        }

        private void BtnCopy_Click(object sender, RoutedEventArgs e)
        {
            txtStandValue.Text = txtFinalValue.Text;

        }

        private void BtnOk_Click(object sender, RoutedEventArgs e)
        {
            string standValue = txtStandValue.Text.Trim();

            if (string.IsNullOrWhiteSpace(standValue))
            {
                MessageBox.Show("请输入标准值。", "提示", 
                    MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            if (!IsPositiveNumber(standValue))
            {
                MessageBox.Show("请输入正确的值。", "提示",
                    MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            mStandValueModel.StandValue = standValue;
            //DialogResult = true;
            this.Close();
            OnEditStandValue?.Invoke(mStandValueModel);
        }

        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            //DialogResult = false;
            this.Close();
        }

        private void TxtStandValue_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            //string input = e.Text;

            //// 检查是否输入的是有效的数字或小数点
            //// 1. 只允许数字、英文小数点
            //// 2. 禁止输入任何中文字符
            //if (Regex.IsMatch(input, @"[^0-9.]") || (input == "." && txtStandValue.Text.Contains(".")))
            //{
            //    e.Handled = true; // 阻止非法字符
            //}
        }

        private void TxtStandValue_TextChanged(object sender, TextChangedEventArgs e)
        {
            //string text = txtStandValue.Text;

            //// 如果输入为空或者不是合法的正数或小数
            //if (!string.IsNullOrEmpty(text) && !IsValidNumber(text))
            //{
            //    // 可以根据需求重置文本或者给出提示
            //    txtStandValue.Text = "0";  // 默认值为 0
            //    txtStandValue.SelectionStart = txtStandValue.Text.Length;  // 保证光标位置在文本末尾
            //}
        }


        private bool IsValidNumber(string text)
        {
            // 正则表达式：仅允许正整数或一个小数点的数字
            Regex regex = new Regex(@"^\d+(\.\d+)?$");
            return regex.IsMatch(text);
        }

        public bool IsPositiveNumber(string input)
        {
            // 使用 double.TryParse 判断是否为数字
            if (double.TryParse(input, out double result))
            {
                // 判断是否为正数
                return result >= 0;
            }
            return false;
        }

        private void Window_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                //if (e.Delta > 0)
                //{
                //    ZoomFactor *= 1.1; // Zoom in
                //}
                //else
                //{
                //    ZoomFactor /= 1.1; // Zoom out
                //}

                //// Optionally, center the zoom around the mouse cursor
                //Point mousePosition = e.GetPosition(imgViewer);
                //double prevZoomFactor = 1 / ZoomFactor;
                //double offsetX = (mousePosition.X * ZoomFactor - mousePosition.X * prevZoomFactor) / prevZoomFactor;
                //double offsetY = (mousePosition.Y * ZoomFactor - mousePosition.Y * prevZoomFactor) / prevZoomFactor;

                //scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset + offsetX);
                //scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + offsetY);

                ucImage.ImageZoom(e.Delta > 0,e);
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            if (WindowState != WindowState.Maximized)
            {
                int width = (int)this.Width;
                int height = (int)this.Height;
                string dialogSize = $"{width},{height}";
                CfginiItem.GetConfigItem().DialogStandSize = dialogSize;
            }
        }
        //----------------------------------------------------------------
    }
}
