using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RiskAssessment;

namespace Calculator
{
    public partial class Form1 : Form
    {
        private Stopwatch _stopwatch = new Stopwatch();

        public Form1()
        {
            InitializeComponent();
        }

        
        private void timer1_Tick(object sender, EventArgs e) {
            if(progressBar1.Value == progressBar1.Maximum) {
                progressBar1.Value = 0;
            }
            progressBar1.Increment(1);
        }

        private void Form1_Load(object sender, EventArgs e) {
            progressBar1.Visible = false;
        }

        protected string[] CreditAgencies {
            get {
                var list = new List<string>();
                if(chk1.Checked) list.Add(chk1.Text);
                if(chk2.Checked) list.Add(chk2.Text);
                if(chk3.Checked) list.Add(chk3.Text);
                return list.ToArray();
            }
        }

        private void EnterWaitingMode() {
            progressBar1.Value = 0;
            panel1.Enabled = false;
            progressBar1.Visible = true;
            lblScore.Text = "";
            lblTime.Text = "";
            _stopwatch.Restart();
        }

        private void Complete(double score) {
            _stopwatch.Stop();
            panel1.Enabled = true;
            progressBar1.Visible = false;
            lblScore.Text = score.ToString("0.00");
            lblTime.Text = string.Format("({0:#.000} secs)", _stopwatch.Elapsed.TotalSeconds);
        }

        private void btnWait_Click(object sender, EventArgs e)
        {
            EnterWaitingMode();
            new System.Threading.Timer(
                _ => Invoke(new Action(()=>Complete(0))), 
                null, 
                TimeSpan.FromSeconds(3), 
                TimeSpan.FromMilliseconds(-1));
        }

        private void btnCalculate_Click(object sender, EventArgs e) {
            var calc = new RiskCalculator();
            EnterWaitingMode();
            double score = calc.GetScore(txtSSN.Text, CreditAgencies);
            Complete(score);
        }

        private void btnAsyncToday_Click(object sender, EventArgs e)
        {
            var calc = new RiskCalculator();
            EnterWaitingMode();
            calc.GetScoreAsyncToday(txtSSN.Text, CreditAgencies, score => {
                Debug.WriteLine("Async done");
                Complete(score);
            });
        }


        private async void btnCalcAsync_Click(object sender, EventArgs e) {
            var calc = new RiskCalculator();
            EnterWaitingMode();
            double score = await calc.GetScoreAsync(txtSSN.Text, CreditAgencies);
            Complete(score);
        }

        private async void btnParallel_Click(object sender, EventArgs e)
        {
            var calc = new RiskCalculator();
            EnterWaitingMode();
            double score = await calc.GetScoreParallelAsync(txtSSN.Text, CreditAgencies);
            Complete(score);
        }
    }
}
