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;

namespace VisimBuilder
{

    public partial class PhysControl : UserControl
    {
        public PhysControl()
        {
            InitializeComponent();
            numericUpDownStepTime.Minimum = 0.0001M;
            numericUpDownStepTime.Value = 0.05M;
            numericUpDownStepTime.Increment = 0.05M;
            numericUpDownGravity.Value = 9.81M;
            numericUpDownGravity.Increment = 0.01M;

            buttonStop.Enabled = false;
        }

        protected bool _running = false;

        public bool Running { 
            get
            {
                return _running;
            }
            set
            {
                _running = value;
                if (_running)
                {
                    buttonStop.Enabled = true;
                    buttonStart.Enabled = false;
                    buttonStep.Enabled = false;
                }
                else
                {
                    buttonStop.Enabled = false;
                    buttonStart.Enabled = true;
                    buttonStep.Enabled = true;
                }
            } 
        }

        public float SimulationTime
        {
            get
            {
                float rval;

                if (float.TryParse(textBoxSimTime.Text, out rval))
                    return rval;
                else return 0;
            }
            set
            {
                textBoxSimTime.Text = value.ToString();
            }
        }

        public float StepTimeSize
        {
            get
            {
                return (float)numericUpDownStepTime.Value;
            }
            set
            {
                float val = value;
                VisiMath.MathTools.Clamp(ref val, 0.001f, 1);
                numericUpDownStepTime.Value = (decimal)val;
            }
        }

        public delegate void PhysControlDelegate();
        public delegate void PhysControlDelegateValue(float stepsize);

        public event PhysControlDelegate SimulationStarted;
        public event PhysControlDelegate SimulationStopped;
        public event PhysControlDelegateValue SimulationStep;
        public event PhysControlDelegate SimulationReset;
        public event PhysControlDelegateValue GravityChange;

        private void button4_Click(object sender, EventArgs e)
        {
            //Reset simulation to zero
            textBoxSimTime.Text = "0";
            if (SimulationReset != null)
                SimulationReset();
        }

        private void StepTimeChanged(object sender, EventArgs e)
        {
            //do nothing?
        }

        private void GravityChanged(object sender, EventArgs e)
        {
            if (GravityChange != null)
            {
                float val = (float)numericUpDownGravity.Value;
                GravityChange(val);
            }
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            Running = true;
            if (SimulationStarted != null)
                SimulationStarted();
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            Running = false;
            if (SimulationStopped != null)
                SimulationStopped();
        }

        private void buttonStep_Click(object sender, EventArgs e)
        {
            if (SimulationStep != null)
                SimulationStep(StepTimeSize);
        }
    }
}
