using MahApps.Metro.Controls.Dialogs;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
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;
using WSDDBBrowser.Chart;
using WSDDBBrowser.Core;
namespace WSDDBBrowser
{
    /// <summary>
    /// PgMonitorChartView.xaml 的交互逻辑
    /// </summary>
    public partial class PgMonitorChartView : UserControl
    {
        PgMonitorChartViewModel viewModel;
        public PgMonitorChartView()
        {
            InitializeComponent();
        }
        public void Init()
        {
            viewModel = new PgMonitorChartViewModel();
            viewModel.Init();
            this.DataContext = viewModel;
        }

        private void ButtonChartSelect_Click(object sender, RoutedEventArgs e)
        {
            RecordCollection rc = ((Button)sender).Tag as RecordCollection;
            viewModel.ChartSelectChange(rc);
        }

        private async void ButtonJpgClick(object sender, RoutedEventArgs e)
        {
            string strDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            string filename = "纵向趋势图_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "jpg files (*.jpg)|*.jpg";
            sfd.InitialDirectory = strDesktopPath;
            sfd.FileName = filename;
            if (sfd.ShowDialog() == true)
            {
                ChartToImage.SaveToJpeg(charts, sfd.FileName);

                await App.MetroWindow.ShowMessageAsync("输出成功",
                    sfd.FileName);
            }
        }
    }

    public class PgMonitorChartViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public List<RecordCollection> RecordCollections { get; set; }

        /// <summary>
        /// 单选模式
        /// </summary>
        public bool IsSingleSelectMode { get; set; } = true;

        public void Init()
        {
            RecordCollections = DBViewerModel.Instance.RecordCollections;
        }
        public void ChartSelectChange(RecordCollection rc)
        {
            if (IsSingleSelectMode)
            {
                rc.IsVisibile = true;
                //把其它隐藏
                foreach (var recordCollection in RecordCollections)
                {
                    if (recordCollection != rc)
                    {
                        recordCollection.IsVisibile = false;
                    }
                }
            }
            else
            {
                //多选模式
                rc.IsVisibile = !rc.IsVisibile;
            }
        }
    }
}
