using ManualSoftwareSystem.Operation_Manual;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ManualSoftwareSystem.Master_Maintenance
{
    public partial class FrmMasterMaintenanceImage : Form
    {
        public dynamic objLanguage;
        private string pictureTxt = string.Empty;

        public FrmMasterMaintenanceImage(string pictureTxt)
        {
            InitializeComponent();
            this.objLanguage = FrmOperationManualOperator.objLanguage;
            this.pictureTxt = pictureTxt;
        }

        private void btnBrowsePictureFile_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.Filter =
                        "Images (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" +
                        "All files (*.*)|*.*";

            this.openFileDialog1.Multiselect = false;
            this.openFileDialog1.Title = objLanguage.Translate("Select Photos");

            DialogResult dr = this.openFileDialog1.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                foreach (String file in openFileDialog1.FileNames)
                {
                    try
                    {
                        PictureBox imageControl = new PictureBox();
                        //imageControl.Height = 640;
                        //imageControl.Width = 840;

                        //Image.GetThumbnailImageAbort myCallback =
                        //        new Image.GetThumbnailImageAbort(ThumbnailCallback);
                        Bitmap myBitmap = new Bitmap(file);

                        ExifRotate(myBitmap);

                        int sourceWidth = myBitmap.Width;
                        int sourceHeight = myBitmap.Height;

                        //Image myThumbnail = myBitmap.GetThumbnailImage(sourceWidth, sourceHeight,
                        //    myCallback, IntPtr.Zero);

                        //imageControl.Height = sourceHeight;
                        //imageControl.Width = sourceWidth;
                        //imageControl.Image = myThumbnail;

                        pictureBox1.Height = sourceHeight;
                        pictureBox1.Width = sourceWidth;
                        //pictureBox1.Image = myThumbnail;
                        pictureBox1.Image = myBitmap;

                        FrmMasterMaintenanceDetail.picturePartFile = file;
                    }

                    catch (Exception ex)
                    {
                        MessageBox.Show("Error: " + ex.Message);
                    }
                }
            }

        }

        private const int exifOrientationID = 0x112; //274

        public static void ExifRotate(Image img)
        {
            if (!img.PropertyIdList.Contains(exifOrientationID))
                return;

            var prop = img.GetPropertyItem(exifOrientationID);
            int val = BitConverter.ToUInt16(prop.Value, 0);
            var rot = RotateFlipType.RotateNoneFlipNone;

            if (val == 3 || val == 4)
                rot = RotateFlipType.Rotate180FlipNone;
            else if (val == 5 || val == 6)
                rot = RotateFlipType.Rotate90FlipNone;
            else if (val == 7 || val == 8)
                rot = RotateFlipType.Rotate270FlipNone;

            if (val == 2 || val == 4 || val == 5 || val == 7)
                rot |= RotateFlipType.RotateNoneFlipX;

            if (rot != RotateFlipType.RotateNoneFlipNone)
            {
                img.RotateFlip(rot);
                img.RemovePropertyItem(exifOrientationID);
            }
        }

        public bool ThumbnailCallback()
        {
            return false;
        }

        private void FrmMasterMaintenanceImage_Load(object sender, EventArgs e)
        {
            this.Text = objLanguage.Translate("Master Maintenance Image");
            btnBrowsePictureFile.Text = objLanguage.Translate("Browse Picture File");

            FrmMasterMaintenanceDetail.picturePartFile = string.Empty;

            if (pictureTxt != string.Empty)
            {
                //byte[] data;
                //using (MemoryStream ms = new MemoryStream())
                //{
                //    data = Convert.FromBase64String(pictureTxt);

                //    //Image.GetThumbnailImageAbort myCallback1 =
                //    //        new Image.GetThumbnailImageAbort(ThumbnailCallback);
                //    //Image myThumbnail1 = byteArrayToImage((byte[])data).GetThumbnailImage(840, 640,
                //    //    myCallback1, IntPtr.Zero);

                //    //pictureBox1.Image = myThumbnail1;
                //}

                byte[] bytes = Convert.FromBase64String(pictureTxt);

                using (MemoryStream ms = new MemoryStream(bytes))
                {
                    Image image = Image.FromStream(ms);

                    ExifRotate(image);
                    pictureBox1.Height = image.Height;
                    pictureBox1.Width = image.Width;
                    pictureBox1.Image = image;

                    ms.Dispose();
                }
            }
        }

        public Image byteArrayToImage(byte[] byteArrayIn)
        {
            System.Drawing.Bitmap bitmap = null;
            ImageConverter converter = new ImageConverter();
            System.Drawing.Image img =
            (System.Drawing.Image)converter.ConvertFrom(byteArrayIn);
            bitmap = (System.Drawing.Bitmap)img;
            return bitmap;
        }

    }
}
