using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace 开发辅助工具.Tools
{
    public partial class FrmEncode : DockContent
    {
        public FrmEncode()
        {
            InitializeComponent();
            CbbEncodingCode.SelectedIndex = 0;
        }
        RyWeb.QuickWeb web = new RyWeb.QuickWeb();
        private void AutoCode()
        {
            string from_text = TxtFromCode.Text;
            if (from_text == "") { TxtResultCode.Text = ""; return; }
            #region 获取编码
            Encoding encoding = Encoding.UTF8;
            switch (CbbEncodingCode.SelectedIndex)
            {
                case 0: encoding = Encoding.UTF8; break;
                case 1: encoding = Encoding.GetEncoding("GBK"); break;
                case 2: encoding = Encoding.GetEncoding("GB2312"); break;
                case 3: encoding = Encoding.ASCII; break;
                case 4: encoding = Encoding.Unicode; break;
                case 5: encoding = Encoding.BigEndianUnicode; break;
                case 6: encoding = Encoding.UTF7; break;
                case 7: encoding = Encoding.UTF32; break;
            }
            #endregion
            if (RbBase64Encode.Checked)
            {
                #region Base64加密
                try
                {
                    byte[] bytedata = encoding.GetBytes(from_text);
                    TxtResultCode.Text = Convert.ToBase64String(bytedata, 0, bytedata.Length);
                }
                catch
                {
                    TxtResultCode.Text = "";
                }
                #endregion
            }
            else if (RbBase64Decode.Checked)
            {
                #region Base64解密
                try
                {
                    byte[] bytes = Convert.FromBase64String(from_text);
                    TxtResultCode.Text = encoding.GetString(bytes);
                }
                catch
                {
                    TxtResultCode.Text = "";
                }
                #endregion
            }
            else if (RbURLEncode.Checked)
            {
                TxtResultCode.Text = web.UrlEncode(from_text, encoding);
            }
            else if (RbURLDecode.Checked)
            {
                TxtResultCode.Text = RyWeb.WebDecode.UrlDecode(from_text, encoding);
            }
            else if (RbUnicodeToChs.Checked)
            {
                #region Unicode转字符串
                try
                {
                    TxtResultCode.Text = Regex.Unescape(from_text);
                }
                catch { TxtResultCode.Text = ""; }
                #endregion
            }
            else if (RbChsToUnicode.Checked)
            {
                #region 字符串转Unicode码
                try
                {
                    TxtResultCode.Text = GetUnicode(from_text);
                }
                catch { TxtResultCode.Text = ""; }
                string GetUnicode(string text)
                {
                    string result = "";
                    for (int i = 0; i < text.Length; i++)
                    {
                        //if ((int)text[i] > 32 && (int)text[i] < 127)
                        //{
                        //    result += text[i].ToString();
                        //}
                        //else
                        result += string.Format("\\u{0:x4}", (int)text[i]);
                    }
                    return result;
                }
                #endregion
            }
            else if (RbUTF8ToChs.Checked)
            {
                #region UTF8转字符串
                try
                {
                    TxtResultCode.Text = RyWeb.WebDecode.Unescape(from_text.Replace("&#x","%u").Replace(";", ""));
                }
                catch { TxtResultCode.Text = ""; }
                #endregion
            }
            else if (RbChsToUTF8.Checked)
            {
                #region 字符串转UTF8
                try
                {
                    TxtResultCode.Text = RyWeb.WebDecode.Escape(from_text);
                }
                catch { TxtResultCode.Text = ""; }
                #endregion
            }
            else if (RbMD5.Checked)
            {
                #region 获取MD5
                try
                {
                    TxtResultCode.Text = rySafe.MD5Sha1.GetMD5(from_text);
                }
                catch { TxtResultCode.Text = ""; }
                #endregion
            }
            else if (RbSHA1.Checked)
            {
                #region 获取SHA1
                try
                {
                    TxtResultCode.Text = rySafe.MD5Sha1.GetSHA1(from_text);
                }
                catch { TxtResultCode.Text = ""; }
                #endregion
            }
            else if (RbSHA224.Checked)
            {
                #region 获取SHA224
                try
                {
                    //TxtResultCode.Text = rySafe.MD5Sha1.GetSHA224(from_text);
                }
                catch { TxtResultCode.Text = ""; }
                #endregion
            }
            else if (RbSHA256.Checked)
            {
                #region 获取SHA256
                try
                {
                    TxtResultCode.Text = rySafe.MD5Sha1.GetSHA256(from_text);
                }
                catch { TxtResultCode.Text = ""; }
                #endregion
            }
            else if (RbSHA512.Checked)
            {
                #region 获取SHA256
                try
                {
                    TxtResultCode.Text = rySafe.MD5Sha1.GetSHA512(from_text);
                }
                catch { TxtResultCode.Text = ""; }
                #endregion
            }
        }
        public string get_uft8(string unicodeString)
        {
            UTF8Encoding utf8 = new UTF8Encoding();
            Byte[] encodedBytes = utf8.GetBytes(unicodeString);
            String decodedString = utf8.GetString(encodedBytes);
            return decodedString;
        }
        public static string gb2312_utf8(string text)
        {
            //声明字符集   
            System.Text.Encoding utf8, gb2312;
            //gb2312   
            gb2312 = System.Text.Encoding.GetEncoding("gb2312");
            //utf8   
            utf8 = System.Text.Encoding.GetEncoding("utf-8");
            byte[] gb;
            gb = gb2312.GetBytes(text);
            gb = System.Text.Encoding.Convert(gb2312, utf8, gb);
            //返回转换后的字符   
            return utf8.GetString(gb);
        }

        private void CbbEncodingCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            AutoCode();
        }

        private void RbBase64Encode_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton chk = (RadioButton)sender;
            if (chk.Checked)
            {
                CbbEncodingCode.Enabled = true;
            }
            AutoCode();
        }

        private void RbUnicodeToChs_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton chk = (RadioButton)sender;
            if (chk.Checked)
            {
                CbbEncodingCode.Enabled = false;
            }
            AutoCode();
        }

        private void RbNoEncoding_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton chk = (RadioButton)sender;
            if (chk.Checked)
            {
                CbbEncodingCode.Enabled = false;
            }
            AutoCode();
        }

        private void TxtFromCode_TextChanged(object sender, EventArgs e)
        {
            AutoCode();
        }

        private void TxtFromCode_DoubleClick_1(object sender, EventArgs e)
        {
            ryControls.Controls.RichTextBox2 txt = (ryControls.Controls.RichTextBox2)sender;
            开发辅助工具.Controls.FrmText frm = new Controls.FrmText
            {
                Icon = Icon
            };
            frm.richTextBox1.Text = txt.Text;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txt.Text = frm.richTextBox1.Text;
            }
        }
    }
}
