using System;
using System.Windows.Forms;

using Spark3D.Algebra3D;
using Spark3D.Objects3D;
using Spark3D.Objects3D.Shapes;
using Spark3D.Projector3D;

namespace Spark3D
{
    public partial class MainWnd : Form
    {
        /// <summary>
        /// Main Window Constructor
        /// </summary>
        /// 
        public MainWnd()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Normal;
            IObject3D o3D = new Sphere(1.0, 4);
            this.View.Insert(new PerspectiveProjector());
            this.View.Insert(o3D);
            this.View.Insert(new LightSources.PointSource(new Vector3D(1.0,1.0,1.0)));
            SetCameraAngle();
            SetCameraDistance();
            SetCameraRotationRate();
        }

        // Camera angle distance and field of view handlers.
        private void tbThetaX_Scroll(object sender, EventArgs e)
        {
            SetCameraAngle();
        }

        private void tbThetaY_Scroll(object sender, EventArgs e)
        {
            SetCameraAngle();
        }

        private void tbThetaZ_Scroll(object sender, EventArgs e)
        {
            SetCameraAngle();
        }

        private void tbFieldOfView_Scroll(object sender, EventArgs e)
        {
            View.FieldOfView=(double)this.tbFieldOfView.Value/(double)this.tbFieldOfView.Maximum;
        }

        private void tbCameraDistance_Scroll(object sender, EventArgs e)
        {
            SetCameraDistance();
        }

        private void SetCameraDistance()
        {
            View.CameraDistance = (double)(this.tbCameraDistance.Value)/2.0;
        }

        private void SetCameraAngle()
        {

            View.CameraAngle = new Vector3D
                (
                tbThetaX.Value,
                tbThetaY.Value,
                tbThetaZ.Value
                );
        }
        
        // Camera rotation rate handlers.
        private void tbThetaXDelta_Scroll(object sender, EventArgs e)
        {
            SetCameraRotationRate();
        }
       
        private void tbThetaYDelta_Scroll(object sender, EventArgs e)
        {
            SetCameraRotationRate();
        }

        private void tbThetaZDelta_Scroll(object sender, EventArgs e)
        {
            SetCameraRotationRate();
        }

        private void SetCameraRotationRate()
        {
            View.CameraAngleDeltas = new Vector3D
                (
                tbThetaXDelta.Value,
                tbThetaYDelta.Value,
                tbThetaZDelta.Value
                );
        }

        //  Animation stop and start.
        private void btnGo_Click(object sender, EventArgs e)
        {
            View.Animate();
            View.CameraAngle = new Vector3D
            (
                tbThetaX.Value,
                tbThetaY.Value,
                tbThetaZ.Value
            );
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            View.Freeze();
            Vector3D Angles = View.CameraAngle;
            tbThetaX.Value = (int)Angles[0]%360;
            tbThetaY.Value = (int)Angles[1]%360;
            tbThetaZ.Value = (int)Angles[2]%360;
        }

   

    }
}