using System;
using System.Drawing;
using System.IO;

namespace Hitsuji.Framework.Utils
{
    public class Base64ConvertUtil
    {
        /// <summary>
        /// コンテンツファイルをBase64形式の文字列へ変換
        /// </summary>
        /// <param name="objectFilePath">対象コンテンツファイルのパス</param>
        /// <returns>Base64形式変換後の文字列</returns>
        public static string convertToBase64(string objectFilePath)
        {
            try
            {
                var stream = new System.IO.FileStream(objectFilePath, 
                                                        System.IO.FileMode.Open, 
                                                        System.IO.FileAccess.Read);
                byte[] bytData = new byte[stream.Length];
                //int readbyte = stream.Read(bytData, 0, (int)stream.Length);
                stream.Close();
                string base64Text = System.Convert.ToBase64String(bytData);
                return base64Text;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Base64形式の文字列をコンテンツファイルへ変換
        /// </summary>
        /// <param name="objectFilePath">対象コンテンツファイルのパス</param>
        /// <returns>Base64形式変換後の文字列</returns>
        public static void convertFileFromBase64(string base64Text, string outputFilePath)
        {
            System.IO.FileStream file = null;
            try
            {
                byte[] bytData = System.Convert.FromBase64String(base64Text);
                file = new System.IO.FileStream(outputFilePath,
                                                System.IO.FileMode.Create,
                                                System.IO.FileAccess.Write);
                file.Write(bytData, 0, bytData.Length);
                //file.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                file.Close();
            }
        }

        /// <summary>
        /// Base64形式の文字列をストリームへ変換
        /// </summary>
        /// <param name="objectFilePath">対象コンテンツファイルのパス</param>
        /// <returns>Base64形式変換後の文字列</returns>
        public static Image convertImageFromBase64(string base64Text)
        {
            if (string.IsNullOrEmpty(base64Text)) return null;
            try
            {
                // Convert Base64 String to byte[]
                byte[] imageBytes = Convert.FromBase64String(base64Text);
                System.IO.MemoryStream ms = new System.IO.MemoryStream(imageBytes, 0, imageBytes.Length);

                // Convert byte[] to Image
                ms.Write(imageBytes, 0, imageBytes.Length);
                Image image = Image.FromStream(ms, true);
                return image;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// ファイルパスに存在するファイルを読み込み、BLOBデータとして取得する
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string filepathToBlob(string path)
        {
            try
            {
                path = "@" + path;
                if (!System.IO.File.Exists(path)) return "";

	            String strBLOBFilePath = path;
	            FileStream fsBLOBFile =  new FileStream(strBLOBFilePath,FileMode.Open, FileAccess.Read);
	            Byte[] bytBLOBData = new Byte[fsBLOBFile.Length]; 
	            fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length);
	            fsBLOBFile.Close();
                return bytBLOBData.ToString();
            }catch(Exception ex)
            {throw ex;}
        }

        public static string imageToBlob(System.Drawing.Image img)
        {
            try
            {
                if (img == null) return "";
                
                ImageConverter imgconv = new ImageConverter();
                Byte[] bytBLOBImage = (Byte[])new ImageConverter().ConvertTo(img, typeof(Byte[]));
                string base64Text = System.Convert.ToBase64String(bytBLOBImage);
                return base64Text;
            }
            catch (Exception ex)
            { throw ex; }
        }

        public static Image streamToImage(string strBLOB)
        {
            if (string.IsNullOrEmpty(strBLOB)) return null;

            Byte[] bytBLOBImg = System.Convert.FromBase64String(strBLOB);
            MemoryStream msBLOBImg = new MemoryStream(bytBLOBImg);
            Image img = Image.FromStream(msBLOBImg);
            return img;
        }

    }
}
