﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ServiceModel;

namespace FileSendReceive
{
    public partial class Form1 : Form
    {
        private IFileHost channel;
        

        public Form1()
        {
            InitializeComponent();

            FileHost fh = new FileHost(this);
            DuplexChannelFactory<IFileHost> cf = new DuplexChannelFactory<IFileHost>(new InstanceContext(fh), "FileHostEP");
            channel = cf.CreateChannel();

            ((ICommunicationObject)channel).Open();
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Text Files(*.txt)|*.txt|All files(*.*)|*.*";
            dlg.FilterIndex = 0;
            dlg.Multiselect = false; //We can only pick one file at a time.
            dlg.RestoreDirectory = true; //Go back to the same directory on re-open.

            DialogResult dr = dlg.ShowDialog(); //Show the open file dialog.  Blocking call.

            if(dr == DialogResult.OK)
            {
                try
                {
                    textBoxFileLocation.Text = dlg.FileName;
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }

        public byte[] SendFile()
        {
            //Turn a file into a byte array.

            if(textBoxFileLocation.Text.Length < 1)
            {
                MessageBox.Show("No file selected.");
                return null;
            }

            try
            {
                FileStream fs = new FileStream(textBoxFileLocation.Text, FileMode.Open, FileAccess.Read);

                byte[] fileArray = new byte[fs.Length];
                fs.Read(fileArray, 0, System.Convert.ToInt32(fs.Length));
                fs.Close();

                return fileArray;
            }
            catch
            {
                MessageBox.Show("File not readable.");
                return null;
            }
        }

        public void ReceiveFile(string fileName, byte[] receivedFile)
        {
            //Turn a byte array into a file.

            if(MessageBox.Show("Receieved File: " + fileName + ".  Would you like to save?", "Save file?", MessageBoxButtons.YesNo) != DialogResult.Yes)
            {
                return;
            }

            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "All files(*.*)|*.*";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;

            DialogResult dr = dlg.ShowDialog();

            if(dr == DialogResult.OK)
            {
                try
                {
                    File.WriteAllBytes(dlg.FileName, receivedFile);
                }
                catch
                {
                    MessageBox.Show("Failed to write file.");
                }
            }
        }

        private void buttonSend_Click(object sender, EventArgs e)
        {
            byte[] fileArray = SendFile();

            if (fileArray != null)
                channel.SendFileToAll(textBoxFileLocation.Text, fileArray);
        }
    }
}
