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 WeightSettingFrom : Form
  {
    List<string> weight = new List<string> { "IND560", "Xk3124", "Xk3190A9", "Xk3190D10", "IND231", "AHS3000", "JST2018S", "一体称" };
    List<string> com = new List<string> { "COM1", "COM2", "COM3", "COM4", "COM5" };
    List<string> rate = new List<string> { "1200", "2400", "4800", "7200", "9600" };
    List<string> bit = new List<string> { "5", "6", "7", "8" };
    List<string> weightRead = new List<string> { "稳定读取", "连续发送" };
    string _flag;
    WeightConfig config;

    public WeightSettingFrom(string flag)
    {
      this._flag = flag;
      InitializeComponent();
      weightSet.DataSource = weight;
      comSet.DataSource = com;
      rateSet.DataSource = rate;
      bitSet.DataSource = bit;
      weightReadType.DataSource = weightRead;
      config = WeightConfig.Init(flag);
      if (!string.IsNullOrEmpty(config.WeightSet))
        weightSet.SelectedIndex = weight.IndexOf(config.WeightSet);
      else
        weightSet.SelectedIndex = 0;
      if (!string.IsNullOrEmpty(config.ComSet))
        comSet.SelectedIndex = com.IndexOf(config.ComSet);
      else
        comSet.SelectedIndex = 0;
      if (config.RateSet.HasValue)
        rateSet.SelectedIndex = rate.IndexOf(config.RateSet.ToString());
      else
        rateSet.SelectedIndex = 2;
      if (config.BitSet.HasValue)
        bitSet.SelectedIndex = bit.IndexOf(config.BitSet.ToString());
      else
        bitSet.SelectedIndex = 3;
      if (string.IsNullOrEmpty(config.Format))
        format.Text = "0.00";
      else
        format.Text = config.Format;
      if (config.Discont == null)
        discont.Text = "0.00";
      else
        discont.Text = config.Discont.ToString();
      weightReadType.SelectedIndex = config.WeightType;
      minInput.Text = config.MinWeight.ToString();
      maxInput.Text = config.MaxWeight.ToString();
    }

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

    private void closeBtn_Click(object sender, EventArgs e)
    {
      if (changed)
        DialogResult = DialogResult.OK;
      Close();
    }
  }
}
