using System;
using System.Windows.Forms;

namespace Calculate
{
    public partial class fCalc : Form
    {

        public fCalc()
        {
            InitializeComponent();
        }


        bool isNewOperation = true;
        int operationType = -1;
        double firstVar = 0, secondVar = 0, result = 0;

        private void fCalc_LocationChanged(object sender, EventArgs e)
        {

        }

        private void fCalc_Load(object sender, EventArgs e)
        {

        }

        private void textBoxMain_Calc_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ((char)Keys.Back) || e.KeyChar == '.' || e.KeyChar == ',' || e.KeyChar >= '0' && e.KeyChar <= '9')
            {
                if (isNewOperation)
                {
                    textBoxMain.Text = "";
                    isNewOperation = false;
                }
                if (e.KeyChar == '.') e.KeyChar = ',';
                if (e.KeyChar == ',' && textBoxMain.Text.IndexOf(',') > -1) e.Handled = true;
                if (e.KeyChar == '0' && textBoxMain.Text == "0") e.Handled = true;
                if (textBoxMain.Text == "0" && e.KeyChar >= '1' && e.KeyChar <= '9') textBoxMain.Text = "";
            }
            else e.Handled = true;
        }


        private void buttonNumeric_Click(object sender, EventArgs e)
        {
            if (isNewOperation == true || textBoxMain.Text == "0")
            {
                textBoxMain.Text = (sender as Button).Text;
                isNewOperation = false;
            }
            else textBoxMain.Text += (sender as Button).Text;
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            operationType = -1;
            textBoxMain.Text = "0";
            isNewOperation = true;
        }

        private void buttonPoint_Click(object sender, EventArgs e)
        {
            if (isNewOperation)
            {
                textBoxMain.Text = "0";
                isNewOperation = false;
            }
            if (textBoxMain.Text.IndexOf(",") == -1) textBoxMain.Text += ',';

        }

        private void buttonResult_Click(object sender, EventArgs e)
        {
            secondVar = Convert.ToDouble(textBoxMain.Text);
            switch (operationType)
            {
                case 1:
                    result = firstVar + secondVar;
                    break;
                case 2:
                    result = firstVar - secondVar;
                    break;
                case 3:
                    result = firstVar * secondVar;
                    break;
                case 4:
                    result = firstVar / secondVar;
                    break;
                case 5:
                    result = Math.Pow(firstVar, secondVar);
                    break;
            }
            textBoxMain.Text = Convert.ToString(result);
            isNewOperation = true;
            operationType = -1;
        }

        private void buttonPlus_Click(object sender, EventArgs e)
        {
            if (textBoxMain.Text == "")
            {
                textBoxMain.Text = "0";
                return;
            }

            if (operationType > 0) buttonResult_Click(sender, e);
            firstVar = Convert.ToDouble(textBoxMain.Text);
            isNewOperation = true;
            operationType = 1;

        }

        private void buttonMinus_Click(object sender, EventArgs e)
        {
            if (textBoxMain.Text == "" || textBoxMain.Text == "0")
            {
                textBoxMain.Text = "-";
                isNewOperation = false;
                operationType = -1;
                return;
            }

            if (!(textBoxMain.Text.IndexOf("-") > -1 && textBoxMain.Text.Length - 1 == textBoxMain.Text.IndexOf("-")))
            {
                if (operationType > 0) buttonResult_Click(sender, e);
                firstVar = Convert.ToDouble(textBoxMain.Text);
                isNewOperation = true;
                operationType = 2;
            }

        }

        private void buttonMultiply_Click(object sender, EventArgs e)
        {
            if (textBoxMain.Text == "")
            {
                textBoxMain.Text = "0";
                return;
            }

            if (operationType > 0) buttonResult_Click(sender, e);
            firstVar = Convert.ToDouble(textBoxMain.Text);
            isNewOperation = true;
            operationType = 3;

        }

        private void bDelenie_Click(object sender, EventArgs e)
        {
            if (textBoxMain.Text == "")
            {
                textBoxMain.Text = "0";
                return;
            }

            if (operationType > 0) buttonResult_Click(sender, e);
            firstVar = Convert.ToDouble(textBoxMain.Text);
            isNewOperation = true;
            operationType = 4;
        }

        private void buttonSin_Click(object sender, EventArgs e)
        {
            if (textBoxMain.Text == "") textBoxMain.Text = "0";
            textBoxMain.Text = Math.Sin(Convert.ToDouble(textBoxMain.Text)).ToString();
            isNewOperation = true;
            operationType = -1;
        }

        private void bCos_Click(object sender, EventArgs e)
        {
            if (textBoxMain.Text == "") textBoxMain.Text = "0";
            textBoxMain.Text = Math.Cos(Convert.ToDouble(textBoxMain.Text)).ToString();
            isNewOperation = true;
            operationType = -1;
        }

        private void bTg_Click(object sender, EventArgs e)
        {
            if (textBoxMain.Text == "") textBoxMain.Text = "0";
            textBoxMain.Text = Math.Tan(Convert.ToDouble(textBoxMain.Text)).ToString();
            isNewOperation = true;
            operationType = -1;
        }

        private void bCtg_Click(object sender, EventArgs e)
        {
            if (textBoxMain.Text == "") textBoxMain.Text = "0";
            textBoxMain.Text = (1 / Math.Tan(Convert.ToDouble(textBoxMain.Text))).ToString();
            isNewOperation = true;
            operationType = -1;
        }

        private void bLn_Click(object sender, EventArgs e)
        {
            if (textBoxMain.Text == "") textBoxMain.Text = "0";
            textBoxMain.Text = Math.Log(Convert.ToDouble(textBoxMain.Text)).ToString();
            isNewOperation = true;
            operationType = -1;
        }

        private void bExp_Click(object sender, EventArgs e)
        {
            if (textBoxMain.Text == "") textBoxMain.Text = "0";
            textBoxMain.Text = Math.Exp(Convert.ToDouble(textBoxMain.Text)).ToString();
            isNewOperation = true;
            operationType = -1;
        }

        private void bSqrt_Click(object sender, EventArgs e)
        {
            if (textBoxMain.Text == "") textBoxMain.Text = "0";
            textBoxMain.Text = Math.Sqrt(Convert.ToDouble(textBoxMain.Text)).ToString();
            isNewOperation = true;
            operationType = -1;
        }

        private void textBoxMain_TextChanged(object sender, EventArgs e)
        {

        }

        private void buttonPowXY_Click(object sender, EventArgs e)
        {
            if (textBoxMain.Text == "") textBoxMain.Text = "0";
            else
            {
                if (operationType > 0)
                    buttonResult_Click(sender, e);
                firstVar = Convert.ToDouble(textBoxMain.Text);
                isNewOperation = true;
                operationType = 5;
            }
        }

        private void buttonFactorial_click(object sender, EventArgs e)
        {
            if (textBoxMain.Text == "") textBoxMain.Text = "0";
            try
            {
                int a = 1;
                for (int i = 2; i <= Convert.ToInt32(textBoxMain.Text); i++) a *= i;

                textBoxMain.Text = a.ToString();
                isNewOperation = true;
                operationType = -1;
            }
            catch
            {
                MessageBox.Show("Введите целое число.", "Ошибка!");
                return;
            }
        }


    }
}
