#define xSECS2HSMS

using JWH;
using JWH.SECS;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DDUtilityApp.LOGPARSER
{
    public partial class FrmHsmsViewer : Form
    {

        #region [ Properties ] ------------------------------------------------

        public SECSMessage SECSMessage { get; set; } = null;

        #endregion

        #region [ FrmTest01 ] -------------------------------------------------

        public FrmHsmsViewer()
        {
            InitializeComponent();

            this.SetLayout();
            this.SetEventHandler();
        }

        public void SetLayout()
        {

            Font font = new Font("돋움체", 9.0F);

            this.panel1.Height = 16;

            this.tboxHsms.Font = font;
            this.tboxHsms.ScrollBars = ScrollBars.Both;
            this.tboxHsms.WordWrap = false;
            this.tboxHsms.MaxLength = 3276700;
            this.tboxHsms.AllowDrop = true;

            this.tboxSecs.Font = font;
            this.tboxSecs.ScrollBars = ScrollBars.Both;
            this.tboxSecs.WordWrap = false;
            this.tboxSecs.MaxLength = 3276700;
            this.tboxSecs.ReadOnly = true;

            this.tboxHexHangul.Font = font;
            this.tboxHexHangul.WordWrap = true;

            this.tboxHangul.Font = font;
            this.tboxHangul.WordWrap = true;

            this.cboxEncoding.DropDownStyle = ComboBoxStyle.DropDownList;
            this.cboxEncoding.Items.Clear();
            this.cboxEncoding.Items.Add(new KeyValuePair<string, Encoding>("Default", Encoding.Default));
            this.cboxEncoding.Items.Add(new KeyValuePair<string, Encoding>("UTF7", Encoding.UTF7));
            this.cboxEncoding.Items.Add(new KeyValuePair<string, Encoding>("UTF8", Encoding.UTF8));
            this.cboxEncoding.Items.Add(new KeyValuePair<string, Encoding>("UTF32", Encoding.UTF32));
            this.cboxEncoding.Items.Add(new KeyValuePair<string, Encoding>("ASCII", Encoding.ASCII));
            this.cboxEncoding.Items.Add(new KeyValuePair<string, Encoding>("Unicode", Encoding.Unicode));
            this.cboxEncoding.Items.Add(new KeyValuePair<string, Encoding>("BigEndianUnicode", Encoding.BigEndianUnicode));
            this.cboxEncoding.DisplayMember = "Key";
            this.cboxEncoding.SelectedIndex = 0;
        }

        public void SetEventHandler()
        {
            this.tboxHsms.DragDrop += Control_DragDrop;
            this.tboxHsms.DragEnter += Control_DragEnter;

            this.btnHtoS.Click += BtnHsmsToSecs_Click;
            this.btnStoH.Click += BtnSecstoHsms_Click;

            this.cboxEncoding.SelectedIndexChanged += HexHangul2Hangul;
            this.tboxHexHangul.TextChanged += HexHangul2Hangul;
        }

        private void HexHangul2Hangul(object sender, EventArgs e)
        {
            string value = this.tboxHexHangul.Text.Replace(" ", "");
            byte[] bytes = value.ConvertBytes(16);

            Encoding encoding = ((KeyValuePair<string, Encoding>)this.cboxEncoding.SelectedItem).Value;
            string result = encoding.GetString(bytes, 0, bytes.Length);

            this.tboxHangul.Text = result;
        }

        #endregion

        #region [ Control Event ] ---------------------------------------------

        private void Control_DragDrop(object sender, DragEventArgs e)
        {
            if (sender == this.tboxHsms)
            {
                string strHsms = (string)e.Data.GetData(DataFormats.Text);
                this.tboxHsms.Text = strHsms;
                this.BtnHsmsToSecs_Click(this.btnHtoS, null);
            }
        }

        private void Control_DragEnter(object sender, DragEventArgs e)
        {
            if (sender == this.tboxHsms)
            {
                if (e.Data.GetDataPresent(DataFormats.Text)) e.Effect = DragDropEffects.Copy;
            }
        }

        private void BtnHsmsToSecs_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.tboxHsms.Text)) return;

                byte[] arrayByte = this.tboxHsms.Text.ConvertBytes(16);
                this.SECSMessage = new SECSMessage(arrayByte);

                //this.tboxSecs.Text = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}] RCVD {item.ToString(0)}";
                this.tboxSecs.Text = $"{this.SECSMessage.ToFullString()}";
                this.propertyGrid1.SelectedObject = this.SECSMessage;
            }
            catch (Exception ex)
            {
                XLogger.Instance.Fatal(ex);
            }
        }

        private void BtnSecstoHsms_Click(object sender, EventArgs e)
        {
            try
            {
#if SECS2HSMS
                if (string.IsNullOrEmpty(this.tboxSecs.Text)) return;
                this.SECSMessage = new SECSMessage(this.tboxSecs.Text);
#else
                if (this.SECSMessage == null) return;
#endif

                //this.tboxSecs.Text = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}] RCVD {item.ToString(0)}";
                this.tboxHsms.Text = this.SECSMessage.ToHexString();
                this.propertyGrid1.SelectedObject = this.SECSMessage;
            }
            catch (Exception ex)
            {
                XLogger.Instance.Fatal(ex);
            }
        }

        #endregion

        #region [ Methods ] ---------------------------------------------------

        #endregion

    }

}