// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2011 Thomas Klose & Torsten Schache, Hiperscan GmbH Dresden, Germany
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.


using System;
using System.Runtime.InteropServices;

using Hiperscan.Unix;


namespace Hiperscan.Extensions.ApoIdent
{
    
    public class WindowsShell
    {
        private enum FileOperations
        {
            FO_MOVE   = 0x0001,
            FO_COPY   = 0x0002,
            FO_DELETE = 0x0003,
            FO_RENAME = 0x0004
        }
        
        [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
        private struct SHFILEOPSTRUCT
        {
            public IntPtr hwnd;
            public UInt32 wFunc;
            public IntPtr pFrom;
            public IntPtr pTo;
            public UInt16 fFlags;
            public Int32  fAnyOperationsAborted;  
            public IntPtr hNameMappings;
            
            [MarshalAs(UnmanagedType.LPWStr)]
            public String lpszProgressTitle;           
        }    
        
        [Flags]
        private enum ShellFileOperationFlags
        {
            FOF_MULTIDESTFILES        = 0x0001,
            FOF_CONFIRMMOUSE          = 0x0002,
            FOF_SILENT                = 0x0004,
            FOF_RENAMEONCOLLISION     = 0x0008,
            FOF_NOCONFIRMATION        = 0x0010,
            FOF_WANTMAPPINGHANDLE     = 0x0020,
            FOF_ALLOWUNDO             = 0x0040,
            FOF_FILESONLY             = 0x0080,
            FOF_SIMPLEPROGRESS        = 0x0100,
            FOF_NOCONFIRMMKDIR        = 0x0200,
            FOF_NOERRORUI             = 0x0400,
            FOF_NOCOPYSECURITYATTRIBS = 0x0800,
            FOF_NORECURSION           = 0x1000,
            FOF_NO_CONNECTED_ELEMENTS = 0x2000,
            FOF_WANTNUKEWARNING       = 0x4000,
            FOF_NORECURSEREPARSE      = 0x8000
        }

        [DllImport("shell32.dll" , CharSet = CharSet.Unicode)]
        private static extern Int32 SHFileOperation(ref SHFILEOPSTRUCT lpFileOp);
        
        public static void CopyFiles(string[] source, string destination, string title)
        {
            // create double null terminated strings
            string multi_source = string.Empty;
            if (source != null && source.Length > 0)
            {
                foreach (string s in source)
                {
                    multi_source += s + '\0';
                }
                multi_source += '\0';
            }
            
            destination += '\0';
            destination += '\0';
            
            SHFILEOPSTRUCT file_op = new SHFILEOPSTRUCT();
            file_op.hwnd  = IntPtr.Zero;
            file_op.wFunc = (uint)FileOperations.FO_COPY;
            file_op.pFrom = Marshal.StringToHGlobalUni(multi_source);
            file_op.pTo   = Marshal.StringToHGlobalUni(destination);
            file_op.fFlags = (ushort)0;
            file_op.lpszProgressTitle = title;
            file_op.fAnyOperationsAborted = 0;
            file_op.hNameMappings = IntPtr.Zero;
            
            int result = WindowsShell.SHFileOperation(ref file_op);            
            if (result != 0)
                throw new Exception(Catalog.GetString("File operation failed."));
 
            if (file_op.fAnyOperationsAborted != 0)
                throw new Exception(Catalog.GetString("File operation was aborted."));
        }
    }
}