using Avalonia.Controls;
using Avalonia.Controls.Notifications;
using Avalonia.Input;
using System;
using 常用工具集.Base;

namespace 常用工具集;

public partial class 进制转换及ASCII转换 : UserControl
{

    public 进制转换及ASCII转换()
    {
        InitializeComponent();
    }

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            button1_Click(sender, e);
        }
    }

    private void button1_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        try
        {
            string bin = textBox1.Text.Trim();
            if (bin.Length == 0)
            {
                bin = "0";
            }
            //二进制转换十进制
            int num = Convert.ToInt32(bin, 2);
            //十进制转
            string num8 = Convert.ToString(num, 8);
            //十进制转16进制
            string num16 = Convert.ToString(num, 16).ToUpper();
            textBox1.Text = bin;
            textBox2.Text = num8;
            textBox3.Text = Convert.ToString(num);
            textBox4.Text = num16;
        }
        catch (Exception ex)
        {
            GlobalValues.Error($"转换错误:{ex.Message}");
        }
    }

    private void textBox2_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            button2_Click(sender, e);
        }
    }
    private void button2_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        try
        {
            string num8 = textBox2.Text.Trim();
            if (num8.Length == 0)
            {
                num8 = "0";
            }
            //二进制转换十进制
            int num10 = Convert.ToInt32(num8, 8);
            //十进制转
            string num2 = Convert.ToString(num10, 2);
            //十进制转16进制
            string num16 = Convert.ToString(num10, 16).ToUpper();
            textBox1.Text = num2;
            textBox2.Text = num8;
            textBox3.Text = Convert.ToString(num10);
            textBox4.Text = num16;
        }
        catch (Exception ex)
        {
            GlobalValues.Error($"转换错误:{ex.Message}");
        }
    }

    private void textBox3_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            button3_Click(sender, e);
        }
    }
    private void button3_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        try
        {
            string strNum10 = textBox3.Text.Trim();
            if (strNum10.Length == 0)
            {
                strNum10 = "0";
            }
            //二进制转换十进制
            int num10 = Convert.ToInt32(strNum10);

            string num2 = Convert.ToString(num10, 2);

            string num8 = Convert.ToString(num10, 8);
            //十进制转16进制
            string num16 = Convert.ToString(num10, 16).ToUpper();
            textBox1.Text = num2;
            textBox2.Text = num8;
            textBox3.Text = Convert.ToString(num10);
            textBox4.Text = num16;
        }
        catch (Exception ex)
        {
            GlobalValues.Error($"转换错误:{ex.Message}");
        }
    }
    private void textBox4_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            button4_Click(sender, e);
        }
    }

    private void button4_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        try
        {
            string strNum16 = textBox4.Text.Trim();
            if (strNum16.Length == 0)
            {
                strNum16 = "0";
            }
            //二进制转换十进制
            int num10 = Convert.ToInt32(strNum16, 16);
            string num2 = Convert.ToString(num10, 2);
            string num8 = Convert.ToString(num10, 8);
            //十进制转16进制
            string num16 = Convert.ToString(num10, 16).ToUpper();
            textBox1.Text = num2;
            textBox2.Text = num8;
            textBox3.Text = Convert.ToString(num10);
            textBox4.Text = num16;
        }
        catch (Exception ex)
        {
            GlobalValues.Error($"转换错误:{ex.Message}");
        }
    }


    private void textBox5_KeyDown(object sender, Avalonia.Input.KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            button5_Click(sender, e);
        }
    }
    private void textBox6_KeyDown(object sender, Avalonia.Input.KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            button5_Click(sender, e);
        }
    }
    private void textBox7_KeyDown(object sender, Avalonia.Input.KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            button5_Click(sender, e);
        }
    }
    private void button5_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        try
        {
            string r = textBox5.Text.Trim();
            if (r.Length == 0)
            {
                textBox5.Text = "0";
            }
            int intR = Convert.ToInt32(r);
            if (intR > 255)
            {
                intR = 255;
                textBox5.Text = "255";
            }
            string g = textBox6.Text.Trim();
            if (g.Length == 0)
            {
                textBox6.Text = "0";
            }
            int intG = Convert.ToInt32(g);
            if (intG > 255)
            {
                intG = 255;
                textBox6.Text = "255";
            }
            string b = textBox7.Text.Trim();
            if (b.Length == 0)
            {
                textBox7.Text = "0";
            }
            int intB = Convert.ToInt32(b);
            if (intB > 255)
            {
                intB = 255;
                textBox7.Text = "255";
            }
            string r16 = Convert.ToString(intR, 16).ToUpper().PadLeft(2, '0');
            string g16 = Convert.ToString(intG, 16).ToUpper().PadLeft(2, '0');
            string b16 = Convert.ToString(intB, 16).ToUpper().PadLeft(2, '0');
            textBox8.Text = r16 + g16 + b16;
        }
        catch (Exception ex)
        {
            GlobalValues.Error($"转换错误:{ex.Message}");
        }
    }


    private void textBox8_KeyDown(object sender, Avalonia.Input.KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            button6_Click(sender, e);
        }
    }
    private void button6_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        try
        {
            string value = textBox8.Text.Trim().ToUpper();
            if (value.Length == 0)
            {
                value = "000000";
            }
            else if (value.Length > 6)
            {
                value = value.Substring(0, 6);
            }
            else
            {
                value = value.PadLeft(6, '0');
            }
            textBox8.Text = value;
            string strR = value.Substring(0, 2);
            string strG = value.Substring(2, 2);
            string strB = value.Substring(4, 2);
            int intR = Convert.ToInt32(strR, 16);
            int intG = Convert.ToInt32(strG, 16);
            int intB = Convert.ToInt32(strB, 16);
            textBox5.Text = Convert.ToString(intR);
            textBox6.Text = Convert.ToString(intG);
            textBox7.Text = Convert.ToString(intB);
        }
        catch (Exception ex)
        {
            GlobalValues.Error($"转换错误:{ex.Message}");
        }
    }


    private void textBox9_KeyDown(object sender, Avalonia.Input.KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            button7_Click(sender, e);
        }
    }
    private void button7_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        try
        {
            string strChar = textBox9.Text.Trim();
            if (strChar.Length == 0)
            {
                strChar = "0";
            }
            textBox9.Text = strChar;
            char c = strChar.ToCharArray()[0];
            int intChar = c;
            textBox10.Text = Convert.ToString(intChar);
            textBox11.Text = Convert.ToString(intChar, 16);
        }
        catch (Exception ex)
        {
            GlobalValues.Error($"转换错误:{ex.Message}");
        }
    }
    private void textBox10_KeyDown(object sender, Avalonia.Input.KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            button8_Click(sender, e);
        }
    }
    private void button8_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        try
        {
            string asc2 = textBox10.Text.Trim();
            if (asc2.Length == 0)
            {
                asc2 = "48";
            }
            textBox10.Text = asc2;
            int num = Convert.ToInt32(asc2);
            char c = (char)num;
            textBox9.Text = Convert.ToString(c);
            textBox11.Text = Convert.ToString(num, 16);
        }
        catch (Exception ex)
        {
            GlobalValues.Error($"转换错误:{ex.Message}");
        }
    }
    private void textBox11_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            button9_Click(sender, e);
        }
    }
    private void button9_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        try
        {
            string asc16 = textBox11.Text.Trim();
            if (asc16.Length == 0)
            {
                asc16 = "30";
            }
            textBox11.Text = asc16;
            int num = Convert.ToInt32(asc16, 16);
            char c = (char)num;
            textBox9.Text = Convert.ToString(c);
            textBox10.Text = Convert.ToString(num);
        }
        catch (Exception ex)
        {
            GlobalValues.Error($"转换错误:{ex.Message}");
        }
    }
}