using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//Add code
using System.Runtime.InteropServices; //ini파일 로드 필요라이브러리
namespace Jdas_Mbic
{
    public partial class Debug : Form
    {
        JdasMbic main_form;
        int LineLimit = 500;
        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
        private const int WM_SETREDRAW = 11;
        Serial serial;
        public RadioButton RadioButton_ascii { get => radioButton_ascii; set => radioButton_ascii = value; }
        public RadioButton RadioButton_hex { get => radioButton_hex; set => radioButton_hex = value; }

        public Debug()
        {
            
            InitializeComponent();
            
        }
#if true
        public void Data_Recv_Str(string text)
        {
           
            int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
            try
            {
                try
                {
                    SendMessage(this.Handle, WM_SETREDRAW, false, 0);
                }
                catch (Exception e) { MessageBox.Show(e.StackTrace); }
                if (tbReceived.Lines.Length > nLimitLines)
                {
                    LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
                    while ((tempLines.Count - nLimitLines) > 0)
                    {
                        tempLines.RemoveFirst();
                    }

                    tbReceived.Lines = tempLines.ToArray();

                }
                try
                {
                    SendMessage(this.Handle, WM_SETREDRAW, true, 0);
                }
                catch { return; }
                tbReceived.AppendText(text);
                tbReceived.SelectionStart = tbReceived.Text.Length;//맨 마지막 선택... 
                tbReceived.ScrollToCaret();
                /****
                  *메모리 누수 방지용 코드
                  */
                System.GC.Collect(0, GCCollectionMode.Forced);
                System.GC.WaitForFullGCComplete();

            }
            catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
        }
#endif
#if false
        public void Data_Recv_Hex(byte[] text)
        {
           int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
            try
            {
                try
                {
                    SendMessage(this.Handle, WM_SETREDRAW, false, 0);
                }
                catch (Exception e) { MessageBox.Show(e.StackTrace); }
                if (tbReceived.Lines.Length > nLimitLines)
                {
                    LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
                    while ((tempLines.Count - nLimitLines) > 0)
                    {
                        tempLines.RemoveFirst();
                    }

                    tbReceived.Lines = tempLines.ToArray();

                }
                try
                {
                    SendMessage(this.Handle, WM_SETREDRAW, true, 0);
                }
                catch { return; }
                tbReceived.AppendText(text.ToString());
                tbReceived.SelectionStart = tbReceived.Text.Length;//맨 마지막 선택... 
                tbReceived.ScrollToCaret();
            }
            catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }

        }
#endif
        public void Data_Recv_Hex(byte[] text)
        {
 
            if (checkBoxRecv.Checked == false)
                return;

            string AppendMessage = "[RX]";
            
            int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
            try
            {/*
                try
                {
                    SendMessage(this.Handle, WM_SETREDRAW, false, 0);
                }
                catch (Exception e) { MessageBox.Show(e.StackTrace); }*/
                for (int i = 0; i < text.Length; i++)
                {
                    //AppendMessage += Convert.ToString(text[i], 16);
                    AppendMessage += text[i].ToString("X2");
                    AppendMessage += " ";
                }
                AppendMessage += "\r\n";
                tbReceived.AppendText(AppendMessage);
               
                if (tbReceived.Lines.Length > nLimitLines)
                {
                    LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);

                    while ((tempLines.Count - nLimitLines) > 0)
                    {
                        tempLines.RemoveFirst();
                    }

                    tbReceived.Lines = tempLines.ToArray();
                }
                try
                {
                    SendMessage(this.Handle, WM_SETREDRAW, true, 0);
                }
                catch { return; }
                tbReceived.Select(tbReceived.Text.Length, 0);
                tbReceived.ScrollToCaret();
            }
            catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
            /****
              *메모리 누수 방지용 코드
              */
            System.GC.Collect(0, GCCollectionMode.Forced);
            System.GC.WaitForFullGCComplete();


        }
        public void Data_Send(byte[] text,int count)
        {
            if (checkBoxSend.Checked == false)
                return;

            string AppendMessage = "\n[TX]";
            
            int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
            try
            {
                for (int i = 0; i < count; i++)
                {
                    
                    AppendMessage += text[i].ToString("X2");
                    //AppendMessage += Convert.ToString("{1,2}", text[i].ToString("X2"));
                    //Console.WriteLine("{0,22:D8} {0,:X8}", byteValue);
                    AppendMessage += " ";
                }
                //Console.WriteLine
                tbReceived.AppendText(AppendMessage + "\r\n");
                try
                {
             //       SendMessage(this.Handle, WM_SETREDRAW, false, 0);
                }
                catch { return; }
                if (tbReceived.Lines.Length > nLimitLines)
                {
                    LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);

                    while ((tempLines.Count - nLimitLines) > 0)
                    {
                        tempLines.RemoveFirst();
                    }

                    tbReceived.Lines = tempLines.ToArray();
                }
                try
                {
              //      SendMessage(this.Handle, WM_SETREDRAW, true, 0);
                }
                catch { return; }
                tbReceived.Select(tbReceived.Text.Length, 0);
                tbReceived.ScrollToCaret();
            }
            finally { }
            /****
              *메모리 누수 방지용 코드
              */
            System.GC.Collect(0, GCCollectionMode.Forced);
            System.GC.WaitForFullGCComplete();

        }

        public void Serial_ClassSet(object serial)
        {
            this.serial = (Serial)serial;
        }
        private void Button_Send_Click(object sender, EventArgs e)
        {
            try
            {
                this.serial.Serial_DataSend(Encoding.ASCII.GetBytes(textBox_senddata.Text), textBox_senddata.Text.Length);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Button_Clear_Click(object sender, EventArgs e)
        {
            try
            {
                this.tbReceived.Text = "";
            }
            finally { }
        }
        public void hex_to_ascii_radiobuttonConvert()
        {
            radioButton_hex.Checked = true;
            radioButton_ascii.Checked = false;
        }
        public void ascii_to_hex_radiobuttonConvert()
        {
            radioButton_ascii.Checked = true;
            radioButton_hex.Checked = false;
        }
        public void Debug_Main_Form_Get(object frm)
        {
            this.main_form = (JdasMbic)frm;
        }
    }
}
