using SelfHelpClient.BL;
using SelfHelpClient.BO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Windows.Input;

namespace SelfHelpClient
{
  public partial class MainForm : Form
  {
    private static MainForm mForm;
    public static MainForm Form
    {
      get { return mForm; }
    }
    public MainForm()
    {
      if (!File.Exists("AppConfig.cfg"))
      {
        MessageBox.Show("请先配置服务器地址");
        this.Close();
      }
      WeightBillBL.ServerUrl = File.ReadAllText("AppConfig.cfg");
      mForm = this;
      InitializeComponent();
      DisableWPFTabletSupport();
    }

    private void selfBtn_Click(object sender, EventArgs e)
    {
       ReadCard();
       if (Program.ve == null)
        return;
       if (Program.ve.BillType == 0)
      {
        DialogForm.ShowDialog("没有待办理业务", 5);
        return;
      }
       new SelfHelpForm(Program.ve.ID).ShowDialog();
      //this.Hide();
    }

    private void weightBtn_Click(object sender, EventArgs e)
    {
       ReadCard();
       if (Program.ve == null)
        return;
       new WeightForm(Program.ve).Show();
      this.Hide();
    }

    void ReadCard()
    {
      Program.ve = null;
      var reader = new ReadCardForm();
      reader.ShowDialog();
    }

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

    public static void DisableWPFTabletSupport()
    {
      // Get a collection of the tablet devices for this window.  
      var devices = System.Windows.Input.Tablet.TabletDevices;

      if (devices.Count > 0)
      {
        // Get the Type of InputManager.
        Type inputManagerType = typeof(System.Windows.Input.InputManager);

        // Call the StylusLogic method on the InputManager.Current instance.
        object stylusLogic = inputManagerType.InvokeMember("StylusLogic",
                    BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
                    null, InputManager.Current, null);

        if (stylusLogic != null)
        {
          //  Get the type of the device class.
          Type devicesType = devices.GetType();

          // Loop until there are no more devices to remove.
          int count = devices.Count + 1;

          while (devices.Count > 0)
          {
            // Remove the first tablet device in the devices collection.
            devicesType.InvokeMember("HandleTabletRemoved", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic, null, devices, new object[] { (uint)0 });

            count--;

            if (devices.Count != count)
            {
              throw new Win32Exception("Unable to remove real-time stylus support.");
            }
          }
        }
      }
    }
  }
}
