using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using FLY.Thick.Base.IService;
using FObjBase;
using System.ComponentModel;
using System.Threading;

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// Window_SaveDatas.xaml 的交互逻辑
    /// </summary>
    public partial class WdSaveDatas : FLY.ControlLibrary.WindowBigClose
    {
        ISaveManager mManager;
        public WdSaveDatas()
        {
            InitializeComponent();
        }
        public void Init(ISaveManager manager)
        {
            mManager = manager;
            this.DataContext = mManager;
        }


        private void button_open_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.SaveFileDialog open = new System.Windows.Forms.SaveFileDialog();
            open.Filter = "csv文件|*.csv";
            open.Title = "保存数据";
            open.FileName = System.IO.Path.GetFileName(mManager.SavePath);

            if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                mManager.SavePath = open.FileName;
            }
        }

        private void button_save_Click(object sender, RoutedEventArgs e)
        {
            FLY.ControlLibrary.Window_Tip.Show("数据保存", "等待。。。。", TimeSpan.FromSeconds(2));
            mManager.Save(SaveReturn, null);
            this.Close();
        }
        private void SaveReturn(object asyncContext, object retData)
        {
            Dispatcher.BeginInvoke(new AsyncCBHandler(_SaveReturn), asyncContext, retData);
        }
        private void _SaveReturn(object asyncContext, object retData)
        {

            bool ret = (bool)retData;
            if (ret)
            {
                FLY.ControlLibrary.Window_Tip.Show("数据保存", "成功输出到" + mManager.SavePath, TimeSpan.FromSeconds(2));
            }
            else
            {
                FLY.ControlLibrary.Window_Tip.Show("数据保存", "保存失败", TimeSpan.FromSeconds(2));
            }
        }
    }

    public interface ISaveManager
    {
        string SavePath { get; set; }

        /// <summary>
        /// 成功或失败,会调用 asyncDelegate
        /// </summary>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        void Save(AsyncCBHandler asyncDelegate, object asyncContext);
    }

    public abstract class SaveManager : ISaveManager, INotifyPropertyChanged
    {

        public string SavePath { get; set; }


        public void PreSave()
        {
            string directory = System.IO.Path.GetDirectoryName(SavePath);
            if (string.IsNullOrEmpty(directory))
            {
                directory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            }

            SavePath = directory + @"\" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".csv";
        }


        AsyncCBHandler asyncDelegate;
        object asyncContext;
        protected void Return(bool state)
        {
            asyncDelegate?.Invoke(asyncContext, state);
        }
        /// <summary>
        /// 成功或失败,会调用 AsyncDelegate
        /// </summary>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        public void Save(AsyncCBHandler asyncDelegate, object asyncContext)
        {
            //需要异步!!!!
            this.asyncDelegate = asyncDelegate;
            this.asyncContext = asyncContext;
            //先复制数据出来
            //再开一个线程输出到csv
            if (!Backup())
            {
                Return(false);
            }
            else
            {
                Thread t = new Thread(Output);
                t.Start();
            }
        }
        /// <summary>
        /// 备份数据
        /// </summary>
        protected abstract bool Backup();
        protected abstract void Output();

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
    }
}