using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MercatorApi;
using MercatorController;
using System.IO;
using MercatorUi;
using System.Windows.Forms;
using System.Drawing;


namespace MercatorUi.MovableControls.ButtonsCodes
{
    public static class Script
    {
        public static void Exec(MercatorUi.MovableControls.MovableButton clickedButton)
        {
            MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)clickedButton.Form;
            string reqSql = "select s_id,s_image from STOCK where (s_image<>'')";
            if (!string.IsNullOrWhiteSpace(sigForm.Filter))
                reqSql += " and (" + sigForm.Filter + ")";
            List<DataImage> l = Api.Zselect<DataImage>(MercatorUi.Globals.RepData, reqSql);
            if (l != null)
            {
                using (MercatorSqlConnection conn = new MercatorSqlConnection(Globals.RepData, true))
                {
                    if (conn.Connection == null)
                        return;

                    MercatorUi.Progress.ProgressCreate(l.Count);
                    int i = 0;
                    bool err = false;
                    foreach (DataImage di in l)
                    {
                        i++;
                        MercatorUi.Progress.ProgressIncrement(1);
                        MercatorUi.Wait.WaitStatic.WaitWindow("Update Images : " + i + " / " + l.Count);
                        string imagePath = MercatorUi.Globals.RepData + "IMAGES\\" + di.s_image;
                        if (File.Exists(imagePath))
                        {
                            using (System.IO.FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
                            using (System.IO.MemoryStream ms = new MemoryStream())
                            {
                                Image img = Image.FromStream(fs);
                                img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                                using (SqlCommand cmd = new SqlCommand("update STOCK set s_imagebin=@img where s_id=@s_id", conn.Connection))
                                {

                                    cmd.CommandTimeout = 900;
                                    cmd.Parameters.Add(new SqlParameter("@img", SqlDbType.Image)).Value = ms.ToArray();
                                    cmd.Parameters.Add(new SqlParameter("@s_id", SqlDbType.Char)).Value = di.s_id;
                                    if (!Api.SqlExec(Globals.RepData, cmd))
                                    {
                                        err = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    conn.Connection.Close();
                    MercatorUi.Progress.ProgressDestroy();
                    MercatorUi.Wait.WaitStatic.WaitClear();
                    if (!err)
                        Dialogs.Stop("OK !");
                }
            }
        }

        public class DataImage
        {
            public string s_id { get; set; }
            public string s_image { get; set; }
        }
    }
}