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

namespace WindowsApplication1
{

    class devdata
    {
        TextBox tbAddress;
        TextBox tbTXDelay;
        TextBox tbRXDelay;
        TextBox tbRPTRDelay;
        TextBox tbTT;
        TextBox tbST;
        DataGridView dgw;

        logger log;

        // konstruktor, napojeni na prvky formulare
        public devdata(logger alog, TextBox atbAddress, TextBox atbTXDelay, TextBox atbRXDelay, TextBox atbRPTRDelay, TextBox atbTT, TextBox atbST, DataGridView adgw)
        {
            tbAddress = atbAddress;
            tbTXDelay = atbTXDelay;
            tbRXDelay = atbRXDelay;
            tbRPTRDelay = atbRPTRDelay;
            tbTT = atbTT;
            tbST = atbST;
            dgw = adgw;
            log = alog;
        }

        // ulozeni adresy do textboxu
        public void ShowAddress(string aArg)
        {
            if(aArg!=null) tbAddress.Text = aArg.Substring(0,3);
        }

        // ulozeni delayu do textboxu
        public void ShowDelays(int[] aArg)
        {
            if (aArg == null || (aArg.Length != 5)) return;
            tbTXDelay.Text = aArg[0].ToString();
            tbRXDelay.Text = aArg[1].ToString();
            tbRPTRDelay.Text = aArg[2].ToString();
            tbTT.Text = aArg[3].ToString();
            tbST.Text = aArg[4].ToString();

        }

        // ulozeni tabulky kodu to datastrigviewu
        public void ShowCodes(string[] aArg)
        {
            int i;
            if (aArg == null || (aArg.Length != 200)) return;
            for (i = 0; i < 200; i++)
            {
                dgw.Rows[i].Cells[1].Value = aArg[i];
            }
        }

        // export adresy z textboxu s kontrolou a upravou
        public string GetAddress()
        {
            if ((tbAddress.Text == null)||(tbAddress.Text.Length != 0))
            {
                log.add(1, "Address not found or bad length");
                return null;
            }
            tbAddress.Text = tbAddress.Text.Substring(0, 3);
            if (!IsInt(tbAddress.Text))
            {
                log.add(1,"Address must be numeric value");
                return null;
            }
            tbAddress.Text=tbAddress.Text.PadLeft(3, '0');
            return tbAddress.Text;
        }

        // export delayu z textboxu s kontrolou na cislo a interval
        public int[] GetDelays()
        {
            int i;
            if ((!IsInt(tbTXDelay.Text)) || (!IsInt(tbRXDelay.Text)) || (!IsInt(tbRPTRDelay.Text)) || (!IsInt(tbTT.Text)) || (!IsInt(tbST.Text)))
            {
                log.add(1, "Delays must be numeric value");
                return null;
            }
            int[] aRVal = new int[5];
            aRVal[0] = int.Parse(tbTXDelay.Text);
            aRVal[1] = int.Parse(tbRXDelay.Text);
            aRVal[2] = int.Parse(tbRPTRDelay.Text);
            aRVal[3] = int.Parse(tbTT.Text);
            aRVal[4] = int.Parse(tbST.Text);
            for (i = 0; i < 3; i++)
            {
                if (!IsInInterval(aRVal[i], 150, 38250))
                {
                    log.add(1, "Delays out of range");
                    return null;
                }
                else
                {
                    aRVal[i] = (aRVal[i] / 150) * 150;
                    switch (i)
                    {
                        case 0: tbTXDelay.Text = aRVal[i].ToString(); break;
                        case 1: tbRXDelay.Text = aRVal[i].ToString(); break;
                        case 2: tbRPTRDelay.Text = aRVal[i].ToString(); break;
                    }
                }
            }
            for (i = 3; i < 5; i++)
            {
                if (!IsInInterval(aRVal[i], 10, 2550))
                {
                    log.add(1, "Times out of range");
                    return null;
                }
                else
                {
                    aRVal[i] = (aRVal[i] / 10) * 10;
                    switch (i)
                    {
                        case 3: tbTT.Text = aRVal[i].ToString(); break;
                        case 4: tbST.Text = aRVal[i].ToString(); break;
                    }
                }
            }
            return aRVal;
        }

        // export tabulky kodu z textboxu s kontrolou na cislo a interval
        public string[] GetCodes()
        {
            int i;
            string s;
            string[] RetVal = new string[200];
            for (i = 0; i < 200; i++)
            {
                try
                {
                    s = dgw.Rows[i].Cells[1].Value.ToString();
                }
                catch
                {
                    s = null;
                }
                if ((!IsInt(s)) || (s.Length > 4))
                {
                    log.add(1, "Error in code table on line "+(i+1).ToString());
                    return null;
                }
                s = s.PadLeft(4, '0');
                dgw.Rows[i].Cells[1].Value = s;
                RetVal[i] = s;
            }
            return RetVal;
        }

        // otestuje string jestli je v nem integer
        public bool IsInt(string s)
        {
            try
            {
                int.Parse(s);
                return true;
            }
            catch
            {
                return false;
            }
        }

        // otestuje int jestli je v danem intervalu
        public bool IsInInterval(int aValue, int aMin, int aMax)
        {
            if ((aValue >= aMin) && (aValue <= aMax)) return true;
            return false;
        }



    }
}
