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 AsyncState, object retData)
        {
            Dispatcher.BeginInvoke(new AsyncCBHandler(_SaveReturn), AsyncState, retData);
        }
        private void _SaveReturn(object AsyncState, 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="AsyncState"></param>
        void Save(AsyncCBHandler AsyncDelegate, object AsyncState);
    }

    public abstract class SaveManager : ISaveManager, INotifyPropertyChanged
    {
        private string savepath;
        public string SavePath
        {
            get
            {
                return savepath;
            }
            set
            {
                if (savepath != value)
                {
                    savepath = value;
                    NotifyPropertyChanged("SavePath");
                }
            }
        }


        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 mAsyncDelegate;
        object mAsyncState;
        protected void Return(bool state)
        {
            if (mAsyncDelegate != null)
                mAsyncDelegate.Invoke(mAsyncState, state);
        }
        /// <summary>
        /// 成功或失败，会调用 AsyncDelegate
        /// </summary>
        /// <param name="AsyncDelegate"></param>
        /// <param name="AsyncState"></param>
        public void Save(AsyncCBHandler AsyncDelegate, object AsyncState)
        {
            //需要异步！！！！
            mAsyncDelegate = AsyncDelegate;
            mAsyncState = AsyncState;
            //先复制数据出来
            //再开一个线程输出到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;
        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        #endregion
    }
}
