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.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Collections.ObjectModel;
using TellerSystem.Library.Ext.Function;
using Microsoft.Win32;

namespace TellerSystem.Contract.Function
{
    /// <summary>
    /// OpenImageFilesViewer.xaml 的交互逻辑
    /// </summary>
    public partial class OpenImageFilesViewer
    {
        public OpenImageFilesViewer()
        {
            InitializeComponent();

            this.ImageCollection = new ObservableCollection<attachImage>();
        }

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {

            OpenFileDialog openFileDialog = new OpenFileDialog();
            //openFileDialog.InitialDirectory = "";
            openFileDialog.Filter = "图片文件|*.jpg";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == true)
            {
                string[] files = openFileDialog.FileNames;

                for (int i = 0; i < files.Length; i++)
                {
                    attachImage img = new attachImage();
                    FileStream stream = new FileInfo(files[i]).OpenRead();
                    img.Bytes = new Byte[stream.Length];
                    stream.Read(img.Bytes, 0, Convert.ToInt32(stream.Length));
                    img.ImagePath = files[i];
                    this.ImageCollection.Add(img);
                }
            }

            this.listBox1.UpdateLayout();
            Platform.Presentation.FocusManagers.FocusManager.GetManagerInstance(this).Refresh();
        }

        public ObservableCollection<attachImage> ImageCollection
        {
            get { return (ObservableCollection<attachImage>)GetValue(ImageCollectionProperty); }
            set { SetValue(ImageCollectionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ImageCollection.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImageCollectionProperty =
            DependencyProperty.Register("ImageCollection", typeof(ObservableCollection<attachImage>), typeof(OpenImageFilesViewer), new UIPropertyMetadata(null));

        private void DeleteButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            attachImage img = (attachImage)((Button)sender).Tag;

            this.ImageCollection.Remove(img);
        }

        public class attachImage : DependencyObject
        {
            public string ImagePath
            {
                get { return (string)GetValue(ImagePathProperty); }
                set { SetValue(ImagePathProperty, value); }
            }

            // Using a DependencyProperty as the backing store for ImagePath.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty ImagePathProperty =
                DependencyProperty.Register("ImagePath", typeof(string), typeof(attachImage), new UIPropertyMetadata(null));


            public byte[] Bytes
            {
                get { return (byte[])GetValue(BytesProperty); }
                set { SetValue(BytesProperty, value); }
            }

            // Using a DependencyProperty as the backing store for Bytes.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty BytesProperty =
                DependencyProperty.Register("Bytes", typeof(byte[]), typeof(attachImage), new UIPropertyMetadata(new byte[0]));
        }

        private void SubmitButton_Click(object sender, RoutedEventArgs e)
        {
            Platform.ServiceProxy.ServiceFunction.Entitys.AttchedFiles[] files = new Platform.ServiceProxy.ServiceFunction.Entitys.AttchedFiles[this.ImageCollection.Count];

            for (int i = 0; i < this.ImageCollection.Count; i++)
            {
                files[i] = new Platform.ServiceProxy.ServiceFunction.Entitys.AttchedFiles();
                files[i].Filedata = this.ImageCollection[i].Bytes;
            }

            this.FilesId = Platform.ServiceProxy.ServiceHelper.FilesHandle.PutAttachedFiles(files);

            this.Close();
        }

        /// <summary>
        /// 获得服务端返回的文件id，这时候文件已经上传到服务端
        /// </summary>
        public string[] FilesId
        {
            get;
            private set;
        }

        /// <summary>
        /// 关闭交易
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
