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 ButcherWeight
{
  public partial class WeightConfig : Form
  {
    List<string> weight = new List<string> { "IND560", "Xk3124", "Xk3190A9", "Xk3190D10" };
    List<string> com = new List<string> { "COM1", "COM2", "COM3", "COM4", "COM5" };
    List<string> rate = new List<string> { "4800", "7200", "9600" };
    List<string> bit = new List<string> { "5", "6", "7", "8" };
    public WeightConfig()
    {
      InitializeComponent();
      weightSet.DataSource = weight;
      comSet.DataSource = com;
      rateSet.DataSource = rate;
      bitSet.DataSource = bit;
      if (!string.IsNullOrEmpty(WeightContext.Config.WeightSet))
        weightSet.SelectedIndex = weight.IndexOf(WeightContext.Config.WeightSet);
      else
        weightSet.SelectedIndex = 0;
      if (!string.IsNullOrEmpty(WeightContext.Config.ComSet))
        comSet.SelectedIndex = com.IndexOf(WeightContext.Config.ComSet);
      else
        comSet.SelectedIndex = 0;
      if (WeightContext.Config.RateSet.HasValue)
        rateSet.SelectedIndex = rate.IndexOf(WeightContext.Config.RateSet.ToString());
      else
        rateSet.SelectedIndex = 2;
      if (WeightContext.Config.BitSet.HasValue)
        bitSet.SelectedIndex = bit.IndexOf(WeightContext.Config.BitSet.ToString());
      else
        bitSet.SelectedIndex = 3;
      if (string.IsNullOrEmpty(WeightContext.Config.Format))
        format.Text = "0.00";
      else
        format.Text = WeightContext.Config.Format;
    }

    private void saveBtn_Click(object sender, EventArgs e)
    {
      WeightContext.Config.WeightSet = weight[this.weightSet.SelectedIndex];
      WeightContext.Config.ComSet = com[this.comSet.SelectedIndex];
      WeightContext.Config.RateSet = int.Parse(rate[this.rateSet.SelectedIndex]);
      WeightContext.Config.BitSet = int.Parse(bit[this.bitSet.SelectedIndex]);
      WeightContext.Config.Format = format.Text;
      WeightContext.Save();
      MessageBox.Show("保存成功！");
    }

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