using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace RogerMfReaderExample
{
    class InfoBuffer
    {
        
        //error 
        internal static void error(String Message)
        {
            MessageBox.Show(Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        //warning
        internal static void warning(String Message)
        {
            MessageBox.Show(Message, "warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        //info
        internal static void info(String Message)
        {
            MessageBox.Show(Message, "information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        
        //message Title
        internal static void messageTitle(String Title)
        {
            
            Font titleFont = new Font(FontFamily.GenericSansSerif.Name, 9.0F, FontStyle.Bold);
            String str = "<<<  - " + Title.ToUpper() + " -  >>>";
            message(str, titleFont, Color.Blue);
                    
        }

        //message Data
        internal static void messageData(String SubTitle, String Data)
        {

            Font subTitleFont = new Font(FontFamily.GenericSansSerif.Name, 9.0F, FontStyle.Regular);
            Font DataFont = new Font(FontFamily.GenericSansSerif.Name, 9.0F, FontStyle.Regular);

            message(SubTitle, subTitleFont, Color.Gray);

            String[] DataStr = Data.Split(Environment.NewLine.ToCharArray(),StringSplitOptions.RemoveEmptyEntries);

            foreach (String str in DataStr)
                message(str.Insert(0, "\t"), DataFont, Color.Green);
            
        }

        private static void message(string msg, Font msgFont, Color msgColor)
        {
            Form1 frm = (Form1)Application.OpenForms["Form1"];
            frm.messageTextBox_AddText(msg, msgFont, msgColor);
        }

    }


    //Creat data and show in textBox
    class ConsolBox
    {
        class Data
        {
            private String Title;
            public List<String> Value = new List<String>();

            public Data(String name)
            {
                Title = name;
            }

            public String GetTitle()
            {
                return Title;
            }

            public void Clear()
            {
                Value.Clear();
            }

            public void AddData(String Title, Object DataValue)
            {

                if (DataValue.GetType().IsArray)        //Only Byte
                {
                    string arrayStr = String.Empty;

                    Byte[] other = (Byte[])DataValue;

                    foreach (Byte element in other)
                        arrayStr += "0x" + element.ToString("X2") + ", ";

                    Value.Add(Title.ToUpper() + ": " + arrayStr);
                }
                else if (DataValue.GetType().IsValueType)
                {

                    Byte other = (Byte)DataValue;
                    Value.Add(Title.ToUpper() + ": " + "0x" + other.ToString("X2"));

                }
                else
                    Value.Add(Title.ToUpper() + ": " + DataValue.ToString());

            }

            public void AddReturn(String Return)
            {
                Title += " - Return: " + Return;
            }

        };

        private String Title;
        private Data Terminal = new Data("Terminal");
        private Data Host = new Data("Host");

        public ConsolBox(string name)
        {
            Title = name;

        }

        //Set Function Name
        public String FunctionName
        {
            set
            {

                Title = value;

                Terminal.Clear();
                Host.Clear();

            }
        }

        //Set Function Return
        public String FunctionReturn
        {
            set
            {
                Title += "Return - " + value;

            }
        }


        //Set Terminal Return
        public String TerminalReturn
        {
            set
            {
                Terminal.AddReturn(value);

            }
        }

        //Set Terminal Return
        public String HostReturn
        {
            set
            {
                Host.AddReturn(value);

            }
        }

        //Add Data 
        public void AddTreminalData(String DataTitle, Object Value)
        {
            Terminal.AddData(DataTitle, Value);
        }

        public void AddHostData(String DataTitle, Object Value)
        {
            Host.AddData(DataTitle, Value);
        }

        //Show 
        public void Show()
        {

            InfoBuffer.messageTitle(Title);

            String HostDataString = String.Empty;

            foreach (String str in Host.Value)
                HostDataString += str + Environment.NewLine;

            InfoBuffer.messageData(Host.GetTitle(), HostDataString);

            String TerminalDataString = String.Empty;

            foreach (String str in Terminal.Value)
                TerminalDataString += str + Environment.NewLine;

            InfoBuffer.messageData(Terminal.GetTitle(), TerminalDataString);



        }
    }

}
