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 CafeNet_Timer
{
    public partial class TimerCP : UserControl
    {
        public TimerCP()
        {
            InitializeComponent();

            btnReset.Enabled = false;
            BtnStop.Enabled = false ;
    
        }

       int timePrice = 0;
    

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar)))
                e.Handled = true;    
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
            btnReset.Enabled = false;
            BtnStop.Enabled = true;

            Mytimer.Enabled = true;
            ActiveControl = null;
        }

        private void BtnStop_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = true;
            btnReset.Enabled = true;
            BtnStop.Enabled = false;

            Mytimer.Enabled = false;
        }

        
        private void btnReset_Click(object sender, EventArgs e)
        {
            btnReset.Enabled = false;

            time.Text = "00:00:00";
           timePrice = 0;
           lblPrice.Text = "0 تومان";
           ActiveControl = null;
        }

        private void Mytimer_Tick(object sender, EventArgs e)
        {
            Check_Seconds();
            timePrice += 1;
            CalcPrice();
            
        }

        void Check_Seconds()
        {
            string sec, min;
            sec = time.Text.Substring(6, 2);
            min = time.Text.Substring(3, 2);
            if (Convert.ToInt32(sec) < 59)
                if (Convert.ToInt32(sec) < 9)
                    sec = "0" + Convert.ToString(Convert.ToInt32(sec) + 1);
                else
                    sec = Convert.ToString(Convert.ToInt32(sec) + 1);
            else
            {
                sec = "00";
                Check_Minutes();
            }
            time.Text = time.Text.Substring(0, 6) + sec;
        }

        void Check_Minutes()
        {
            string min, hour;
            min = time.Text.Substring(3, 2);
            hour = time.Text.Substring(0, 2);
            if (Convert.ToInt32(min) < 59)
                if (Convert.ToInt32(min) < 9)
                    min = "0" + Convert.ToString(Convert.ToInt32(min) + 1);
                else
                    min = Convert.ToString(Convert.ToInt32(min) + 1);
            else
            {
                min = "00";
                if (Convert.ToInt32(hour) < 23)
                    if (Convert.ToInt32(hour) < 9)
                        hour = "0" + Convert.ToString(Convert.ToInt32(hour) + 1);
                    else
                        hour = Convert.ToString(Convert.ToInt32(hour) + 1);
                else
                    hour = "00";
            }
            time.Text = hour + ":" + min + time.Text.Substring(5, 3);
        }

        void CalcPrice()
        {
            int PerPrice; Int32.TryParse(txtPerPrice.Text, out PerPrice);
            double AllMinTime = timePrice / 60;
            double CalcedFinalPrice = Math.Round((PerPrice * AllMinTime) / 60); 
            lblPrice.Text = CalcedFinalPrice.ToString() + " تومان";


        }

        private void txtPerPrice_TextChanged(object sender, EventArgs e)
        {
            CalcPrice();
        }


    }
}
