using System;
using System.Windows.Forms;

namespace MainTemplate
{
    public partial class frmCalculator : Form
    {
        private bool isFirst = true;
        private bool shouldClear = true;
        private double firstOperand;
        private double secondOperand;
        private double result = 0;
        private string symbol = String.Empty;

        public frmCalculator()
        {
            InitializeComponent();
        }

        private void numberButton_Click(object sender, EventArgs e)
        {
            Button sourceButton = (sender as Button);
            double oldNumber, buttonNumber, newNumber;

            if (shouldClear)
            {
                outputTextBox.Clear();
                oldNumber = 0;
                shouldClear = false;
            }
            else
            {
                oldNumber = Double.Parse(outputTextBox.Text);
            }

            
            buttonNumber = Double.Parse(sourceButton.Text);
            newNumber = (oldNumber * 10) + buttonNumber;

            if (isFirst)
            {
                firstOperand = newNumber;
            }
            else
            {
                secondOperand = newNumber;
            }

            outputTextBox.Text += sourceButton.Text;

            Calculate(symbol);
        }

        private void operatorButton_Click(object sender, EventArgs e)
        {
            firstOperand = result;

            Button sourceButton = (sender as Button);
            string operatorSymbol = sourceButton.Text;

            if (isFirst)
                isFirst = false;

            shouldClear = true;
            symbol = operatorSymbol;
            outputTextBox.Text = result.ToString();
        }

        private void Calculate(string operatorSymbol)
        {
            if (isFirst)
            {
                result = firstOperand;
                return;
            }

            switch (operatorSymbol)
            {
                case "+":
                    result = firstOperand + secondOperand;
                    break;
                case "-":
                    result = firstOperand - secondOperand;
                    break;
                case "*":
                    result = firstOperand * secondOperand;
                    break;
                case "/":
                    result = firstOperand / secondOperand;
                    break;
            }
        }

        private void equalsButton_Click(object sender, EventArgs e)
        {
            outputTextBox.Text = result.ToString();
            isFirst = true;
            shouldClear = true;
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            outputTextBox.Text = "0";
            result = 0;
            isFirst = true;
            shouldClear = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " is Opened.");
        }

        private void frmCalculator_FormClosed(object sender, FormClosedEventArgs e)
        {
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " is Closed.");
        }
    }
}
