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 WeighAndGrading
{
  public partial class GradeSettingFrom : 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" };
    List<string> weightRead = new List<string> { "稳定读取", "连续发送" };
    public GradeSettingFrom()
    {
      InitializeComponent();
      weightSet.DataSource = weight;
      comSet.DataSource = com;
      rateSet.DataSource = rate;
      bitSet.DataSource = bit;
      weightReadType.DataSource = weightRead;
      if (!string.IsNullOrEmpty(GradeContext.Config.WeightSet))
        weightSet.SelectedIndex = weight.IndexOf(GradeContext.Config.WeightSet);
      else
        weightSet.SelectedIndex = 0;
      if (!string.IsNullOrEmpty(GradeContext.Config.ComSet))
        comSet.SelectedIndex = com.IndexOf(GradeContext.Config.ComSet);
      else
        comSet.SelectedIndex = 0;
      if (GradeContext.Config.RateSet.HasValue)
        rateSet.SelectedIndex = rate.IndexOf(GradeContext.Config.RateSet.ToString());
      else
        rateSet.SelectedIndex = 2;
      if (GradeContext.Config.BitSet.HasValue)
        bitSet.SelectedIndex = bit.IndexOf(GradeContext.Config.BitSet.ToString());
      else
        bitSet.SelectedIndex = 3;
      if (string.IsNullOrEmpty(GradeContext.Config.Format))
        format.Text = "0.00";
      else
        format.Text = GradeContext.Config.Format;
      if (GradeContext.Config.Discont == null)
        discont.Text = "0.00";
      else
        discont.Text = GradeContext.Config.Discont.ToString();
      weightReadType.SelectedIndex = GradeContext.Config.WeightType;
      minInput.Text = GradeContext.Config.MinWeight.ToString();
      maxInput.Text = GradeContext.Config.MaxWeight.ToString();
    }

    private void saveBtn_Click(object sender, EventArgs e)
    {
      GradeContext.Config.WeightSet = weight[this.weightSet.SelectedIndex];
      GradeContext.Config.ComSet = com[this.comSet.SelectedIndex];
      GradeContext.Config.RateSet = int.Parse(rate[this.rateSet.SelectedIndex]);
      GradeContext.Config.BitSet = int.Parse(bit[this.bitSet.SelectedIndex]);
      GradeContext.Config.Format = format.Text;
      GradeContext.Config.WeightType = weightReadType.SelectedIndex;
      if (GradeContext.Config.WeightType == 1)
      {
        decimal min = 0;
        decimal max = 0;
        if (!decimal.TryParse(minInput.Text.Trim(), out min))
          throw new Exception("连续发送时 必须输入有效区间");
        if (!decimal.TryParse(maxInput.Text.Trim(), out max))
          throw new Exception("连续发送时 必须输入有效区间");
        GradeContext.Config.MinWeight = min;
        GradeContext.Config.MaxWeight = max;
      }
      if (!string.IsNullOrEmpty(discont.Text))
      {
        decimal v;
        if (decimal.TryParse(discont.Text, out v))
          GradeContext.Config.Discont = v;
        else
          throw new Exception("扣重格式输入不正确");
      }
      else
        GradeContext.Config.Discont = 0;
      GradeContext.Save();
      MessageBox.Show("保存成功！");
    }

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