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

using Motion6D;

using DiagramUI;

namespace MotionUI
{
   
    public partial class FormRigidFrame : Form, IUpdatableForm
    {
        private IObjectLabel label;
        RigidReferenceFrame frame;
        private TextBox[] coord;
        private FormRigidFrame()
        {
            InitializeComponent();
        }
        public FormRigidFrame(IObjectLabel label)
        {
            InitializeComponent();

            ResourceService.Resources.LoadControlResources(this);
            this.label = label;
            frame = label.Object as RigidReferenceFrame;
            Text = label.Name;
            coord = new TextBox[] { textBoxX, textBoxY, textBoxZ };
            dataGridViewMatrix.DataSource = dataSetMatrix.Tables[0];
            fill();
        }

        #region IUpdatableForm Members

        void IUpdatableForm.UpdateFormUI()
        {
           Text = label.Name;
        }

        #endregion


        void fill()
        {
            double[] p = frame.RelativePosition;
            for (int i = 0; i < coord.Length; i++)
            {
                coord[i].Text = p[i] + "";
            }
            double[,] m = frame.RelativeMatrix;
            fill(m);
        }

        void fill(double[,] m)
        {
            dataSetMatrix.Tables[0].Clear();
            string[] c = new string[] { "X", "Y", "Z" };
            for (int i = 0; i < 3; i++)
            {
                object[] o = new object[] { c[i], m[i, 0], m[i, 1], m[i, 2] };
                dataSetMatrix.Tables[0].Rows.Add(o);
            }
        }

        void norm(bool save)
        {
            double[,] m = new double[3, 3];
            DataTable t = dataSetMatrix.Tables[0];
            for (int i = 0; i < t.Rows.Count; i++)
            {
                DataRow row = t.Rows[i];
                for (int j = 0; j < 3; j++)
                {
                    m[i, j] = (double)row[j + 1];
                }
            }
            Vector3D.V3DOperations.NormMatrix(m);
            fill(m);
            if (save)
            {
                frame.RelativeMatrix = m;
            }
        }


        private void buttonNorm_Click(object sender, EventArgs e)
        {
            try
            {
                norm(false);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void buttonAccept_Click(object sender, EventArgs e)
        {
            try
            {
                norm(true);
                double[] p = frame.RelativePosition;
                for (int i = 0; i < 3; i++)
                {
                    p[i] = Double.Parse(coord[i].Text);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        double[] coordinates
        {
            get
            {
                double[] p = new double[3];
                for (int i = 0; i < 3; i++)
                {
                    p[i] = Double.Parse(coord[i].Text);
                }
                return p;
            }
        }

        private void buttonAlong_Click(object sender, EventArgs e)
        {
            try
            {
                double[] p = coordinates;
                double a = Double.Parse(textBoxRot.Text);
                double[,] m = ReferenceFrame.CalucateViewMatrix(p, a);
                fill(m);
            }
            catch (Exception)
            {
            }
        }
    }
}