using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ButcherFactory.Controls
{
  public partial class InfoBox : Form
  {
    System.Timers.Timer timer;
    int sec = 0;
    public InfoBox(string title, string content,Color foreColor, int second)
    {
      InitializeComponent();
      label1.Text = content;
      this.Text = title;
      this.label1.ForeColor = foreColor;

      button1.Text = "确定";
      if (second > 0)
      {
        sec = second;
        timer = new System.Timers.Timer();
        timer.Interval = 500;
        timer.Elapsed += timer_Elapsed;
        timer.Start();
      }
      this.FormClosing += DialogForm_FormClosing;
    }

    void DialogForm_FormClosing(object sender, FormClosingEventArgs e)
    {
      if (timer != null)
      {
        if (timer.Enabled)
          timer.Enabled = false;
        timer.Dispose();
      }
    }

    void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
      this.Invoke(new Action(() =>
      {
        if (sec > 0)
        {
          button1.Text = string.Format("确定（{0}秒）", sec);
          sec--;
        }
        else
        {
          timer.Enabled = false;
          this.Close();
        }
      }));
    }

    public static void Show(string title, string msg, Color foreColor, int closeSec, IWin32Window owner)
    {
      var d = new InfoBox(title, msg, foreColor, closeSec);
      d.ShowDialog(owner);
    }

    private void button1_Click(object sender, EventArgs e)
    {
      this.Close();
    }
  }
}
