using ButcherFactory.BO.Utils;
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;
using WinFormControl;

namespace ButcherFactory.SegmentSaleOut_
{
  public partial class DiscontSetDialog : Form
  {
    public string disCont = string.Empty;
    SegmentSaleOutFormConfig config;
    List<Label> lbls;
    public DiscontSetDialog()
    {
      InitializeComponent();
      config = XmlUtil.DeserializeFromFile<SegmentSaleOutFormConfig>();
      disCont = config.DiscontWeight;
      lbls = new List<Label> { lbl1, lbl2, lbl3, lbl4 };
    }

    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);
      var arr = config.DiscontWeight.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => decimal.Parse(x)).OrderByDescending(x => x);
      var idx = 0;
      foreach (var item in arr)
      {
        lbls[idx].Text = item.ToString("#0.##");
        idx++;
        if (idx == 4)
          break;
      }
    }

    private void lbl1_Click(object sender, EventArgs e)
    {
      var keyBoard = new NumberPad();
      if (keyBoard.ShowDialog() == true)
        (sender as Label).Text = keyBoard.Result;
    }

    private void okBtn_Click(object sender, EventArgs e)
    {
      List<decimal> arr = new List<decimal>();
      foreach (var item in lbls)
      {
        if (!string.IsNullOrEmpty(item.Text))
          arr.Add(decimal.Parse(item.Text));
      }

      disCont = string.Join(",", arr.Distinct().OrderBy(x => x));
      if (config.DiscontWeight != disCont)
      {
        config.DiscontWeight = disCont;
        XmlUtil.SerializerObjToFile(config);
        DialogResult = DialogResult.OK;
      }
      else
        DialogResult = DialogResult.Cancel;
    }
  }
}
