﻿namespace CalcForm
{
	public enum StateType
	{
		Initial,
		InputDigit,
		Compute,
		Equal,
	};

	public enum EventType
	{
		Digit,
		Operator,
		Equal,
		Clear,
	}

	public partial class Calculator
	{
		StateMachine.StateMachine<StateType, EventType> fsm;

		void InitializeState()
		{
			this.fsm = new StateMachine.StateMachine<StateType, EventType>(StateType.Initial);

			this.fsm.RegisterTransition(
				StateType.Initial, EventType.Digit,
				StateType.InputDigit,
				delegate(object x) { dspValue = (double)x; }
				);
			this.fsm.RegisterTransition(
				StateType.Initial, EventType.Operator,
				StateType.Compute,
				delegate(object x)
				{
					CalKey();
					op = (Operator)x;
				});

			this.fsm.RegisterTransition(
				StateType.InputDigit, EventType.Digit,
				StateType.InputDigit,
				delegate(object x)
				{
					dspValue = dspValue * 10 + (double)x;
				});
			this.fsm.RegisterTransition(
				StateType.InputDigit, EventType.Operator,
				StateType.Compute,
				delegate(object x)
				{
					CalKey();
					op = (Operator)x;
				});
			this.fsm.RegisterTransition(
				StateType.InputDigit, EventType.Equal,
				StateType.Equal,
				delegate(object x)
				{
					CalKey();
				});
			this.fsm.RegisterTransition(
				StateType.InputDigit, EventType.Clear,
				StateType.Initial, delegate(object x) { this.Init(); });

			this.fsm.RegisterTransition(
				StateType.Compute, EventType.Digit,
				StateType.InputDigit,
				delegate(object x)
				{
					memValue = dspValue;
					dspValue = (double)x;
				});
			this.fsm.RegisterTransition(
				StateType.Compute, EventType.Operator,
				StateType.Compute,
				delegate(object x)
				{
					op = (Operator)x;
				});
			this.fsm.RegisterTransition(
				StateType.Compute, EventType.Equal,
				StateType.Equal, delegate(object x) { });
			this.fsm.RegisterTransition(
				StateType.Compute, EventType.Clear,
				StateType.Initial, delegate(object x) { this.Init(); });

			this.fsm.RegisterTransition(
				StateType.Equal, EventType.Digit,
				StateType.InputDigit,
				delegate(object x)
				{
					memValue = 0d;
					dspValue = (double)x;
					op = Operator.None;
				});
			this.fsm.RegisterTransition(
				StateType.Equal, EventType.Operator,
				StateType.Compute,
				delegate(object x)
				{
					op = (Operator)x;
				});
			this.fsm.RegisterTransition(
				StateType.Equal, EventType.Equal,
				StateType.Equal,
				delegate(object x)
				{
					CalKey();
				});
			this.fsm.RegisterTransition(
				StateType.Equal, EventType.Clear,
				StateType.Initial, delegate(object x) { this.Init(); });
		}
	}
}
