using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Ladybug_TestHarness
{
    public partial class RgrTst_SetGetResponse : Form
    {
        const int COLL_WIDTH = 12;

        public RgrTst_SetGetResponse()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string newLine = udFreq.Value.ToString().PadLeft(COLL_WIDTH) + udCorr.Value.ToString().PadLeft(COLL_WIDTH);
            lstResponse.Items.Add(newLine);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lstResponse.SelectedIndex >= 0)
            {
                lstResponse.Items.RemoveAt(lstResponse.SelectedIndex);
            }
        }

        private LB_API2_Declarations.ResponsePoints[] m_rsp;
        public int numPts = 0;
        internal LB_API2_Declarations.ResponsePoints[] Response
        {
            get
            {
                numPts = 0;
                m_rsp = new LB_API2_Declarations.ResponsePoints[LB_API2_Declarations.MAX_NUM_RESP_PTS];
                if (lstResponse.Items.Count > 0)
                {
                    for (int i = 0; i < lstResponse.Items.Count; i++)
                    {
                        m_rsp[i].Frequency = Convert.ToDouble(lstResponse.Items[i].ToString().Substring(0, COLL_WIDTH).Trim()) * 1e6;
                        m_rsp[i].Amplitude = Convert.ToDouble(lstResponse.Items[i].ToString().Substring(COLL_WIDTH+1).Trim());
                        numPts++;
                    }
                }
                return m_rsp;
            }
            set
            {
                m_rsp = value;
                lstResponse.Items.Clear();
                for (int i = m_rsp.GetLowerBound(0); i < m_rsp.GetUpperBound(0); i++)
                {
                    if (m_rsp[i].Frequency > 0)
                    {
                        if (i > 0)
                        {
                            if (m_rsp[i].Frequency > m_rsp[i - 1].Frequency)
                            {
                                string newLine = (m_rsp[i].Frequency / 1e6).ToString().PadLeft(COLL_WIDTH) + m_rsp[i].Amplitude.ToString().PadLeft(COLL_WIDTH);
                                lstResponse.Items.Add(newLine);
                            }
                            else break;
                        }
                        else
                        {
                            string newLine = (m_rsp[i].Frequency / 1e6).ToString().PadLeft(COLL_WIDTH) + m_rsp[i].Amplitude.ToString().PadLeft(COLL_WIDTH);
                            lstResponse.Items.Add(newLine);
                        }
                    }
                    else break;
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}