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.Dialogs
{
  public partial class NumberSetDialog : Form
  {
    const string FilePatch = @"Config\NumberSetDialog.cfg";
    public NumberSetDialog()
    {
      InitializeComponent();
      if (System.IO.File.Exists(FilePatch))
      {
        var text = System.IO.File.ReadAllText(FilePatch);
        var arr = text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (var item in arr)
          flowLayoutPanel1.Controls.Add(BuildBtn(item));
      }
    }

    private void setBtn_Click(object sender, EventArgs e)
    {
      if (string.IsNullOrEmpty(numBox.Text))
        return;
      var selected = flowLayoutPanel1.Controls.Cast<UButton>().FirstOrDefault(x => x.AsClicked);
      if (selected != null)
      {
        selected.Text = numBox.Text;
        selected.AsClicked = false;
      }
      else if (flowLayoutPanel1.Controls.Count == 3)
        throw new Exception("最多设置3项");
      else
        flowLayoutPanel1.Controls.Add(BuildBtn(numBox.Text));
      SaveConfig();
    }

    UButton BuildBtn(string text)
    {
      var btn = new UButton() { Width = 100, Height = 34, Text = text, Font = new Font("宋体", 15), Margin = new Padding(6, 2, 6, 0), WithStataHode = true, EnableGroup = true };
      return btn;
    }

    private void clearAllBtn_Click(object sender, EventArgs e)
    {
      flowLayoutPanel1.Controls.Clear();
      SaveConfig();
    }

    void SaveConfig()
    {
      var arr = flowLayoutPanel1.Controls.Cast<UButton>().Select(x => x.Text);
      System.IO.File.WriteAllText(FilePatch, string.Join(",", arr));
    }
  }
}
