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 TrunksIousOutInStore
{
  public partial class WeightSettingFrom : 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 WeightSettingFrom()
    {
      InitializeComponent();
      weightSet.DataSource = weight;
      comSet.DataSource = com;
      rateSet.DataSource = rate;
      bitSet.DataSource = bit;
      weightReadType.DataSource = weightRead;
      if (!string.IsNullOrEmpty(TrunksIousOutInStoreContext.Config.WeightSet))
        weightSet.SelectedIndex = weight.IndexOf(TrunksIousOutInStoreContext.Config.WeightSet);
      else
        weightSet.SelectedIndex = 0;
      if (!string.IsNullOrEmpty(TrunksIousOutInStoreContext.Config.ComSet))
        comSet.SelectedIndex = com.IndexOf(TrunksIousOutInStoreContext.Config.ComSet);
      else
        comSet.SelectedIndex = 0;
      if (TrunksIousOutInStoreContext.Config.RateSet.HasValue)
        rateSet.SelectedIndex = rate.IndexOf(TrunksIousOutInStoreContext.Config.RateSet.ToString());
      else
        rateSet.SelectedIndex = 2;
      if (TrunksIousOutInStoreContext.Config.BitSet.HasValue)
        bitSet.SelectedIndex = bit.IndexOf(TrunksIousOutInStoreContext.Config.BitSet.ToString());
      else
        bitSet.SelectedIndex = 3;
      if (string.IsNullOrEmpty(TrunksIousOutInStoreContext.Config.Format))
        format.Text = "0.00";
      else
        format.Text = TrunksIousOutInStoreContext.Config.Format;
      if (TrunksIousOutInStoreContext.Config.Discont == null)
        discont.Text = "0.00";
      else
        discont.Text = TrunksIousOutInStoreContext.Config.Discont.ToString();
      weightReadType.SelectedIndex = TrunksIousOutInStoreContext.Config.WeightType;
      minInput.Text = TrunksIousOutInStoreContext.Config.MinWeight.ToString();
      maxInput.Text = TrunksIousOutInStoreContext.Config.MaxWeight.ToString();
    }

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

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

    private void WeightSettingFrom_Load(object sender, EventArgs e)
    {

    }
  }
}
