using System;
using System.Globalization;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Data;
using System.ComponentModel;
using System.Collections.ObjectModel;
using Message;

namespace TMC_Tool
{
  /// <summary>
  /// EditMaterialWindow.xaml の相互作用ロジック
  /// </summary>
  public partial class InputObjNameWindow : Window
  {
    private static Dictionary<string, string> txt = new Dictionary<string, string>();

    private static bool dialogResult;

    private static TMCData tmcData;
    private static TmcTables tmctables;

    private bool IsActivated = false;

    public InputObjNameWindow()
    {
      InitializeComponent();

      this.MouseLeftButtonDown += (sender, e) => this.DragMove();
      this.ShowInTaskbar = false;

      title.Text = txt["inputObjNameTitle"];
      btnOk.Content = txt["OK"];
      btnCancel.Content = txt["Cancel"];
    }

    public static void lang(Dictionary<string, string> langText)
    {
      txt = langText;
    }

    private void btnOk_Click(object sender, RoutedEventArgs e)
    {
      dialogResult = true;
      this.Close();
    }

    private void btnCancel_Click(object sender, RoutedEventArgs e)
    {
      dialogResult = false;
    }

    private void tbName_KeyDown(object sender, KeyEventArgs e)
    {
      TextBox tb = sender as TextBox;
      ModifierKeys modifierKeys = Keyboard.Modifiers;

      //Console.WriteLine(e.Key.GetHashCode());

      if (e.Key == Key.System || e.Key == Key.Tab)
      {
        e.Handled = false;
      }
      else if ((Key.D0 <= e.Key && e.Key <= Key.D9) || (Key.NumPad0 <= e.Key && e.Key <= Key.NumPad9))
      {
        e.Handled = false;
      }
      else if (e.Key.GetHashCode() >= 44 && e.Key.GetHashCode() <= 69)
      {
        e.Handled = false;
      }
      else if (e.Key == Key.OemBackslash && (modifierKeys & ModifierKeys.Shift) != ModifierKeys.None)
      {
        e.Handled = false;
      }
      else
      {
        e.Handled = true;
      }

      //btnOk.IsEnabled = true;
    }

    private void tbName_KeyUp(object sender, KeyEventArgs e)
    {
      CheckObjGrpName();
    }

    //private void tbName_LostFocus(object sender, RoutedEventArgs e)
    //{
    //  TextBox tb = sender as TextBox;
    //
    //
    //}

    private void tb_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
      TextBox target = sender as TextBox;
      if (target == null) return;
      target.SelectAll();
    }



    public string Show(Window owner, TMCData data, TmcTables tables, string curName)
    {
      this.Owner = owner;
      this.Owner.IsEnabled = false;
      btnOk.IsEnabled = false;

      tmcData = data;
      tmctables = tables;

      if (this.ActualHeight != 0)
      {
        this.Top = owner.Top + (owner.ActualHeight / 2) - (this.ActualHeight / 2);
        this.Left = owner.Left + (owner.ActualWidth / 2) - (this.ActualWidth / 2);
      }

      CheckNameNum(curName);
      tbName.Focus();
      tbName.SelectAll();

      IsActivated = false;
      this.ShowDialog();

      this.Owner.IsEnabled = true;
      this.Owner.Focus();

      if (dialogResult)
      {
        return "WGT_" + tbName.Text;
      }
      else
      {
        return null;
      }
    }

    public void CheckNameNum(string curName)
    {
      if (curName == "") return;

      string name = "";
      int idx = -1;
      int start = curName.IndexOf('_') + 1;

      if (curName.Substring(0, 4) != "WGT_")
      {
        name = curName.Substring(start);
        //idx = Array.FindIndex(tmcData.Node.ToArray(), node => node.Name == "WGT_" + name);
        idx = Array.FindIndex(tmctables.Nodes.ToArray(), node => node.Name == "WGT_" + name);
        if (name != "" && idx == -1)
        {
          tbName.Text = name;
          btnOk.IsEnabled = true;
          return;
        }
      }

      bool parsableNum = true;
      int num = 0;
      int numLength = 0;
      do
      {
        numLength++;
        parsableNum = Int32.TryParse(curName.Substring(curName.Length - numLength), out num);
      } while (parsableNum);
      numLength--;

      int countUp = 1;
      if (numLength > 0)
      {
        num = Int32.Parse(curName.Substring(curName.Length - numLength));
      }
      else
      {
        countUp++;
      }
      do
      {
        name = curName.Substring(start, curName.Length - 4 - numLength) + (num + countUp).ToString();
        //idx = Array.FindIndex(tmcData.Node.ToArray(), node => node.Name == "WGT_" + name);
        idx = Array.FindIndex(tmctables.Nodes.ToArray(), node => node.Name == "WGT_" + name);
        countUp++;
      } while (idx != -1);

      if (name != "")
      {
        tbName.Text = name;
        btnOk.IsEnabled = true;
      }
    }

    public void CheckObjGrpName()
    {
      int idx = Array.FindIndex(tmctables.Nodes.ToArray(), node => node.Name == "WGT_" + tbName.Text);

      if (idx == -1)
        btnOk.IsEnabled = true;
      else
        btnOk.IsEnabled = false;
  }



    private void Window_Activated(object sender, EventArgs e)
    {
      if (!IsActivated) IsActivated = true;
    }

    private void Window_Closing(object sender, CancelEventArgs e)
    {
      e.Cancel = true;
      IsActivated = false;
      this.Hide();
      if (this.Owner != null) this.Owner.Activate();
    }
  }
}
