using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using WjwLib.API.System;
using WjwLib.API.Algorithm;

namespace GatewayTool
{
    public partial class FormParity : FormBase
    {
        private char _splitter = ',';

        //public string TitleText
        //{
        //    get => this.Title;
        //    set => this.Title = value;
        //}

        public FormParity()
        {
            InitializeComponent();
            this.IsMinimizable = false;
            this.IsMaximizable = false;
            this.Title = "校验计算器";
            this.ShowInTaskbar = false;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            rtHex.Clear();
            txtParity.Clear();
            rtHex.Focus();
        }

        private void btnCalc_Click(object sender, EventArgs e)
        {
            string data = rtHex.Text.Trim().Replace("\n", "");
            bool isLegal = Regex.IsMatch(data.Replace(" ", ""), "^[0-9A-Fa-f]+$");
            if (isLegal)
            {
                string hex = ConvertionUtility.FreeHexStringToFormal(data, _splitter);
                rtHex.Text = hex.Replace(_splitter, ' ');
                byte[] bytes = ConvertionUtility.HexStringToByteArray(hex, _splitter.ToString());
                string result = string.Empty;
                bool res = true;
                int sum = 0;
                if (rdoSum.Checked)
                {
                    res = Parity.Sum(bytes.ToList(), out sum);
                    result = sum.ToString("X2");
                }
                else if (rdoXor.Checked)
                {
                    res = Parity.Xor(bytes.ToList(), out sum);
                    result = sum.ToString("X2");
                }
                else if (rdoCrc.Checked)
                {
                    byte[] crc = Parity.CRC16(bytes);
                    result = string.Format("{0} {1} (低字节在前，高字节在后)", crc[0].ToString("X2"), crc[1].ToString("X2"));
                }
                if (res)
                {
                    txtParity.Text = result;
                }
                else
                {
                    MessageBox.Show("计算失败！", "注意", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("数据包含非十六进制的字符，无法计算！", "注意", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void rtHex_TextChanged(object sender, EventArgs e)
        {
            btnCalc.Enabled = !string.IsNullOrEmpty(rtHex.Text.Trim());
            btnClear.Enabled = !string.IsNullOrEmpty(rtHex.Text.Trim());
            if (string.IsNullOrEmpty(rtHex.Text.Trim()))
            {
                txtParity.Clear();
            }
        }

        private void rtHex_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = "0123456789ABCDEF ".IndexOf(char.ToUpper(e.KeyChar)) < 0;
        }
    }
}