using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace JWH
{

    public static class ExtensionAPI
    {

        /// <summary>
        /// 네트워크 리소스
        /// </summary>
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct NETRESOURCE
        {
            public uint dwScope;
            public uint dwType;
            public uint dwDisplayType;
            public uint dwUsage;
            public string lpLocalName;
            public string lpRemoteName;
            public string lpComment;
            public string lpProvider;
        }

        /// <summary>
        /// Set Tab stops to a width of 4
        /// </summary>
        private const int EM_SETTABSTOPS = 0x00CB;

        /// <summary>
        /// 네트워크 드라이브 연결
        /// </summary>
        /// <param name="hwndOwner"></param>
        /// <param name="lpNetResource"></param>
        /// <param name="lpPassword"></param>
        /// <param name="lpUserID"></param>
        /// <param name="dwFlags"></param>
        /// <param name="lpAccessName"></param>
        /// <param name="lpBufferSize"></param>
        /// <param name="lpResult"></param>
        /// <returns></returns>
        [DllImport("mpr.dll", CharSet = CharSet.Auto)]
        public static extern int WNetUseConnection(
                    IntPtr hwndOwner,
                    [MarshalAs(UnmanagedType.Struct)] ref NETRESOURCE lpNetResource,
                    string lpPassword,
                    string lpUserID,
                    uint dwFlags,
                    StringBuilder lpAccessName,
                    ref int lpBufferSize,
                    out uint lpResult);

        /// <summary>
        /// 네트워크 드라이브 해제
        /// </summary>
        /// <param name="lpName"></param>
        /// <param name="dwFlags"></param>
        /// <param name="fForce"></param>
        /// <returns></returns>
        [DllImport("mpr.dll", EntryPoint = "WNetCancelConnection2", CharSet = CharSet.Auto)]
        public static extern int WNetCancelConnection2A(string lpName, int dwFlags, int fForce);

        /// <summary>
        /// 네트워크 드라이브 연결
        /// </summary>
        /// <param name="server"></param>
        /// <param name="uid"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static int ConnectRemoteServer(string server, string uid, string pwd)
        {
            int capacity = 64;
            uint resultFlags = 0;
            uint flags = 0;
            System.Text.StringBuilder sb = new System.Text.StringBuilder(capacity);
            NETRESOURCE ns = new NETRESOURCE();
            ns.dwType = 1;              // 공유 디스크
            ns.lpLocalName = null;      // 로컬 드라이브 지정하지 않음
            ns.lpRemoteName = $@"\\{server}";
            ns.lpProvider = null;
            int result = 0;

            result = WNetUseConnection(IntPtr.Zero, ref ns, pwd, uid, flags, sb, ref capacity, out resultFlags);
            return result;
        }

        /// <summary>
        /// 네트워크 드라이브 해제
        /// </summary>
        /// <param name="server"></param>
        /// <returns></returns>
        public static int CencelRemoteServer(string server)
        {
            string lpRemoteName = $@"\\{server}";
            int result = 0;

            result = WNetCancelConnection2A(lpRemoteName, 1, 0);
            return result;
        }

        /// <summary>
        /// SendMessage
        /// </summary>
        /// <param name="h"></param>
        /// <param name="msg"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr h, int msg, int wParam, int[] lParam);

        /// <summary>
        /// 텍스트박스의 탭 간격을 설정합니다.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="tabWidths">탭간격</param>
        public static void SetTabWidth(this TextBox sender, params int[] tabWidths)
        {
            Graphics graphics = sender.CreateGraphics();
            int characterWidth = 4;
            //characterWidth = (int)graphics.MeasureString("A", sender.Font).Width;

            int[] lParams = new int[tabWidths.Length];
            for(int i=0; i< tabWidths.Length; i++)
            {
                lParams[i] = tabWidths[i] * characterWidth;
                if (i > 0) lParams[i] += lParams[i - 1];
            }

            IntPtr ptr = SendMessage(sender.Handle, EM_SETTABSTOPS, lParams.Length, lParams);
        }

    }

}
