using System;
using System.Collections;
using static System.Configuration.ConfigurationSettings;
using System.Data;
using System.Runtime.InteropServices;
using System.Xml;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;

namespace AutoRunLib_C
{

    static class modWIN
    {

        // ReviseDate : 2003/08/07,修改處理AdditionalXML
        // ReviseDate : 2003/08/05,新增Public gReturnArray
        // ReviseDate : 2003/07/01,更改AddFlow產生的Error(CInput)
        // ReviseDate : 2002/11/04,新增CUnInput function

        public static string gComputerName = System.Environment.MachineName;
        public static string gUserNo = "AUTService";
        public static string gMESWebServiceHost = GetAppSettings("MesWebServiceHost");
        public static string gCUSWebServiceHost = GetAppSettings("CusWebServiceHost");
        public static string gERPWebServiceHost = GetAppSettings("ERPWebServiceHost");
        public static string gLanguageMode = GetAppSettings("LanguageMode");

        // Add By Peter 2005/6/20
        public static string gSettingMode = "";


        public static string PasswordEncoding(ref string Password)
        {
            string PasswordEncodingRet = default(string);
            // 此 Function 將傳入值加以編碼後傳出,編碼後長度不變
            // 傳入值: 密碼
            // 傳回值: 編碼後密碼

            // Vernam密碼是由Gilbert Vernam在1918年發明的
            string g_Key = "xNDFz6LH67LOv7xKbWFpbMu1wejrM7SzvV4tLRvq3X47m708O1xMHLoaMNCqGhoaEN";
            string strChar, iCryptChar, strEncrypted = default(string);
            int i, iKeyChar, iStringChar;

            var loopTo = Strings.Len(Password);
            for (i = 1; i <= loopTo; i++)
            {
                iKeyChar = Strings.Asc(Strings.Mid(g_Key, i, 1));
                iStringChar = Strings.Asc(Strings.Mid(Password, i, 1));
                iCryptChar = (iKeyChar ^ iStringChar).ToString();
                strEncrypted = strEncrypted + Strings.Chr(Conversions.ToInteger(iCryptChar));
            }

            PasswordEncodingRet = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(strEncrypted));
            return PasswordEncodingRet;

        }


        // 2017-06-05, Joe, 依XML標準定義轉換特殊字元
        public static string CInputWithXMLStandard(ref string strInput)
        {
            string CInputWithXMLStandardRet = default(string);

            // 轉換 ' 為 &apos; 
            CInputWithXMLStandardRet = Strings.Replace(strInput, "'", "&apos;");

            // 轉換 & 為 &amp;
            CInputWithXMLStandardRet = Strings.Replace(CInputWithXMLStandardRet, "&", "&amp;");

            // 轉換 " 為 quot;
            CInputWithXMLStandardRet = Strings.Replace(CInputWithXMLStandardRet, "\"", "&quot;");

            // 轉換 > 為 &gt;
            CInputWithXMLStandardRet = Strings.Replace(CInputWithXMLStandardRet, ">", "&gt;");

            // 轉換 < 為 &lt;
            CInputWithXMLStandardRet = Strings.Replace(CInputWithXMLStandardRet, "<", "&lt;");
            return CInputWithXMLStandardRet;

        }

        // 2017-06-05, Joe, 依XML標準定義反轉換特殊字元
        public static string CUnInputWithXMLStandard(ref string strInput)
        {
            string CUnInputWithXMLStandardRet = default(string);

            // 轉換 ' 為 "&apos;"
            CUnInputWithXMLStandardRet = Strings.Replace(strInput, "&apos;", "'");

            // 轉換 " 為 quot;
            CUnInputWithXMLStandardRet = Strings.Replace(CUnInputWithXMLStandardRet, "&quot;", "\"");

            // 轉換 & 為 &amp;
            CUnInputWithXMLStandardRet = Strings.Replace(CUnInputWithXMLStandardRet, "&amp;", "&");

            // 轉換 > 為 &gt;
            CUnInputWithXMLStandardRet = Strings.Replace(CUnInputWithXMLStandardRet, "&gt;", ">");

            // 轉換 < 為 &lt;
            CUnInputWithXMLStandardRet = Strings.Replace(CUnInputWithXMLStandardRet, "&lt;", "<");
            return CUnInputWithXMLStandardRet;

        }

