using System;
using System.Globalization;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Message;
using System.Reflection;

namespace ___C_File_Editor
{
  public partial class MainWindow : Window
  {

    private void MainWindowTitle()
    {
      Assembly thisAssem = Assembly.GetExecutingAssembly();
      Version ver = thisAssem.GetName().Version;
      labelTitle.Content = thisAssem.GetName().Name + " " + ver.Major + "." + ver.Minor;
      if (ver.Build > 0)
      {
        labelTitle.Content += "." + ver.Build;
      }
    }

    private void MainWindow_DragOver(object sender, DragEventArgs e)
    {
      e.Handled = true;
      if (e.Data.GetDataPresent(DataFormats.FileDrop))
      {
        e.Effects = DragDropEffects.Copy;
      }
      else
      {
        e.Effects = DragDropEffects.None;
      }
    }

    private void exitCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
      e.CanExecute = true; // 常に有効にする
    }
    private void exitCommand_Executed(object sender, ExecutedRoutedEventArgs e)
    {
      if (modified)
      {
        if (MessageWindow.Show(this, txt["ExitWithoutSave"], txt["Confirm"], txt["Exit"], txt["Cancel"]) == "Cancel")
        {
          return;
        }
      }
      Close();
    }

    private void pathPasteCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
      e.CanExecute = true; // 常に有効にする
    }
    private void pathPasteCommand_Executed(object sender, ExecutedRoutedEventArgs e)
    {
      OpenFromClipboard();
    }

    private void tb_OpenFromClipboard(object sender, KeyEventArgs e)
    {
      TextBox tb = sender as TextBox;
      if (tb.IsReadOnly && e.Key == Key.V && Keyboard.Modifiers == ModifierKeys.Control)
      {
        OpenFromClipboard();
      }
    }

    private void ButtonMinimized_Click(object sender, RoutedEventArgs e)
    {
      this.WindowState = System.Windows.WindowState.Minimized;
    }

    private void ButtonMaximized_Click(object sender, RoutedEventArgs e)
    {
      this.WindowState = System.Windows.WindowState.Maximized;
    }

    private void ButtonNormal_Click(object sender, RoutedEventArgs e)
    {
      this.WindowState = System.Windows.WindowState.Normal;
    }

    private void tb_LostFocus(object sender, RoutedEventArgs e)
    {
      TextBox tb = sender as TextBox;
      tb.Text = tb.Text.Trim();
    }

    private void tbNum_KeyDown(object sender, KeyEventArgs e)
    {
      TextBox target = sender as TextBox;
      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 == Key.Decimal || e.Key == Key.OemPeriod)
      {
        if (target.Text.IndexOf(".") < 0 || target.SelectedText.IndexOf(".") >= 0)
        {
          e.Handled = false;
        }
        else
        {
          e.Handled = true;
        }
      }
      else if (e.Key == Key.Divide || e.Key == Key.Subtract)
      {
        if ((target.Text == target.SelectedText || target.SelectionStart == 0) &&
            (target.Text.IndexOf("-") < 0 || target.SelectedText.IndexOf("-") >= 0))
        {
          e.Handled = false;
        }
        else
        {
          e.Handled = true;
        }
      }
      else
      {
        e.Handled = true;
      }
    }

    private void tbPositiveNum_KeyDown(object sender, KeyEventArgs e)
    {
      TextBox target = sender as TextBox;
      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 == Key.Decimal || e.Key == Key.OemPeriod)
      {
        if (target.Text.IndexOf(".") < 0 || target.SelectedText.IndexOf(".") >= 0)
        {
          e.Handled = false;
        }
        else
        {
          e.Handled = true;
        }
      }
      else
      {
        e.Handled = true;
      }
    }

    private void tbNums_KeyDown(object sender, KeyEventArgs e)
    {
      TextBox target = sender as TextBox;
      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 == Key.OemComma)
      {
        e.Handled = false;
      }
      else
      {
        e.Handled = true;
      }
    }

    private void tbUint_KeyDown(object sender, KeyEventArgs e)
    {
      TextBox target = sender as TextBox;
      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
      {
        e.Handled = true;
      }
    }

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

    private void tb_PreMouseLeftDown(object sender, MouseButtonEventArgs e)
    {
      TextBox target = sender as TextBox;
      if (target.IsFocused == false)
      {
        target.Focus();
        e.Handled = true;
      }
    }

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



    private uint hexTextToUInt(string str)
    {
      byte[] bytes = BitConverter.GetBytes(Int32.Parse(str, NumberStyles.AllowHexSpecifier));
      uint num = BitConverter.ToUInt32(bytes, 0);
      return num;
    }

    private string uintToHexText(uint num)
    {
      byte[] bytes = BitConverter.GetBytes(num);
      Array.Reverse(bytes);
      string str = BitConverter.ToString(bytes).Replace("-", "");
      return str;
    }

    private List<byte> replaceByteList(List<byte> list, byte[] replace, int index)
    {
      list.RemoveRange(index, replace.Length);
      list.InsertRange(index, replace);
      return list;
    }
    
    private void mainWindow_Activated(object sender, EventArgs e)
    {
      if (!appStarted)
      {
        var story = (System.Windows.Media.Animation.Storyboard)this.FindResource("fadeIn");
        story.Begin();
        appStarted = true;
      }
    }

    private void FadeInAnimation_Completed(object sender, EventArgs e)
    {
      var story = (System.Windows.Media.Animation.Storyboard)this.FindResource("fadeIn");
      story.Stop();
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
      var story = (System.Windows.Media.Animation.Storyboard)this.FindResource("fadeOut");
      story.Begin();
      e.Cancel = true;
    }

    private void FadeOutAnimation_Completed(object sender, EventArgs e)
    {
      var story = (System.Windows.Media.Animation.Storyboard)this.FindResource("fadeOut");
      story.Stop();

      Application.Current.Shutdown();
    }

  }
}
