using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VisimBox.Scene;
using VisiMath;

namespace VisimBuilder
{
    public partial class ObjectProperties : UserControl
    {
        Node myNode;
        MainBuilderForm Boss;

        public ObjectProperties()
        {
            InitializeComponent();
        }

        public ObjectProperties(Node node)
            : this()
        {
            ChangeNode(node);
        }

        public void ChangeNode(Node node)
        {
            myNode = node;
            UpdateFrom();
        }

        public void Init(MainBuilderForm boss)
        {
            Boss = boss;
            Boss.SelectedNodeChanged += new MainBuilderForm.NodeChangedCallback(Boss_SelectedNodeChanged);
        }


        void Boss_SelectedNodeChanged()
        {
            ChangeNode(Boss.SelectedNode);
        }

        public void UpdateFrom()
        {
            if (myNode == null)
            {
                buttonAddTrans.Enabled = false;
                buttonEdit.Enabled = false;
                propertyGrid1.SelectedObject = null;
                buttonParent.Enabled = false;
                return;
            }

            string nodetypename = myNode.GetType().Name;

            objectTypeLabel.Text = "Object type: " + nodetypename;

            textBoxName.Text = myNode.Name;
            if (myNode.Trans == null)
            {
                buttonAddTrans.Enabled = true;
            }
            else
                buttonAddTrans.Enabled = false;

            if (nodetypename == "LightNode")
            {
                buttonEdit.Enabled = true;
            }
            else
            {
                buttonEdit.Enabled = false;
            }

            if (myNode.Parent != null)
                buttonParent.Enabled = true;
            else
                buttonParent.Enabled = false;

            propertyGrid1.SelectedObject = myNode;

            comboBoxChildren.Enabled = true;
            comboBoxChildren.Items.Clear();
            foreach (Node node in myNode)
            {
                comboBoxChildren.Items.Add(node);
            }

            if (comboBoxChildren.Items.Count == 0)
                comboBoxChildren.Enabled = false;

        }

        public void UpdateTo()
        {
            VisibilityChanged(null, null);
        }

        private void VisibilityChanged(object sender, EventArgs e)
        {
            if (myNode == null)
                return;
            //myNode.Visible = checkBoxVisible.Checked;
        }


        private TransTypePicker typepicker = new TransTypePicker();

        private void buttonAddTrans_Click(object sender, EventArgs e)
        {
            //sanity check
            if (myNode == null)
                return;
            if (myNode.Trans != null)
                return;

            if (typepicker.ShowDialog() == DialogResult.Cancel)
                return;

            ITransformation itrans;

            switch (typepicker.TransformationType)
            {
                case TransType.Movement:
                    itrans = new MoveTransformation();
                    break;
                case TransType.Rotable:
                    itrans = new QTransformation();
                    break;
                default:
                    itrans = new Transformation();
                    break;
            }


            myNode.Trans = itrans;
            UpdateFrom();
        }


        LightEditDlg ldlg;

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            string typename = myNode.GetType().Name;

            switch (typename)
            {
                case "LightNode":
                    LightNode lnode = myNode as LightNode;
                    if (lnode == null)
                        return;
                    if (ldlg == null)
                        ldlg = new LightEditDlg();
                    ldlg.lightEdit1.AttachedLight = lnode.LightObject;
                    ldlg.ShowDialog();
                    ldlg.lightEdit1.AttachedLight = null;
                    break;
            }

        }

        private void buttonParent_Click(object sender, EventArgs e)
        {
            if ((myNode == null) || (myNode.Parent == null))
                return;
            if (Boss != null)
                Boss.SelectedNode = myNode.Parent;
            else ChangeNode(myNode.Parent);
        }

        private void ComboChanged(object sender, EventArgs e)
        {
            Node node = comboBoxChildren.SelectedItem as Node;

            if (node == null)
                return;

            if (Boss != null)
                Boss.SelectedNode = node;
            else ChangeNode(node);
        }

        private void NameFocusLeave(object sender, EventArgs e)
        {
            RenameNode();
        }

        private void NameKeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                RenameNode();
            }
        }

        void RenameNode()
        {
            if (myNode == null)
                return;
            if (myNode.Name == textBoxName.Text)
                return;
            if ((myNode.Name == null) && (textBoxName.Text == ""))
                return;

            if (MessageBox.Show("Rename node ' " + myNode.Name + " ' to ' " + textBoxName.Text + " '?", "Node renaming", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                myNode.Rename(textBoxName.Text);
            }
            textBoxName.Text = myNode.Name;
                
        }

        private void buttonLookAt_Click(object sender, EventArgs e)
        {
            if (myNode == null)
                return;
            Boss.LookAtObject(myNode);
        }

        private void buttonteleport_Click(object sender, EventArgs e)
        {
            if (myNode == null)
                return;
            Boss.TeleportToObject(myNode);
        }

        private void buttoncopy_Click(object sender, EventArgs e)
        {
            if (myNode == null)
                return;
            Boss.CreateNodeCopy(myNode);
        }
    }
}
