using GalaSoft.MvvmLight.Command;
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.Shapes;

namespace FLY.Thick.FilmCasting.UI.Server
{
    /// <summary>
    /// WdPicHistory.xaml 的交互逻辑
    /// </summary>
    public partial class WdPicHistory : Window
    {
        WdPicHistoryVm viewModel;
        PicHistory picHistory;
        public WdPicHistory()
        {
            InitializeComponent();
        }
        public void Init(PicHistory picHistory) 
        {
            this.picHistory = picHistory;
            viewModel = new WdPicHistoryVm();
            this.viewModel.Init(picHistory);
            this.DataContext = viewModel;

            scanGraph.Init(picHistory.scanGraphVm);
        }

        private void btnSavePicClick(object sender, RoutedEventArgs e)
        {
            string strDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.InitialDirectory = strDesktopPath;
            sfd.Filter = "jpg|*.jpg";
            sfd.FileName = System.IO.Path.ChangeExtension(System.IO.Path.GetRandomFileName(),".jpg");
            if (sfd.ShowDialog() == true)
            {
                
                //ChartToImage.SaveToJpeg(scanGraph, sfd.FileName);
                picHistory.SavePic(sfd.FileName);
            }
        }
    }

    public class WdPicHistoryVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public bool Enable { get; set; }

        /// <summary>
        /// 图片文件夹路径
        /// </summary>
        public string RootPath { get; set; } = @"D:\film_casting_pic";

        /// <summary>
        /// 图片宽度
        /// </summary>
        public int PicWidth { get; set; } = 1024;

        /// <summary>
        /// 图片高度
        /// </summary>
        public int PicHeight { get; set; } = 768 / 3;

        /// <summary>
        /// 数据能保存的天数
        /// </summary>
        public int KeepDay { get; set; } = 360;


        /// <summary>
        /// X轴间隔
        /// </summary>
        public int XStep { get; set; } = 10;
        /// <summary>
        /// Y轴比例
        /// </summary>
        public double YRangePercent { get; set; } = 3;

        /// <summary>
        /// 间隔时间
        /// </summary>
        public double IntervalMin { get; set; } = 1;

        /// <summary>
        /// Y轴%显示
        /// </summary>
        public bool IsPercent { get; set; } = true;

        public RelayCommand OkCmd { get; private set; }
        private PicHistory picHistory;
        public PicHistory PicHistory => picHistory;
        public WdPicHistoryVm() 
        {
            OkCmd = new RelayCommand(Ok);
        }



        public void Init(PicHistory picHistory) 
        {
            Enable = picHistory.Enable;
            RootPath = picHistory.RootPath;
            PicWidth = picHistory.PicWidth;
            PicHeight = picHistory.PicHeight;
            KeepDay = picHistory.KeepDay;
            XStep = picHistory.XStep;
            YRangePercent = picHistory.YRangePercent;
            IntervalMin = picHistory.IntervalMin;
            IsPercent = picHistory.IsPercent;
            this.picHistory = picHistory;
        }

        private void Ok()
        {
            //检查
            if (!System.IO.Directory.Exists(RootPath)) {
                try
                {
                    System.IO.Directory.CreateDirectory(RootPath);
                }
                catch (Exception e) {
                    MessageBox.Show($"图片文件夹路径 {RootPath} 异常,{e}");
                    return;
                }
            }
            if (PicWidth < 100 || PicWidth > 2000)
            {
                MessageBox.Show($"图片宽度 异常, 应该 >=100 and <2000");
                return;
            }
            if (PicHeight < 100 || PicHeight > 2000)
            {
                MessageBox.Show($"图片高度 异常, 应该 >=100 and <2000");
                return;
            }

            if (KeepDay < 1 || KeepDay > 2000)
            {
                MessageBox.Show($"数据能保存的天数 异常, 应该 >=1 and <2000");
                return;
            }

            if (XStep < 1)
            {
                MessageBox.Show($"X轴间隔 异常, 应该 >=1");
                return;
            }

            if (YRangePercent < 1)
            {
                MessageBox.Show($"Y轴比例 异常, 应该 >=1");
                return;
            }

            if (IntervalMin < 1)
            {
                MessageBox.Show($"图片保存时间间隔 异常, 应该 >=1min");
                return;
            }

            picHistory.Enable = Enable;


            picHistory.RootPath = RootPath;
            
            picHistory.PicWidth = PicWidth;
            picHistory.PicHeight = PicHeight;
            picHistory.KeepDay = KeepDay;
            picHistory.XStep = XStep;
            picHistory.YRangePercent = YRangePercent;
            picHistory.IntervalMin = IntervalMin;
            picHistory.IsPercent = IsPercent;

            picHistory.Save();

            MessageBox.Show($"设置完成");
        }
    }
}
