using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using BO;
using BO.Utils;
using SegmentationInStore.Rpc;
using SegmentationInStore.Rpc.Dto;

namespace SegmentationInStore
{
  public partial class SegmentationInStoreForm : Form,IAfterLogin
  {
    private static readonly Object lockOjbUnScan = new object();
    private static readonly Object lockOjbUnSubmit = new object();
    private static readonly Object lockOjbSubmited = new object();

    private BindingList<SegmentationWeightRecord> mUnScanList;
    private BindingList<SegmentationInStoreDto> mUnSubmitList;
    private BindingList<SegmentationInStoreDto> mSubmitedList;


    BardCodeHooK BarCode = new BardCodeHooK();
    public SegmentationInStoreForm()
    {
      InitializeComponent();
      gridUnScan.AutoGenerateColumns = false;
      gridUnSubmit.AutoGenerateColumns = false;
      gridSubmited.AutoGenerateColumns = false;
      mUnScanList=new BindingList<SegmentationWeightRecord>();
      mUnSubmitList = new BindingList<SegmentationInStoreDto>();
      mSubmitedList = new BindingList<SegmentationInStoreDto>();
      BarCode.BarCodeEvent += new BardCodeHooK.BardCodeDeletegate(BarCode_BarCodeEvent);
    }

    void BarCode_BarCodeEvent(BardCodeHooK.BarCodes barCode)
    {
      ShowInfo(barCode);
    }
    private delegate void ShowInfoDelegate(BardCodeHooK.BarCodes barCode);

    private void ShowInfo(BardCodeHooK.BarCodes barCode)
    {
      if (this.InvokeRequired)
      {
        this.BeginInvoke(new ShowInfoDelegate(ShowInfo), new object[] { barCode });
      }
      else
      {
        if (barCode.IsValid)
        {
          if (!string.IsNullOrWhiteSpace(barCode.BarCode))
          {
            doInsertUnSubmit(barCode.BarCode);
          }
        }
      }
    }

    private void doInsertUnSubmit(string barCode)
    {
      var fd = mUnScanList.FirstOrDefault(x => x.BarCode == barCode);
      if (fd == null)
      {
        //todo 放到错误信息里
      }
      mUnScanList.Remove(fd);
      var inStoreDto = fd.EToSegmentationInStore();
      inStoreDto.Store_ID = 0;
      inStoreDto.Store_Name = "仓库";
      mUnSubmitList.Insert(0, inStoreDto);
      BindUnSubmitGrid();
    }

    void BindUnScanGrid()
    {
      lock (lockOjbUnScan)
      {
        gridUnScan.DataSource = mUnScanList;
      }
    }
    void BindUnSubmitGrid()
    {
      lock (lockOjbUnSubmit)
      {
        gridUnSubmit.DataSource = mUnSubmitList;
      }
    }
    void BindSubmitedGrid()
    {
      lock (lockOjbSubmited)
      {
        gridSubmited.DataSource = mSubmitedList;
      }
    }



    private void SegmentationInStoreForm_Load(object sender, EventArgs e)
    {
      var enable=BarCode.Start();
      if (!enable)
      {
        MessageBox.Show("注册扫码失败，联系管理员");
      }
    }

    //同步未扫码
    private void timerSyncUnScan_Tick(object sender, EventArgs e)
    {
      if (mUnScanList.Count < 1)
      {
        var list = SegmentationInStoreRpc.GetNotInStoreList();
        foreach (SegmentationWeightRecord record in list)
        {
          mUnScanList.Insert(0,record);
        }
      }
      else
      {
        var maxid = mUnScanList.Max(x => x.ID);
        var list = SegmentationInStoreRpc.GetNotInStoreListByMaxId(maxid);
        foreach (SegmentationWeightRecord record in list)
        {
          mUnScanList.Insert(0,record);
        }
      }
      BindUnScanGrid();
    }

    //扫到的 未提交的提交
    private void timerUpload_Tick(object sender, EventArgs e)
    {
      var unSubmitList = mUnSubmitList.ToList();
      for (int i = unSubmitList.Count-1; i >=0 ; i--)
      {
        try
        {
          var dto = unSubmitList[i];
          dto.CreateTime = DateTime.Now;
          dto.ID=SegmentationInStoreRpc.Insert(dto);
          mSubmitedList.Insert(0,dto);
          mUnSubmitList.Remove(dto);
        }
        catch (Exception ex)
        {
          //todo 记录异常
        }
      }
      //把成功的从未提交记录中移除
      BindUnSubmitGrid();
      BindSubmitedGrid();
 
    }

    public string RoleName { get { return "分割入库"; } }
    public Form Generate()
    {
      return this;
    }

    private void SegmentationInStoreForm_FormClosed(object sender, FormClosedEventArgs e)
    {
      BarCode.Stop();
    }
  }
}
