using System;
using System.Linq;
using System.Net;
using System.IO;
using System.Drawing;

namespace AgencySystem
{
    public class FTPLayout
    {
        private static String ftpURL = "ftp://127.0.0.1/";
        private static String ftpUsernameWrite = "robertosAgency";
        private static String ftpPasswordWrite = "RobertMichalek2017";
        private static string[] enabledFTPFolders = { "restaurants", "notSorted" };
        private static String ftpUsernameRead = "robertosAgencyRead";
        private static String ftpPasswordRead= "RobertMichalek2017";

        public static string FTPUpload(string source, string folderName)
        {
            string fileName = "";
            string extention = "";
            string ftpFilePath = "";
            try
            {
                extention = Path.GetExtension(source);
                fileName = Path.GetFileName(source.Remove(source.Length - extention.Length));
                if (!enabledFTPFolders.Contains(folderName))
                {
                    folderName = "notSorted";
                }
                
                ftpFilePath = folderName + "/" + fileName + "_" + DateTime.Now.ToString("yyyyMMdd-HHmmss") + extention;
                string ftpFullPath = ftpURL + ftpFilePath;
                FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpFullPath);
                ftp.Credentials = new NetworkCredential(ftpUsernameWrite, ftpPasswordWrite);

                ftp.KeepAlive = true;
                ftp.UseBinary = true;
                ftp.Method = WebRequestMethods.Ftp.UploadFile;

                FileStream fs = File.OpenRead(source);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                fs.Close();

                Stream ftpstream = ftp.GetRequestStream();
                ftpstream.Write(buffer, 0, buffer.Length);
                ftpstream.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ftpFilePath;
        }
        public static Bitmap FTPRead(string filePath)
        {
             string filePathFull = ftpURL + filePath;
            if (checkFTPExists(filePathFull))
            { 
                byte[] imgByte = GetImgByte(filePathFull);
                Bitmap imageBitmap = ByteToImage(imgByte);
                return imageBitmap;
            }
            else {
                return null;
            }
        }

        private static bool checkFTPExists(string ftpFilePath)
        {
            FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(ftpFilePath);
            ftpRequest.Credentials = new NetworkCredential(ftpUsernameRead, ftpPasswordRead);
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            try
            {
                FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            }
            catch
            {
                return false;
            }
            return true;
        }

        private static byte[] GetImgByte(string ftpFilePath)
        {
            WebClient ftpClient = new WebClient();
            ftpClient.Credentials = new NetworkCredential(ftpUsernameRead, ftpPasswordRead);
            
            byte[] imageByte = ftpClient.DownloadData(ftpFilePath);
            
            return imageByte;
        }

        private static Bitmap ByteToImage(byte[] blob)
        {
            MemoryStream mStream = new MemoryStream();
            byte[] pData = blob;
            mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
            Bitmap bm = new Bitmap(mStream, false);
            mStream.Dispose();
            return bm;
        }

        private static FtpWebRequest GetRequest(string uriString)
        {
            var request = (FtpWebRequest)WebRequest.Create(uriString);
            request.Credentials = new NetworkCredential(ftpUsernameRead, ftpPasswordRead);
            request.Method = WebRequestMethods.Ftp.GetFileSize;

            return request;
        }

        /*private static bool checkFileExists(WebRequest request)
        {
            try
            {
                request.GetResponse();
                return true;
            }
            catch
            {
                return false;
            }
        }*/
    }
}