        // **Add by py 2003/12/04,Gary Lu 20120911:以MESWin1~Win4的版本進行替換
        public static string LocalizeWebService(string wsUrl, bool Customize = false)
        {

            string[] tmpString;
            int i;
            tmpString = wsUrl.Split('/');
            if (Customize == true)
            {
                if (!string.IsNullOrEmpty(Strings.Trim(gCUSWebServiceHost)))
                {
                    if (tmpString.Length == 6)
                    {
                        wsUrl = tmpString[0].ToString() + "/" + tmpString[1].ToString() + "/" + gCUSWebServiceHost + "/" + tmpString[4].ToString() + "/" + tmpString[5].ToString();
                    }
                    else if (tmpString.Length == 5)
                    {
                        wsUrl = "http://" + gCUSWebServiceHost + "/" + tmpString[tmpString.Length - 1].ToString();
                    }
                }
            }
            else if (!string.IsNullOrEmpty(Strings.Trim(gMESWebServiceHost)))
            {
                if (tmpString.Length == 6)
                {
                    // wsUrl = "http://" & gMESWebServiceHost & "/" & tmpString(tmpString.Length - 2).ToString & "/" & tmpString(tmpString.Length - 1).ToString
                    wsUrl = tmpString[0].ToString() + "/" + tmpString[1].ToString() + "/" + gMESWebServiceHost + "/" + tmpString[4].ToString() + "/" + tmpString[5].ToString();
                }
                else if (tmpString.Length == 5)
                {
                    wsUrl = "http://" + gMESWebServiceHost + "/" + tmpString[tmpString.Length - 1].ToString();
                }
            }

            return wsUrl;
            // Have to assume the web service is on the machine that this application came from
        } // LocalizeWebService

        public static string LocalizeWebService_ERP(string wsUrl)
        {

            string[] tmpString;
            int i;
            tmpString = wsUrl.Split('/');

            if (!string.IsNullOrEmpty(Strings.Trim(gERPWebServiceHost)))
            {
                wsUrl = tmpString[0].ToString() + "/" + tmpString[1].ToString() + "/" + gERPWebServiceHost + "/" + tmpString[4].ToString();
            }

            return wsUrl;

        }

        // Add By Peter 2005/6/20
        public static string GetAppSettings(string key, string section = "")
        {
            string result = string.Empty;

            try
            {

                if (!string.IsNullOrEmpty(section))
                {
                    result = Conversions.ToString(((Hashtable)GetConfig(section))[key]);
                }
                else if (string.IsNullOrEmpty(gSettingMode) || string.IsNullOrEmpty(gSettingMode) || gSettingMode.Length == 0)
                {
                    result = AppSettings[key];
                }
                else
                {
                    Hashtable ht = (Hashtable)GetConfig(gSettingMode);
                    if (ht != null)
                    {
                        result = Conversions.ToString(ht[key]);
                        if (string.IsNullOrEmpty(result))
                        {
                            result = AppSettings[key];
                            if (string.IsNullOrEmpty(result))
                            {
                                result = "";
                            }
                        }
                    }
                    else
                    {
                        result = AppSettings[key];
                    }
                }
            }

            catch (Exception ex)
            {
                throw ex;
            }

            return result;

        }


        public static string GetSubString(string strValue, int intLength)
        {
            int intLen;
            string strReturn;

            // Dim byteValue As Byte() = System.Text.Encoding.UTF8.GetBytes(strValue)
            // If intLength > byteValue.Length - 1 Then
            // intLen = byteValue.Length - 1
            // End If
            // Dim strReturn As String = System.Text.Encoding.UTF8.GetString(byteValue, 0, intLen)
            intLen = (int)Math.Round(Conversion.Int(intLength / 3d));
            strReturn = Strings.Left(strValue, intLen);

            return strReturn;

        }

    }
}