using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BO.BO.Dtos;
using BO.Utils;
using BO.Utils.BillRpc;

namespace SegmentationWeight
{
  public partial class SegmentationWeightGoodsSetForm : Form
  {
    public readonly static string SegmentationWeightGoodsSetFileName = "SegmentationWeightGoodSet.xml";
    private List<SegmentationWeightGoodSet> mLocaList;
    private List<SegmentationWeightGoodSet> mWillDeleteList;

    public SegmentationWeightGoodsSetForm()
    {
      InitializeComponent();
    }

    private void SegmentationWeightGoodsSetForm_Load(object sender, EventArgs e)
    {
      //      CheckAddCreateConfig();
      mLocaList = XmlUtil.DeserializeFromFile<List<SegmentationWeightGoodSet>>(SegmentationWeightGoodsSetFileName);

      var fromRpclist = ClientGoodsSetRpc.GetList();
      foreach (ClientGoodsSetDto rpcSet in fromRpclist)
      {
        var set = new SegmentationWeightGoodSet();
        set.Name = rpcSet.Name;
        set.Goods_ID = rpcSet.Goods_ID;
        set.Goods_Name = rpcSet.Goods_Name;
        set.Goods_Code = rpcSet.Goods_Code;
        set.Goods_Spec = rpcSet.Goods_Spec;

        var fd = mLocaList.FirstOrDefault(x => x.Goods_ID == set.Goods_ID);
        if (fd != null)
        {
          fd.Name = set.Name;
          fd.Goods_Name = set.Goods_Name;
          fd.Goods_Code = set.Goods_Code;
          fd.Goods_Spec = set.Goods_Spec;
        }
        else
        {
          mLocaList.Add(set);
        }
      }

      InitControl();

    }

    private void InitControl()
    {
      flpClass.Controls.Clear();
      foreach (var grouping in mLocaList.GroupBy(x => x.Name))
      {
        var btn = CreateClassButton(grouping.Key);
        flpClass.Controls.Add(btn);

      }
    }

    private Button CreateClassButton(string text)
    {
      var button = new Button();
      button.Text = text;
      button.Click += classButton_Click;
      button.Width = 100;
      button.Height = 60;
      return button;

    }

    private void classButton_Click(object sender, EventArgs e)
    {
      var btn = sender as Button;
      foreach (Button cbutton in flpClass.Controls)
      {
        if (btn.Text == cbutton.Text)
        {
          cbutton.BackColor = Color.Aqua;
        }
        else
        {
          cbutton.BackColor = SystemColors.Control;
        }
      }


      flpGoods.Controls.Clear();
      foreach (SegmentationWeightGoodSet set in mLocaList.Where(x => x.Name == btn.Text))
      {
        var button = CreateGoodsButton(set);
        flpGoods.Controls.Add(button);
      }

    }

    private Button CreateGoodsButton(SegmentationWeightGoodSet set)
    {
      var btn=new Button();
      btn.Text = set.Goods_Name;
      btn.Tag = set;
      btn.Click += GoodsBtn_Click;
      btn.Width = 100;
      btn.Height = 60;
      if (set.IsSelected)
      {
        btn.BackColor=Color.Aqua;
      }

      return btn;
    }

    private void GoodsBtn_Click(object sender, EventArgs e)
    {
      var btn = sender as Button;
      var set = btn.Tag as SegmentationWeightGoodSet;
      if (btn.BackColor == Color.Aqua)
      {
        set.IsSelected = false;
        btn.BackColor = SystemColors.Control;
      }
      else
      {
        set.IsSelected = true;
        btn.BackColor = Color.Aqua;
      }
    }

    private void CheckAddCreateConfig()
    {
      if (!File.Exists(SegmentationWeightGoodsSetFileName))
      {
        var list = new List<SegmentationWeightGoodSet>();
        XmlUtil.SerializerObjToFile(list, SegmentationWeightGoodsSetFileName);
      }
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
      if (mLocaList != null)
      {
        XmlUtil.SerializerObjToFile(mLocaList, SegmentationWeightGoodsSetFileName);
        DialogResult = DialogResult.OK;
      }
      Close();

    }
  }
}
