using ButcherFactory.BO.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WinFormControl;

namespace ButcherFactory.Tools
{
  /// <summary>
  /// MainWindow.xaml 的交互逻辑
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
      modeComboBox.Items.Add("MES");
      modeComboBox.Items.Add("B3");
      try
      {
        modeComboBox.SelectedIndex = BO.Utils.AppContext.ConnectInfo.ServerMode;
        var list = XmlUtil.DeserializeFromFile<List<DbSelectEntity>>("Config\\DbSelectList.xml");
        dbComboBox.SelectedValuePath = "Value";
        dbComboBox.DisplayMemberPath = "Name";
        dbComboBox.ItemsSource = list;

        if (!string.IsNullOrEmpty(BO.Utils.AppContext.ConnectInfo.SqlConnection))
        {
          var idx = list.FindIndex(x => x.Value == BO.Utils.AppContext.ConnectInfo.SqlConnection);
          if (idx > -1)
            dbComboBox.SelectedIndex = idx;
        }
        this.mesUrlBox.Text = BO.Utils.AppContext.ConnectInfo.MESUrl;
        this.b3UrlBox.Text = BO.Utils.AppContext.ConnectInfo.B3Url;
        this.clientCodeBox.Text = BO.Utils.AppContext.ConnectInfo.ClientCode;
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
      }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
      if (string.IsNullOrEmpty(BO.Utils.AppContext.ConnectInfo.SqlConnection))
      {
        MessageBox.Show("请先设置数据库地址", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
      }
      try
      {
        DbUtil.UpdateDatabase(BO.Utils.AppContext.ConnectInfo.SqlConnection);
      }
      catch (Exception ex) {
        MessageBox.Show(ex.Message);
        return;
      }
      MessageBox.Show("数据库升级成功");
    }

    private void SaveBtnClick(object sender, RoutedEventArgs e)
    {
      BO.Utils.AppContext.ConnectInfo.MESUrl = this.mesUrlBox.Text.Trim();
      BO.Utils.AppContext.ConnectInfo.B3Url = this.b3UrlBox.Text.Trim();
      BO.Utils.AppContext.ConnectInfo.SqlConnection = (string)dbComboBox.SelectedValue;
      BO.Utils.AppContext.ConnectInfo.ServerMode = modeComboBox.SelectedIndex;
      var code = this.clientCodeBox.Text.Trim();
      if (!string.IsNullOrEmpty(code) && code.Length != 2)
      {
        MessageBox.Show("机器码长度必须为2位");
        return;
      }
      BO.Utils.AppContext.ConnectInfo.ClientCode = code;
      BO.Utils.AppContext.ConnectInfo.Save();

      MessageBox.Show("设置保存成功!");
    }

    private void CloseBtnClick(object sender, RoutedEventArgs e)
    {
      Application.Current.Shutdown();
    }

    private void UrlTextBoxClick(object sender, MouseButtonEventArgs e)
    {
      var box = sender as TextBox;
      box.Focus();
      if (box.Name == "mesUrlBox")
      {
        var keyBoard = new VirtualKeyPad();
        if (keyBoard.ShowDialog() == true)
          box.Text = keyBoard.Result;
      }
      else
      {
        var keyBoard = new NumberPad();
        if (keyBoard.ShowDialog() == true)
          box.Text = keyBoard.Result;
      }
    }
  }

  public class DbSelectEntity
  {
    public string Name { get; set; }

    public string Value { get; set; }
  }
}
