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 VisiMath.Geometry;
using VisiMath;
using VisimBox.Scene;
using VisimBox;
using VisimBox.Renderer;

namespace VisimBuilder.ObjectCreation
{
    public partial class ObjectCreationBase : UserControl
    {
        protected Transformation trans;

        public MainBuilderForm Boss;

        protected MainBuilderForm.NodeChangedCallback NodeCallback;

        bool _DisableScale;
        public bool DisableScale
        {
            get { return _DisableScale; }
            set
            {
                _DisableScale = value;
                if (_DisableScale)
                {
                    checkBoxScale.Enabled = false;
                    checkBoxScale.Checked = false;
                }
                else
                    checkBoxScale.Enabled = true;
            }

        }

        bool _DisableRotation;
        public bool DisableRotation
        {
            get { return _DisableRotation; }
            set
            {
                _DisableRotation = value;
                if (_DisableRotation)
                {
                    checkBoxRot.Enabled = false;
                    checkBoxRot.Checked = false;
                }
                else
                    checkBoxRot.Enabled = true;
            }
        }

        public ObjectCreationBase()
        {
            InitializeComponent();
            trans = new Transformation();
            this.transformationControl1.ChangeTrans(trans);
        }
        

        public virtual void Init(MainBuilderForm boss)
        {
            //stupid c# GUI designer;
            Boss = boss;
            NodeCallback = new MainBuilderForm.NodeChangedCallback(Boss_SelectedNodeChanged);

            Boss.SelectedNodeChanged += NodeCallback;
        }

        protected virtual void Boss_SelectedNodeChanged()
        {
            if (Boss.SelectedNode != null)
            {
                labelSelected.Text = Boss.SelectedNode.Name;
                checkBoxToSelected.Enabled = true;
            }
            else
            {
                labelSelected.Text = "";
                checkBoxToSelected.Enabled = false;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (DesignMode)
                return;

            Boss_SelectedNodeChanged();
        }

        public virtual Node ObjectCreation(string name, ITransformation itrans, Node owner)
        {
            //do nothing
            return null;
        }

        public virtual void CreateObject()
        {
            CreateObject(null, new EventArgs());
        }

        public virtual void CreateObject(object sender, EventArgs e)
        {
            #region Transformation

            ITransformation finaltrans = null;

            if (checkBoxDoTrans.Checked)
            {
                if (checkBoxScale.Checked)
                {
                    //note, block rotations if rotation not allowed???
                    finaltrans = new Transformation();
                }
                else if (checkBoxRot.Checked)
                {
                    finaltrans = new QTransformation();
                }
                else
                    finaltrans = new MoveTransformation();

                finaltrans.Position = trans.Position;
                if (checkBoxScale.Checked)
                    finaltrans.Scale = trans.Scale;
                if (checkBoxRot.Checked)
                    finaltrans.RPY = trans.RPY;
            }

            #endregion

            #region Owner

            Node owner = null;

            if (checkBoxToRoot.Checked || (Boss.SelectedNode == null))
            {
                owner = VisimEngine.Instance.MainScene;
            }
            else
            {
                owner = Boss.SelectedNode;
            }

            #endregion


            Node node = ObjectCreation(textBoxName.Text, finaltrans, owner);

            VisimBox.Console.WriteLine("New object " + node.XMLNodeName + " " + textBoxName.Text + "created.");

            Boss.UpdateSceneChange();
            Boss.SelectNode(node, false);
        }



        #region Node hierarchy

        private void nodeOwnerChoose(object sender, EventArgs e)
        {
            if (Boss.ShowSceneBrowser() == DialogResult.OK)
                checkBoxToSelected.Checked = true;
        }

        private void SceneRootChanged(object sender, EventArgs e)
        {
            if (checkBoxToRoot.Checked)
                checkBoxToSelected.Checked = false;
        }

        private void ToSelectedChanged(object sender, EventArgs e)
        {
            if (checkBoxToSelected.Checked)
                checkBoxToRoot.Checked = false;
        }

        #endregion


        #region Transformation

        private void UpdateTransformationChecks()
        {
            if (!checkBoxDoTrans.Checked)
            {
                transformationControl1.textBoxPos.ReadOnly = true;
                transformationControl1.textBoxRot.ReadOnly = true;
                transformationControl1.textBoxScale.ReadOnly = true;
            }
            else
            {
                transformationControl1.textBoxPos.ReadOnly = false;

                transformationControl1.textBoxRot.ReadOnly = !checkBoxRot.Checked;
                transformationControl1.textBoxScale.ReadOnly = !checkBoxScale.Checked;
            }
        }

        private void TransMoveCheck(object sender, EventArgs e)
        {
            if (!checkBoxDoTrans.Checked)
            {
                checkBoxRot.CheckState = CheckState.Indeterminate;
                checkBoxScale.CheckState = CheckState.Indeterminate;
            }
            UpdateTransformationChecks();
        }

        private void TransRotCheck(object sender, EventArgs e)
        {
            if (checkBoxRot.Checked)
            {
                if (!checkBoxDoTrans.Checked)
                {
                    checkBoxDoTrans.Checked = true;
                }
            }
            UpdateTransformationChecks();
        }

        private void TransScaleCheck(object sender, EventArgs e)
        {
            if (checkBoxScale.Checked)
            {
                if (!checkBoxDoTrans.Checked)
                {
                    checkBoxDoTrans.Checked = true;
                }
            }
            UpdateTransformationChecks();
        }

        #endregion


        public virtual void SetDefault()
        {
            textBoxName.Text = "New object";

            checkBoxDoTrans.Checked = true;
            checkBoxRot.Checked = true;
            checkBoxScale.Checked = true;

            checkBoxToRoot.Checked = true;
            checkBoxToSelected.Checked = false;
        }

        private void buttonDefault_Click(object sender, EventArgs e)
        {
            SetDefault();
        }
    }
}
