using MeterVision.Dlg;
using MeterVision.model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
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.Navigation;
using System.Windows.Shapes;

namespace MeterVision.Station
{
    /// <summary>
    /// UCStationMain.xaml 的交互逻辑
    /// </summary>
    public partial class UCStationMain : UserControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private string _findStationId;
        public string FindStationId
        {
            get => _findStationId;
            set
            {
                if(_findStationId != value)
                {
                    _findStationId = value;
                    OnPropertyChanged(nameof(FindStationId));
                    //ucStationGrid.ChangeFindStationId(FindStationId);
                }
            }
        }

        public UCStationMain()
        {
            InitializeComponent();

            txtFindStationId.Text = "";
            FindStationId = "";            

            this.DataContext = this;
        }
        

        private void BtnQuery_Click(object sender, RoutedEventArgs e)
        {
            FindStationId = txtFindStationId.Text.Trim();
        }

        private void BtnClearAll_Click(object sender, RoutedEventArgs e)
        {
            ucStationGrid.ClearAllStation();
        }

        private void BtnAddStation_Click(object sender, RoutedEventArgs e)
        {
            ////StationItem newItem = new StationItem();
            //EditStationDlg2 dialog = new EditStationDlg2(null)
            //{
            //    Owner = Application.Current.MainWindow,
            //    WindowStartupLocation = WindowStartupLocation.CenterOwner
            //};
            //if(dialog.ShowDialog() == true)
            //{
            //    //ucStationGrid.ChangeFindStationId(null);
            //    //ucStationGrid.ChangeFindStationId(FindStationId);
            //}
        }

        private void BtnRefresh_Click(object sender, RoutedEventArgs e)
        {
            //ucStationGrid.ChangeFindStationId(null);
            //ucStationGrid.ChangeFindStationId(FindStationId);
        }

        private async void BtnStationFirstPage_Click(object sender, RoutedEventArgs e)
        {
            await ucStationGrid.FirstPage();
        }

        private async void BtnStationPrePage_Click(object sender, RoutedEventArgs e)
        {
            await ucStationGrid.PrePage();
        }

        private async void BtnStationNextPage_Click(object sender, RoutedEventArgs e)
        {
            await ucStationGrid.NextPage();
        }

        private async void BtnStationLastPage_Click(object sender, RoutedEventArgs e)
        {
            await ucStationGrid.LastPage();
        }

        private async void BtnStationSpeciPage_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int pageNumber = int.Parse(txtStationPageNumber.Text.ToString());
                await ucStationGrid.SpeciPage(pageNumber);
            }
            catch
            {

            }
        }

        private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            // 只允许数字输入
            e.Handled = !Regex.IsMatch(e.Text, @"^\d$");
        }

        private bool IsDigit(string text)
        {
            // 返回输入的字符是否为数字
            return text.All(char.IsDigit);
        }

        private void TxtFindStationId_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            // 使用正则表达式检查输入的字符是否是数字
            e.Handled = !IsDigit(e.Text);
        }

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